tst-attr2.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. pthread_attr_t a;
  25. if (pthread_attr_init (&a) != 0)
  26. {
  27. puts ("attr_init failed");
  28. exit (1);
  29. }
  30. /* Check default value of detach state. */
  31. int s;
  32. if (pthread_attr_getdetachstate (&a, &s) != 0)
  33. {
  34. puts ("1st attr_getdestachstate failed");
  35. exit (1);
  36. }
  37. if (s != PTHREAD_CREATE_JOINABLE)
  38. {
  39. printf ("\
  40. default detach state wrong: %d, expected %d (PTHREAD_CREATE_JOINABLE)\n",
  41. s, PTHREAD_CREATE_JOINABLE);
  42. exit (1);
  43. }
  44. int e = pthread_attr_setdetachstate (&a, PTHREAD_CREATE_DETACHED);
  45. if (e != 0)
  46. {
  47. puts ("1st attr_setdetachstate failed");
  48. exit (1);
  49. }
  50. if (pthread_attr_getdetachstate (&a, &s) != 0)
  51. {
  52. puts ("2nd attr_getdestachstate failed");
  53. exit (1);
  54. }
  55. if (s != PTHREAD_CREATE_DETACHED)
  56. {
  57. puts ("PTHREAD_CREATE_DETACHED set, but not given back");
  58. exit (1);
  59. }
  60. e = pthread_attr_setdetachstate (&a, PTHREAD_CREATE_JOINABLE);
  61. if (e != 0)
  62. {
  63. puts ("2nd attr_setdetachstate failed");
  64. exit (1);
  65. }
  66. if (pthread_attr_getdetachstate (&a, &s) != 0)
  67. {
  68. puts ("3rd attr_getdestachstate failed");
  69. exit (1);
  70. }
  71. if (s != PTHREAD_CREATE_JOINABLE)
  72. {
  73. puts ("PTHREAD_CREATE_JOINABLE set, but not given back");
  74. exit (1);
  75. }
  76. size_t g;
  77. if (pthread_attr_getguardsize (&a, &g) != 0)
  78. {
  79. puts ("1st attr_getguardsize failed");
  80. exit (1);
  81. }
  82. if (g != (size_t) sysconf (_SC_PAGESIZE))
  83. {
  84. printf ("default guardsize %zu, expected %ld (PAGESIZE)\n",
  85. g, sysconf (_SC_PAGESIZE));
  86. exit (1);
  87. }
  88. e = pthread_attr_setguardsize (&a, 0);
  89. if (e != 0)
  90. {
  91. puts ("1st attr_setguardsize failed");
  92. exit (1);
  93. }
  94. if (pthread_attr_getguardsize (&a, &g) != 0)
  95. {
  96. puts ("2nd attr_getguardsize failed");
  97. exit (1);
  98. }
  99. if (g != 0)
  100. {
  101. printf ("guardsize set to zero but %zu returned\n", g);
  102. exit (1);
  103. }
  104. e = pthread_attr_setguardsize (&a, 1);
  105. if (e != 0)
  106. {
  107. puts ("2nd attr_setguardsize failed");
  108. exit (1);
  109. }
  110. if (pthread_attr_getguardsize (&a, &g) != 0)
  111. {
  112. puts ("3rd attr_getguardsize failed");
  113. exit (1);
  114. }
  115. if (g != 1)
  116. {
  117. printf ("guardsize set to 1 but %zu returned\n", g);
  118. exit (1);
  119. }
  120. if (pthread_attr_getinheritsched (&a, &s) != 0)
  121. {
  122. puts ("1st attr_getinheritsched failed");
  123. exit (1);
  124. }
  125. /* XXX What is the correct default value. */
  126. if (s != PTHREAD_INHERIT_SCHED && s != PTHREAD_EXPLICIT_SCHED)
  127. {
  128. puts ("incorrect default value for inheritsched");
  129. exit (1);
  130. }
  131. e = pthread_attr_setinheritsched (&a, PTHREAD_EXPLICIT_SCHED);
  132. if (e != 0)
  133. {
  134. puts ("1st attr_setinheritsched failed");
  135. exit (1);
  136. }
  137. if (pthread_attr_getinheritsched (&a, &s) != 0)
  138. {
  139. puts ("2nd attr_getinheritsched failed");
  140. exit (1);
  141. }
  142. if (s != PTHREAD_EXPLICIT_SCHED)
  143. {
  144. printf ("inheritsched set to PTHREAD_EXPLICIT_SCHED, but got %d\n", s);
  145. exit (1);
  146. }
  147. e = pthread_attr_setinheritsched (&a, PTHREAD_INHERIT_SCHED);
  148. if (e != 0)
  149. {
  150. puts ("2nd attr_setinheritsched failed");
  151. exit (1);
  152. }
  153. if (pthread_attr_getinheritsched (&a, &s) != 0)
  154. {
  155. puts ("3rd attr_getinheritsched failed");
  156. exit (1);
  157. }
  158. if (s != PTHREAD_INHERIT_SCHED)
  159. {
  160. printf ("inheritsched set to PTHREAD_INHERIT_SCHED, but got %d\n", s);
  161. exit (1);
  162. }
  163. if (pthread_attr_getschedpolicy (&a, &s) != 0)
  164. {
  165. puts ("1st attr_getschedpolicy failed");
  166. exit (1);
  167. }
  168. /* XXX What is the correct default value. */
  169. if (s != SCHED_OTHER && s != SCHED_FIFO && s != SCHED_RR)
  170. {
  171. puts ("incorrect default value for schedpolicy");
  172. exit (1);
  173. }
  174. e = pthread_attr_setschedpolicy (&a, SCHED_RR);
  175. if (e != 0)
  176. {
  177. puts ("1st attr_setschedpolicy failed");
  178. exit (1);
  179. }
  180. if (pthread_attr_getschedpolicy (&a, &s) != 0)
  181. {
  182. puts ("2nd attr_getschedpolicy failed");
  183. exit (1);
  184. }
  185. if (s != SCHED_RR)
  186. {
  187. printf ("schedpolicy set to SCHED_RR, but got %d\n", s);
  188. exit (1);
  189. }
  190. e = pthread_attr_setschedpolicy (&a, SCHED_FIFO);
  191. if (e != 0)
  192. {
  193. puts ("2nd attr_setschedpolicy failed");
  194. exit (1);
  195. }
  196. if (pthread_attr_getschedpolicy (&a, &s) != 0)
  197. {
  198. puts ("3rd attr_getschedpolicy failed");
  199. exit (1);
  200. }
  201. if (s != SCHED_FIFO)
  202. {
  203. printf ("schedpolicy set to SCHED_FIFO, but got %d\n", s);
  204. exit (1);
  205. }
  206. e = pthread_attr_setschedpolicy (&a, SCHED_OTHER);
  207. if (e != 0)
  208. {
  209. puts ("3rd attr_setschedpolicy failed");
  210. exit (1);
  211. }
  212. if (pthread_attr_getschedpolicy (&a, &s) != 0)
  213. {
  214. puts ("4th attr_getschedpolicy failed");
  215. exit (1);
  216. }
  217. if (s != SCHED_OTHER)
  218. {
  219. printf ("schedpolicy set to SCHED_OTHER, but got %d\n", s);
  220. exit (1);
  221. }
  222. if (pthread_attr_getscope (&a, &s) != 0)
  223. {
  224. puts ("1st attr_getscope failed");
  225. exit (1);
  226. }
  227. /* XXX What is the correct default value. */
  228. if (s != PTHREAD_SCOPE_SYSTEM && s != PTHREAD_SCOPE_PROCESS)
  229. {
  230. puts ("incorrect default value for contentionscope");
  231. exit (1);
  232. }
  233. e = pthread_attr_setscope (&a, PTHREAD_SCOPE_PROCESS);
  234. if (e != ENOTSUP)
  235. {
  236. if (e != 0)
  237. {
  238. puts ("1st attr_setscope failed");
  239. exit (1);
  240. }
  241. if (pthread_attr_getscope (&a, &s) != 0)
  242. {
  243. puts ("2nd attr_getscope failed");
  244. exit (1);
  245. }
  246. if (s != PTHREAD_SCOPE_PROCESS)
  247. {
  248. printf ("\
  249. contentionscope set to PTHREAD_SCOPE_PROCESS, but got %d\n", s);
  250. exit (1);
  251. }
  252. }
  253. e = pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
  254. if (e != 0)
  255. {
  256. puts ("2nd attr_setscope failed");
  257. exit (1);
  258. }
  259. if (pthread_attr_getscope (&a, &s) != 0)
  260. {
  261. puts ("3rd attr_getscope failed");
  262. exit (1);
  263. }
  264. if (s != PTHREAD_SCOPE_SYSTEM)
  265. {
  266. printf ("contentionscope set to PTHREAD_SCOPE_SYSTEM, but got %d\n", s);
  267. exit (1);
  268. }
  269. char buf[1];
  270. e = pthread_attr_setstack (&a, buf, 1);
  271. if (e != EINVAL)
  272. {
  273. puts ("setstack with size 1 did not produce EINVAL");
  274. exit (1);
  275. }
  276. e = pthread_attr_setstacksize (&a, 1);
  277. if (e != EINVAL)
  278. {
  279. puts ("setstacksize with size 1 did not produce EINVAL");
  280. exit (1);
  281. }
  282. return 0;
  283. }
  284. #define TEST_FUNCTION do_test ()
  285. #include "../test-skeleton.c"