tst-mutex3.c 4.5 KB

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