tst-attr2.c 7.2 KB

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