tst-cond8.c 5.4 KB

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