tst-cond11.c 4.1 KB

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