tst-eintr2.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/time.h>
  22. #include "eintr.c"
  23. static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
  24. static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
  25. static void *
  26. tf1 (void *arg)
  27. {
  28. struct timespec ts;
  29. struct timeval tv;
  30. gettimeofday (&tv, NULL);
  31. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  32. ts.tv_sec += 10000;
  33. /* This call must never return. */
  34. int e = pthread_mutex_timedlock (&m1, &ts);
  35. char buf[100];
  36. printf ("tf1: mutex_timedlock returned: %s\n",
  37. strerror_r (e, buf, sizeof (buf)));
  38. exit (1);
  39. }
  40. static void *
  41. tf2 (void *arg)
  42. {
  43. while (1)
  44. {
  45. int e = pthread_mutex_lock (&m2);
  46. if (e != 0)
  47. {
  48. puts ("tf2: mutex_lock failed");
  49. exit (1);
  50. }
  51. e = pthread_mutex_unlock (&m2);
  52. if (e != 0)
  53. {
  54. puts ("tf2: mutex_unlock failed");
  55. exit (1);
  56. }
  57. struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
  58. nanosleep (&ts, NULL);
  59. }
  60. }
  61. static int
  62. do_test (void)
  63. {
  64. if (pthread_mutex_lock (&m1) != 0)
  65. {
  66. puts ("mutex_lock failed");
  67. exit (1);
  68. }
  69. setup_eintr (SIGUSR1, NULL);
  70. pthread_t th;
  71. char buf[100];
  72. int e = pthread_create (&th, NULL, tf1, NULL);
  73. if (e != 0)
  74. {
  75. printf ("main: 1st pthread_create failed: %s\n",
  76. strerror_r (e, buf, sizeof (buf)));
  77. exit (1);
  78. }
  79. e = pthread_create (&th, NULL, tf2, NULL);
  80. if (e != 0)
  81. {
  82. printf ("main: 2nd pthread_create failed: %s\n",
  83. strerror_r (e, buf, sizeof (buf)));
  84. exit (1);
  85. }
  86. /* This call must never return. */
  87. e = pthread_mutex_lock (&m1);
  88. printf ("main: mutex_lock returned: %s\n",
  89. strerror_r (e, buf, sizeof (buf)));
  90. return 0;
  91. }
  92. #define EXPECTED_SIGNAL SIGALRM
  93. #define TIMEOUT 3
  94. #define TEST_FUNCTION do_test ()
  95. #include "../test-skeleton.c"