tst-mutex8.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /* This test checks behavior not required by POSIX. */
  17. #include <errno.h>
  18. #include <pthread.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. static pthread_mutex_t *m;
  23. static pthread_barrier_t b;
  24. static pthread_cond_t c;
  25. static bool done;
  26. static void
  27. cl (void *arg)
  28. {
  29. if (pthread_mutex_unlock (m) != 0)
  30. {
  31. puts ("cl: mutex_unlocked failed");
  32. exit (1);
  33. }
  34. }
  35. static void *
  36. tf (void *arg)
  37. {
  38. if (pthread_mutex_lock (m) != 0)
  39. {
  40. puts ("tf: mutex_lock failed");
  41. return (void *) 1l;
  42. }
  43. int e = pthread_barrier_wait (&b);
  44. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  45. {
  46. puts ("barrier_wait failed");
  47. return (void *) 1l;
  48. }
  49. if (arg == NULL)
  50. do
  51. if (pthread_cond_wait (&c, m) != 0)
  52. {
  53. puts ("tf: cond_wait failed");
  54. return (void *) 1l;
  55. }
  56. while (! done);
  57. else
  58. do
  59. {
  60. pthread_cleanup_push (cl, NULL);
  61. if (pthread_cond_wait (&c, m) != 0)
  62. {
  63. puts ("tf: cond_wait failed");
  64. return (void *) 1l;
  65. }
  66. pthread_cleanup_pop (0);
  67. }
  68. while (! done);
  69. if (pthread_mutex_unlock (m) != 0)
  70. {
  71. puts ("tf: mutex_unlock failed");
  72. return (void *) 1l;
  73. }
  74. return NULL;
  75. }
  76. static int
  77. check_type (const char *mas, pthread_mutexattr_t *ma)
  78. {
  79. if (pthread_mutex_init (m, ma) != 0)
  80. {
  81. printf ("1st mutex_init failed for %s\n", mas);
  82. return 1;
  83. }
  84. if (pthread_mutex_destroy (m) != 0)
  85. {
  86. printf ("immediate mutex_destroy failed for %s\n", mas);
  87. return 1;
  88. }
  89. if (pthread_mutex_init (m, ma) != 0)
  90. {
  91. printf ("2nd mutex_init failed for %s\n", mas);
  92. return 1;
  93. }
  94. if (pthread_mutex_lock (m) != 0)
  95. {
  96. printf ("1st mutex_lock failed for %s\n", mas);
  97. return 1;
  98. }
  99. int e = pthread_mutex_destroy (m);
  100. if (e == 0)
  101. {
  102. printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas);
  103. return 1;
  104. }
  105. if (e != EBUSY)
  106. {
  107. printf ("mutex_destroy of self-locked mutex did not return EBUSY %s\n",
  108. mas);
  109. return 1;
  110. }
  111. if (pthread_mutex_unlock (m) != 0)
  112. {
  113. printf ("1st mutex_unlock failed for %s\n", mas);
  114. return 1;
  115. }
  116. if (pthread_mutex_trylock (m) != 0)
  117. {
  118. printf ("mutex_trylock failed for %s\n", mas);
  119. return 1;
  120. }
  121. e = pthread_mutex_destroy (m);
  122. if (e == 0)
  123. {
  124. printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", mas);
  125. return 1;
  126. }
  127. if (e != EBUSY)
  128. {
  129. printf ("\
  130. mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
  131. mas);
  132. return 1;
  133. }
  134. if (pthread_mutex_unlock (m) != 0)
  135. {
  136. printf ("2nd mutex_unlock failed for %s\n", mas);
  137. return 1;
  138. }
  139. pthread_t th;
  140. if (pthread_create (&th, NULL, tf, NULL) != 0)
  141. {
  142. puts ("1st create failed");
  143. return 1;
  144. }
  145. done = false;
  146. e = pthread_barrier_wait (&b);
  147. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  148. {
  149. puts ("1st barrier_wait failed");
  150. return 1;
  151. }
  152. if (pthread_mutex_lock (m) != 0)
  153. {
  154. printf ("2nd mutex_lock failed for %s\n", mas);
  155. return 1;
  156. }
  157. if (pthread_mutex_unlock (m) != 0)
  158. {
  159. printf ("3rd mutex_unlock failed for %s\n", mas);
  160. return 1;
  161. }
  162. e = pthread_mutex_destroy (m);
  163. if (e == 0)
  164. {
  165. printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", mas);
  166. return 1;
  167. }
  168. if (e != EBUSY)
  169. {
  170. printf ("\
  171. mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas);
  172. return 1;
  173. }
  174. done = true;
  175. if (pthread_cond_signal (&c) != 0)
  176. {
  177. puts ("cond_signal failed");
  178. return 1;
  179. }
  180. void *r;
  181. if (pthread_join (th, &r) != 0)
  182. {
  183. puts ("join failed");
  184. return 1;
  185. }
  186. if (r != NULL)
  187. {
  188. puts ("thread didn't return NULL");
  189. return 1;
  190. }
  191. if (pthread_mutex_destroy (m) != 0)
  192. {
  193. printf ("mutex_destroy after condvar-use failed for %s\n", mas);
  194. return 1;
  195. }
  196. if (pthread_mutex_init (m, ma) != 0)
  197. {
  198. printf ("3rd mutex_init failed for %s\n", mas);
  199. return 1;
  200. }
  201. if (pthread_create (&th, NULL, tf, (void *) 1) != 0)
  202. {
  203. puts ("2nd create failed");
  204. return 1;
  205. }
  206. done = false;
  207. e = pthread_barrier_wait (&b);
  208. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  209. {
  210. puts ("2nd barrier_wait failed");
  211. return 1;
  212. }
  213. if (pthread_mutex_lock (m) != 0)
  214. {
  215. printf ("3rd mutex_lock failed for %s\n", mas);
  216. return 1;
  217. }
  218. if (pthread_mutex_unlock (m) != 0)
  219. {
  220. printf ("4th mutex_unlock failed for %s\n", mas);
  221. return 1;
  222. }
  223. e = pthread_mutex_destroy (m);
  224. if (e == 0)
  225. {
  226. printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
  227. mas);
  228. return 1;
  229. }
  230. if (e != EBUSY)
  231. {
  232. printf ("\
  233. 2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
  234. mas);
  235. return 1;
  236. }
  237. if (pthread_cancel (th) != 0)
  238. {
  239. puts ("cond_cancel failed");
  240. return 1;
  241. }
  242. if (pthread_join (th, &r) != 0)
  243. {
  244. puts ("join failed");
  245. return 1;
  246. }
  247. if (r != PTHREAD_CANCELED)
  248. {
  249. puts ("thread not canceled");
  250. return 1;
  251. }
  252. if (pthread_mutex_destroy (m) != 0)
  253. {
  254. printf ("mutex_destroy after condvar-canceled failed for %s\n", mas);
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. static int
  260. do_test (void)
  261. {
  262. pthread_mutex_t mm;
  263. m = &mm;
  264. if (pthread_barrier_init (&b, NULL, 2) != 0)
  265. {
  266. puts ("barrier_init failed");
  267. return 1;
  268. }
  269. if (pthread_cond_init (&c, NULL) != 0)
  270. {
  271. puts ("cond_init failed");
  272. return 1;
  273. }
  274. puts ("check normal mutex");
  275. int res = check_type ("normal", NULL);
  276. pthread_mutexattr_t ma;
  277. if (pthread_mutexattr_init (&ma) != 0)
  278. {
  279. puts ("1st mutexattr_init failed");
  280. return 1;
  281. }
  282. if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_RECURSIVE) != 0)
  283. {
  284. puts ("1st mutexattr_settype failed");
  285. return 1;
  286. }
  287. puts ("check recursive mutex");
  288. res |= check_type ("recursive", &ma);
  289. if (pthread_mutexattr_destroy (&ma) != 0)
  290. {
  291. puts ("1st mutexattr_destroy failed");
  292. return 1;
  293. }
  294. if (pthread_mutexattr_init (&ma) != 0)
  295. {
  296. puts ("2nd mutexattr_init failed");
  297. return 1;
  298. }
  299. if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
  300. {
  301. puts ("2nd mutexattr_settype failed");
  302. return 1;
  303. }
  304. puts ("check error-checking mutex");
  305. res |= check_type ("error-checking", &ma);
  306. if (pthread_mutexattr_destroy (&ma) != 0)
  307. {
  308. puts ("2nd mutexattr_destroy failed");
  309. return 1;
  310. }
  311. return res;
  312. }
  313. #define TEST_FUNCTION do_test ()
  314. #include "../test-skeleton.c"