tst-mutex3.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. static pthread_mutex_t m;
  20. static pthread_barrier_t b;
  21. static void *
  22. tf (void *arg)
  23. {
  24. int e = pthread_mutex_unlock (&m);
  25. if (e == 0)
  26. {
  27. puts ("1st mutex_unlock in child succeeded");
  28. exit (1);
  29. }
  30. if (e != EPERM)
  31. {
  32. puts ("1st mutex_unlock in child didn't return EPERM");
  33. exit (1);
  34. }
  35. e = pthread_mutex_trylock (&m);
  36. if (e == 0)
  37. {
  38. puts ("mutex_trylock in second thread succeeded");
  39. exit (1);
  40. }
  41. if (e != EBUSY)
  42. {
  43. puts ("mutex_trylock returned wrong value");
  44. exit (1);
  45. }
  46. e = pthread_barrier_wait (&b);
  47. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  48. {
  49. puts ("barrier_wait failed");
  50. exit (1);
  51. }
  52. e = pthread_barrier_wait (&b);
  53. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  54. {
  55. puts ("barrier_wait failed");
  56. exit (1);
  57. }
  58. e = pthread_mutex_unlock (&m);
  59. if (e == 0)
  60. {
  61. puts ("2nd mutex_unlock in child succeeded");
  62. exit (1);
  63. }
  64. if (e != EPERM)
  65. {
  66. puts ("2nd mutex_unlock in child didn't return EPERM");
  67. exit (1);
  68. }
  69. if (pthread_mutex_trylock (&m) != 0)
  70. {
  71. puts ("2nd mutex_trylock in second thread failed");
  72. exit (1);
  73. }
  74. if (pthread_mutex_unlock (&m) != 0)
  75. {
  76. puts ("3rd mutex_unlock in second thread failed");
  77. exit (1);
  78. }
  79. return NULL;
  80. }
  81. static int
  82. do_test (void)
  83. {
  84. pthread_mutexattr_t a;
  85. if (pthread_mutexattr_init (&a) != 0)
  86. {
  87. puts ("mutexattr_init failed");
  88. return 1;
  89. }
  90. if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE) != 0)
  91. {
  92. puts ("mutexattr_settype failed");
  93. return 1;
  94. }
  95. if (pthread_mutex_init (&m, &a) != 0)
  96. {
  97. puts ("mutex_init failed");
  98. return 1;
  99. }
  100. if (pthread_barrier_init (&b, NULL, 2) != 0)
  101. {
  102. puts ("barrier_init failed");
  103. return 1;
  104. }
  105. if (pthread_mutex_lock (&m) != 0)
  106. {
  107. puts ("mutex_lock failed");
  108. return 1;
  109. }
  110. if (pthread_mutex_lock (&m) != 0)
  111. {
  112. puts ("2nd mutex_lock failed");
  113. return 1;
  114. }
  115. if (pthread_mutex_trylock (&m) != 0)
  116. {
  117. puts ("1st trylock failed");
  118. return 1;
  119. }
  120. if (pthread_mutex_unlock (&m) != 0)
  121. {
  122. puts ("mutex_unlock failed");
  123. return 1;
  124. }
  125. if (pthread_mutex_unlock (&m) != 0)
  126. {
  127. puts ("2nd mutex_unlock failed");
  128. return 1;
  129. }
  130. pthread_t th;
  131. if (pthread_create (&th, NULL, tf, NULL) != 0)
  132. {
  133. puts ("create failed");
  134. return 1;
  135. }
  136. int e = pthread_barrier_wait (&b);
  137. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  138. {
  139. puts ("barrier_wait failed");
  140. return 1;
  141. }
  142. if (pthread_mutex_unlock (&m) != 0)
  143. {
  144. puts ("3rd mutex_unlock failed");
  145. return 1;
  146. }
  147. e = pthread_mutex_unlock (&m);
  148. if (e == 0)
  149. {
  150. puts ("4th mutex_unlock succeeded");
  151. return 1;
  152. }
  153. if (e != EPERM)
  154. {
  155. puts ("4th mutex_unlock didn't return EPERM");
  156. return 1;
  157. }
  158. e = pthread_barrier_wait (&b);
  159. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  160. {
  161. puts ("barrier_wait failed");
  162. return 1;
  163. }
  164. if (pthread_join (th, NULL) != 0)
  165. {
  166. puts ("join failed");
  167. return 1;
  168. }
  169. if (pthread_barrier_destroy (&b) != 0)
  170. {
  171. puts ("barrier_destroy failed");
  172. return 1;
  173. }
  174. if (pthread_mutex_destroy (&m) != 0)
  175. {
  176. puts ("mutex_destroy failed");
  177. return 1;
  178. }
  179. if (pthread_mutexattr_destroy (&a) != 0)
  180. {
  181. puts ("mutexattr_destroy failed");
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. #define TEST_FUNCTION do_test ()
  187. #include "../test-skeleton.c"