tst-attr1.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. int
  21. do_test (void)
  22. {
  23. int i;
  24. pthread_attr_t a;
  25. if (pthread_attr_init (&a) != 0)
  26. {
  27. puts ("attr_init failed");
  28. exit (1);
  29. }
  30. pthread_mutexattr_t ma;
  31. if (pthread_mutexattr_init (&ma) != 0)
  32. {
  33. puts ("mutexattr_init failed");
  34. exit (1);
  35. }
  36. pthread_rwlockattr_t rwa;
  37. if (pthread_rwlockattr_init (&rwa) != 0)
  38. {
  39. puts ("rwlockattr_init failed");
  40. exit (1);
  41. }
  42. /* XXX Remove if default value is clear. */
  43. pthread_attr_setinheritsched (&a, PTHREAD_INHERIT_SCHED);
  44. pthread_attr_setschedpolicy (&a, SCHED_OTHER);
  45. pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
  46. for (i = 0; i < 10000; ++i)
  47. {
  48. long int r = random ();
  49. if (r != PTHREAD_CREATE_DETACHED && r != PTHREAD_CREATE_JOINABLE)
  50. {
  51. int e = pthread_attr_setdetachstate (&a, r);
  52. if (e == 0)
  53. {
  54. printf ("attr_setdetachstate with value %ld succeeded\n", r);
  55. exit (1);
  56. }
  57. if (e != EINVAL)
  58. {
  59. puts ("attr_setdetachstate didn't return EINVAL");
  60. exit (1);
  61. }
  62. int s;
  63. if (pthread_attr_getdetachstate (&a, &s) != 0)
  64. {
  65. puts ("attr_getdetachstate failed");
  66. exit (1);
  67. }
  68. if (s != PTHREAD_CREATE_JOINABLE)
  69. {
  70. printf ("\
  71. detach state changed to %d by invalid setdetachstate call\n", s);
  72. exit (1);
  73. }
  74. }
  75. if (r != PTHREAD_INHERIT_SCHED && r != PTHREAD_EXPLICIT_SCHED)
  76. {
  77. int e = pthread_attr_setinheritsched (&a, r);
  78. if (e == 0)
  79. {
  80. printf ("attr_setinheritsched with value %ld succeeded\n", r);
  81. exit (1);
  82. }
  83. if (e != EINVAL)
  84. {
  85. puts ("attr_setinheritsched didn't return EINVAL");
  86. exit (1);
  87. }
  88. int s;
  89. if (pthread_attr_getinheritsched (&a, &s) != 0)
  90. {
  91. puts ("attr_getinheritsched failed");
  92. exit (1);
  93. }
  94. if (s != PTHREAD_INHERIT_SCHED)
  95. {
  96. printf ("\
  97. inheritsched changed to %d by invalid setinheritsched call\n", s);
  98. exit (1);
  99. }
  100. }
  101. if (r != SCHED_OTHER && r != SCHED_RR && r != SCHED_FIFO)
  102. {
  103. int e = pthread_attr_setschedpolicy (&a, r);
  104. if (e == 0)
  105. {
  106. printf ("attr_setschedpolicy with value %ld succeeded\n", r);
  107. exit (1);
  108. }
  109. if (e != EINVAL)
  110. {
  111. puts ("attr_setschedpolicy didn't return EINVAL");
  112. exit (1);
  113. }
  114. int s;
  115. if (pthread_attr_getschedpolicy (&a, &s) != 0)
  116. {
  117. puts ("attr_getschedpolicy failed");
  118. exit (1);
  119. }
  120. if (s != SCHED_OTHER)
  121. {
  122. printf ("\
  123. schedpolicy changed to %d by invalid setschedpolicy call\n", s);
  124. exit (1);
  125. }
  126. }
  127. if (r != PTHREAD_SCOPE_SYSTEM && r != PTHREAD_SCOPE_PROCESS)
  128. {
  129. int e = pthread_attr_setscope (&a, r);
  130. if (e == 0)
  131. {
  132. printf ("attr_setscope with value %ld succeeded\n", r);
  133. exit (1);
  134. }
  135. if (e != EINVAL)
  136. {
  137. puts ("attr_setscope didn't return EINVAL");
  138. exit (1);
  139. }
  140. int s;
  141. if (pthread_attr_getscope (&a, &s) != 0)
  142. {
  143. puts ("attr_getscope failed");
  144. exit (1);
  145. }
  146. if (s != PTHREAD_SCOPE_SYSTEM)
  147. {
  148. printf ("\
  149. contentionscope changed to %d by invalid setscope call\n", s);
  150. exit (1);
  151. }
  152. }
  153. if (r != PTHREAD_PROCESS_PRIVATE && r != PTHREAD_PROCESS_SHARED)
  154. {
  155. int e = pthread_mutexattr_setpshared (&ma, r);
  156. if (e == 0)
  157. {
  158. printf ("mutexattr_setpshared with value %ld succeeded\n", r);
  159. exit (1);
  160. }
  161. if (e != EINVAL)
  162. {
  163. puts ("mutexattr_setpshared didn't return EINVAL");
  164. exit (1);
  165. }
  166. int s;
  167. if (pthread_mutexattr_getpshared (&ma, &s) != 0)
  168. {
  169. puts ("mutexattr_getpshared failed");
  170. exit (1);
  171. }
  172. if (s != PTHREAD_PROCESS_PRIVATE)
  173. {
  174. printf ("\
  175. pshared changed to %d by invalid mutexattr_setpshared call\n", s);
  176. exit (1);
  177. }
  178. e = pthread_rwlockattr_setpshared (&rwa, r);
  179. if (e == 0)
  180. {
  181. printf ("rwlockattr_setpshared with value %ld succeeded\n", r);
  182. exit (1);
  183. }
  184. if (e != EINVAL)
  185. {
  186. puts ("rwlockattr_setpshared didn't return EINVAL");
  187. exit (1);
  188. }
  189. if (pthread_rwlockattr_getpshared (&rwa, &s) != 0)
  190. {
  191. puts ("rwlockattr_getpshared failed");
  192. exit (1);
  193. }
  194. if (s != PTHREAD_PROCESS_PRIVATE)
  195. {
  196. printf ("\
  197. pshared changed to %d by invalid rwlockattr_setpshared call\n", s);
  198. exit (1);
  199. }
  200. }
  201. if (r != PTHREAD_CANCEL_ENABLE && r != PTHREAD_CANCEL_DISABLE)
  202. {
  203. int e = pthread_setcancelstate (r, NULL);
  204. if (e == 0)
  205. {
  206. printf ("setcancelstate with value %ld succeeded\n", r);
  207. exit (1);
  208. }
  209. if (e != EINVAL)
  210. {
  211. puts ("setcancelstate didn't return EINVAL");
  212. exit (1);
  213. }
  214. int s;
  215. if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, &s) != 0)
  216. {
  217. puts ("setcancelstate failed for PTHREAD_CANCEL_ENABLE");
  218. exit (1);
  219. }
  220. if (s != PTHREAD_CANCEL_ENABLE)
  221. {
  222. puts ("invalid setcancelstate changed state");
  223. exit (1);
  224. }
  225. }
  226. if (r != PTHREAD_CANCEL_DEFERRED && r != PTHREAD_CANCEL_ASYNCHRONOUS)
  227. {
  228. int e = pthread_setcanceltype (r, NULL);
  229. if (e == 0)
  230. {
  231. printf ("setcanceltype with value %ld succeeded\n", r);
  232. exit (1);
  233. }
  234. if (e != EINVAL)
  235. {
  236. puts ("setcanceltype didn't return EINVAL");
  237. exit (1);
  238. }
  239. int s;
  240. if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &s) != 0)
  241. {
  242. puts ("setcanceltype failed for PTHREAD_CANCEL_DEFERRED");
  243. exit (1);
  244. }
  245. if (s != PTHREAD_CANCEL_DEFERRED)
  246. {
  247. puts ("invalid setcanceltype changed state");
  248. exit (1);
  249. }
  250. }
  251. }
  252. return 0;
  253. }
  254. #define TEST_FUNCTION do_test ()
  255. #include "../test-skeleton.c"