tst-cond8.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, 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 <stdlib.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  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. static int
  112. do_test (void)
  113. {
  114. pthread_t th;
  115. int err;
  116. printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
  117. puts ("parent: get mutex");
  118. err = pthread_barrier_init (&bar, NULL, 2);
  119. if (err != 0)
  120. {
  121. puts ("parent: cannot init barrier");
  122. exit (1);
  123. }
  124. puts ("parent: create child");
  125. err = pthread_create (&th, NULL, tf1, NULL);
  126. if (err != 0)
  127. {
  128. puts ("parent: cannot create thread");
  129. exit (1);
  130. }
  131. puts ("parent: wait for child to lock mutex");
  132. err = pthread_barrier_wait (&bar);
  133. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  134. {
  135. puts ("parent: cannot wait for barrier");
  136. exit (1);
  137. }
  138. err = pthread_mutex_lock (&mut);
  139. if (err != 0)
  140. {
  141. puts ("parent: mutex_lock failed");
  142. exit (1);
  143. }
  144. err = pthread_mutex_unlock (&mut);
  145. if (err != 0)
  146. {
  147. puts ("parent: mutex_unlock failed");
  148. exit (1);
  149. }
  150. if (pthread_cancel (th) != 0)
  151. {
  152. puts ("cannot cancel thread");
  153. exit (1);
  154. }
  155. void *r;
  156. err = pthread_join (th, &r);
  157. if (err != 0)
  158. {
  159. puts ("parent: failed to join");
  160. exit (1);
  161. }
  162. if (r != PTHREAD_CANCELED)
  163. {
  164. puts ("child hasn't been canceled");
  165. exit (1);
  166. }
  167. puts ("parent: create 2nd child");
  168. err = pthread_create (&th, NULL, tf2, NULL);
  169. if (err != 0)
  170. {
  171. puts ("parent: cannot create thread");
  172. exit (1);
  173. }
  174. puts ("parent: wait for child to lock mutex");
  175. err = pthread_barrier_wait (&bar);
  176. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  177. {
  178. puts ("parent: cannot wait for barrier");
  179. exit (1);
  180. }
  181. err = pthread_mutex_lock (&mut);
  182. if (err != 0)
  183. {
  184. puts ("parent: mutex_lock failed");
  185. exit (1);
  186. }
  187. err = pthread_mutex_unlock (&mut);
  188. if (err != 0)
  189. {
  190. puts ("parent: mutex_unlock failed");
  191. exit (1);
  192. }
  193. if (pthread_cancel (th) != 0)
  194. {
  195. puts ("cannot cancel thread");
  196. exit (1);
  197. }
  198. err = pthread_join (th, &r);
  199. if (err != 0)
  200. {
  201. puts ("parent: failed to join");
  202. exit (1);
  203. }
  204. if (r != PTHREAD_CANCELED)
  205. {
  206. puts ("child hasn't been canceled");
  207. exit (1);
  208. }
  209. puts ("done");
  210. return 0;
  211. }
  212. #define TEST_FUNCTION do_test ()
  213. #include "../test-skeleton.c"