tst-mutex8.c 7.4 KB

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