tst-mutex2.c 4.5 KB

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