tst-cond11.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (C) 2003, 2004 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 <stdio.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
  21. static int
  22. run_test (clockid_t cl)
  23. {
  24. pthread_condattr_t condattr;
  25. pthread_cond_t cond;
  26. pthread_mutexattr_t mutattr;
  27. pthread_mutex_t mut;
  28. printf ("clock = %d\n", (int) cl);
  29. if (pthread_condattr_init (&condattr) != 0)
  30. {
  31. puts ("condattr_init failed");
  32. return 1;
  33. }
  34. if (pthread_condattr_setclock (&condattr, cl) != 0)
  35. {
  36. puts ("condattr_setclock failed");
  37. return 1;
  38. }
  39. clockid_t cl2;
  40. if (pthread_condattr_getclock (&condattr, &cl2) != 0)
  41. {
  42. puts ("condattr_getclock failed");
  43. return 1;
  44. }
  45. if (cl != cl2)
  46. {
  47. printf ("condattr_getclock returned wrong value: %d, expected %d\n",
  48. (int) cl2, (int) cl);
  49. return 1;
  50. }
  51. if (pthread_cond_init (&cond, &condattr) != 0)
  52. {
  53. puts ("cond_init failed");
  54. return 1;
  55. }
  56. if (pthread_condattr_destroy (&condattr) != 0)
  57. {
  58. puts ("condattr_destroy failed");
  59. return 1;
  60. }
  61. if (pthread_mutexattr_init (&mutattr) != 0)
  62. {
  63. puts ("mutexattr_init failed");
  64. return 1;
  65. }
  66. if (pthread_mutexattr_settype (&mutattr, PTHREAD_MUTEX_ERRORCHECK) != 0)
  67. {
  68. puts ("mutexattr_settype failed");
  69. return 1;
  70. }
  71. if (pthread_mutex_init (&mut, &mutattr) != 0)
  72. {
  73. puts ("mutex_init failed");
  74. return 1;
  75. }
  76. if (pthread_mutexattr_destroy (&mutattr) != 0)
  77. {
  78. puts ("mutexattr_destroy failed");
  79. return 1;
  80. }
  81. if (pthread_mutex_lock (&mut) != 0)
  82. {
  83. puts ("mutex_lock failed");
  84. return 1;
  85. }
  86. if (pthread_mutex_lock (&mut) != EDEADLK)
  87. {
  88. puts ("2nd mutex_lock did not return EDEADLK");
  89. return 1;
  90. }
  91. struct timespec ts;
  92. if (clock_gettime (cl, &ts) != 0)
  93. {
  94. puts ("clock_gettime failed");
  95. return 1;
  96. }
  97. /* Wait one second. */
  98. ++ts.tv_sec;
  99. int e = pthread_cond_timedwait (&cond, &mut, &ts);
  100. if (e == 0)
  101. {
  102. puts ("cond_timedwait succeeded");
  103. return 1;
  104. }
  105. else if (e != ETIMEDOUT)
  106. {
  107. puts ("cond_timedwait did not return ETIMEDOUT");
  108. return 1;
  109. }
  110. if (pthread_mutex_unlock (&mut) != 0)
  111. {
  112. puts ("mutex_unlock failed");
  113. return 1;
  114. }
  115. if (pthread_mutex_destroy (&mut) != 0)
  116. {
  117. puts ("mutex_destroy failed");
  118. return 1;
  119. }
  120. if (pthread_cond_destroy (&cond) != 0)
  121. {
  122. puts ("cond_destroy failed");
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. #endif
  128. static int
  129. do_test (void)
  130. {
  131. #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
  132. puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
  133. return 0;
  134. #else
  135. int res = run_test (CLOCK_REALTIME);
  136. # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
  137. # if _POSIX_MONOTONIC_CLOCK == 0
  138. int e = sysconf (_SC_MONOTONIC_CLOCK);
  139. if (e < 0)
  140. puts ("CLOCK_MONOTONIC not supported");
  141. else if (e == 0)
  142. {
  143. puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
  144. res = 1;
  145. }
  146. else
  147. # endif
  148. res |= run_test (CLOCK_MONOTONIC);
  149. # else
  150. puts ("_POSIX_MONOTONIC_CLOCK not defined");
  151. # endif
  152. return res;
  153. #endif
  154. }
  155. #define TIMEOUT 3
  156. #define TEST_FUNCTION do_test ()
  157. #include "../test-skeleton.c"