tst-cond8.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <sys/time.h>
  21. #if defined(__GLIBC__) || defined(__UCLIBC__)
  22. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  23. static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
  24. static pthread_barrier_t bar;
  25. static void
  26. ch (void *arg)
  27. {
  28. int e = pthread_mutex_lock (&mut);
  29. if (e == 0)
  30. {
  31. puts ("mutex not locked at all by cond_wait");
  32. exit (1);
  33. }
  34. if (e != EDEADLK)
  35. {
  36. puts ("no deadlock error signaled");
  37. exit (1);
  38. }
  39. if (pthread_mutex_unlock (&mut) != 0)
  40. {
  41. puts ("ch: cannot unlock mutex");
  42. exit (1);
  43. }
  44. puts ("ch done");
  45. }
  46. static void *
  47. tf1 (void *p)
  48. {
  49. int err;
  50. if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
  51. || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
  52. {
  53. puts ("cannot set cancellation options");
  54. exit (1);
  55. }
  56. err = pthread_mutex_lock (&mut);
  57. if (err != 0)
  58. {
  59. puts ("child: cannot get mutex");
  60. exit (1);
  61. }
  62. err = pthread_barrier_wait (&bar);
  63. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  64. {
  65. printf ("barrier_wait returned %d\n", err);
  66. exit (1);
  67. }
  68. puts ("child: got mutex; waiting");
  69. pthread_cleanup_push (ch, NULL);
  70. pthread_cond_wait (&cond, &mut);
  71. pthread_cleanup_pop (0);
  72. puts ("child: cond_wait should not have returned");
  73. return NULL;
  74. }
  75. static void *
  76. tf2 (void *p)
  77. {
  78. int err;
  79. if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
  80. || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
  81. {
  82. puts ("cannot set cancellation options");
  83. exit (1);
  84. }
  85. err = pthread_mutex_lock (&mut);
  86. if (err != 0)
  87. {
  88. puts ("child: cannot get mutex");
  89. exit (1);
  90. }
  91. err = pthread_barrier_wait (&bar);
  92. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  93. {
  94. printf ("barrier_wait returned %d\n", err);
  95. exit (1);
  96. }
  97. puts ("child: got mutex; waiting");
  98. pthread_cleanup_push (ch, NULL);
  99. /* Current time. */
  100. struct timeval tv;
  101. (void) gettimeofday (&tv, NULL);
  102. /* +1000 seconds in correct format. */
  103. struct timespec ts;
  104. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  105. ts.tv_sec += 1000;
  106. pthread_cond_timedwait (&cond, &mut, &ts);
  107. pthread_cleanup_pop (0);
  108. puts ("child: cond_wait should not have returned");
  109. return NULL;
  110. }
  111. #endif
  112. static int
  113. do_test (void)
  114. {
  115. #if defined(__GLIBC__) || defined(__UCLIBC__)
  116. pthread_t th;
  117. int err;
  118. printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
  119. puts ("parent: get mutex");
  120. err = pthread_barrier_init (&bar, NULL, 2);
  121. if (err != 0)
  122. {
  123. puts ("parent: cannot init barrier");
  124. exit (1);
  125. }
  126. puts ("parent: create child");
  127. err = pthread_create (&th, NULL, tf1, NULL);
  128. if (err != 0)
  129. {
  130. puts ("parent: cannot create thread");
  131. exit (1);
  132. }
  133. puts ("parent: wait for child to lock mutex");
  134. err = pthread_barrier_wait (&bar);
  135. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  136. {
  137. puts ("parent: cannot wait for barrier");
  138. exit (1);
  139. }
  140. err = pthread_mutex_lock (&mut);
  141. if (err != 0)
  142. {
  143. puts ("parent: mutex_lock failed");
  144. exit (1);
  145. }
  146. err = pthread_mutex_unlock (&mut);
  147. if (err != 0)
  148. {
  149. puts ("parent: mutex_unlock failed");
  150. exit (1);
  151. }
  152. if (pthread_cancel (th) != 0)
  153. {
  154. puts ("cannot cancel thread");
  155. exit (1);
  156. }
  157. void *r;
  158. err = pthread_join (th, &r);
  159. if (err != 0)
  160. {
  161. puts ("parent: failed to join");
  162. exit (1);
  163. }
  164. if (r != PTHREAD_CANCELED)
  165. {
  166. puts ("child hasn't been canceled");
  167. exit (1);
  168. }
  169. puts ("parent: create 2nd child");
  170. err = pthread_create (&th, NULL, tf2, NULL);
  171. if (err != 0)
  172. {
  173. puts ("parent: cannot create thread");
  174. exit (1);
  175. }
  176. puts ("parent: wait for child to lock mutex");
  177. err = pthread_barrier_wait (&bar);
  178. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  179. {
  180. puts ("parent: cannot wait for barrier");
  181. exit (1);
  182. }
  183. err = pthread_mutex_lock (&mut);
  184. if (err != 0)
  185. {
  186. puts ("parent: mutex_lock failed");
  187. exit (1);
  188. }
  189. err = pthread_mutex_unlock (&mut);
  190. if (err != 0)
  191. {
  192. puts ("parent: mutex_unlock failed");
  193. exit (1);
  194. }
  195. if (pthread_cancel (th) != 0)
  196. {
  197. puts ("cannot cancel thread");
  198. exit (1);
  199. }
  200. err = pthread_join (th, &r);
  201. if (err != 0)
  202. {
  203. puts ("parent: failed to join");
  204. exit (1);
  205. }
  206. if (r != PTHREAD_CANCELED)
  207. {
  208. puts ("child hasn't been canceled");
  209. exit (1);
  210. }
  211. puts ("done");
  212. return 0;
  213. #else
  214. return 23;
  215. #endif
  216. }
  217. #define TEST_FUNCTION do_test ()
  218. #include "../test-skeleton.c"