tst-attr1.c 6.6 KB

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