tst-attr3.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* pthread_getattr_np test.
  2. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <errno.h>
  18. #include <error.h>
  19. #include <pthread.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. static void *
  25. tf (void *arg)
  26. {
  27. pthread_attr_t a, *ap, a2;
  28. int err;
  29. void *result = NULL;
  30. if (arg == NULL)
  31. {
  32. ap = &a2;
  33. err = pthread_attr_init (ap);
  34. if (err)
  35. {
  36. error (0, err, "pthread_attr_init failed");
  37. return tf;
  38. }
  39. }
  40. else
  41. ap = (pthread_attr_t *) arg;
  42. err = pthread_getattr_np (pthread_self (), &a);
  43. if (err)
  44. {
  45. error (0, err, "pthread_getattr_np failed");
  46. result = tf;
  47. }
  48. int detachstate1, detachstate2;
  49. err = pthread_attr_getdetachstate (&a, &detachstate1);
  50. if (err)
  51. {
  52. error (0, err, "pthread_attr_getdetachstate failed");
  53. result = tf;
  54. }
  55. else
  56. {
  57. err = pthread_attr_getdetachstate (ap, &detachstate2);
  58. if (err)
  59. {
  60. error (0, err, "pthread_attr_getdetachstate failed");
  61. result = tf;
  62. }
  63. else if (detachstate1 != detachstate2)
  64. {
  65. error (0, 0, "detachstate differs %d != %d",
  66. detachstate1, detachstate2);
  67. result = tf;
  68. }
  69. }
  70. void *stackaddr;
  71. size_t stacksize;
  72. err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
  73. if (err)
  74. {
  75. error (0, err, "pthread_attr_getstack failed");
  76. result = tf;
  77. }
  78. else if ((void *) &a < stackaddr
  79. || (void *) &a >= stackaddr + stacksize)
  80. {
  81. error (0, 0, "pthread_attr_getstack returned range does not cover thread's stack");
  82. result = tf;
  83. }
  84. else
  85. printf ("thread stack %p-%p (0x%zx)\n", stackaddr, stackaddr + stacksize,
  86. stacksize);
  87. size_t guardsize1, guardsize2;
  88. err = pthread_attr_getguardsize (&a, &guardsize1);
  89. if (err)
  90. {
  91. error (0, err, "pthread_attr_getguardsize failed");
  92. result = tf;
  93. }
  94. else
  95. {
  96. err = pthread_attr_getguardsize (ap, &guardsize2);
  97. if (err)
  98. {
  99. error (0, err, "pthread_attr_getguardsize failed");
  100. result = tf;
  101. }
  102. else if (guardsize1 != guardsize2)
  103. {
  104. error (0, 0, "guardsize differs %zd != %zd",
  105. guardsize1, guardsize2);
  106. result = tf;
  107. }
  108. else
  109. printf ("thread guardsize %zd\n", guardsize1);
  110. }
  111. int scope1, scope2;
  112. err = pthread_attr_getscope (&a, &scope1);
  113. if (err)
  114. {
  115. error (0, err, "pthread_attr_getscope failed");
  116. result = tf;
  117. }
  118. else
  119. {
  120. err = pthread_attr_getscope (ap, &scope2);
  121. if (err)
  122. {
  123. error (0, err, "pthread_attr_getscope failed");
  124. result = tf;
  125. }
  126. else if (scope1 != scope2)
  127. {
  128. error (0, 0, "scope differs %d != %d",
  129. scope1, scope2);
  130. result = tf;
  131. }
  132. }
  133. int inheritsched1, inheritsched2;
  134. err = pthread_attr_getinheritsched (&a, &inheritsched1);
  135. if (err)
  136. {
  137. error (0, err, "pthread_attr_getinheritsched failed");
  138. result = tf;
  139. }
  140. else
  141. {
  142. err = pthread_attr_getinheritsched (ap, &inheritsched2);
  143. if (err)
  144. {
  145. error (0, err, "pthread_attr_getinheritsched failed");
  146. result = tf;
  147. }
  148. else if (inheritsched1 != inheritsched2)
  149. {
  150. error (0, 0, "inheritsched differs %d != %d",
  151. inheritsched1, inheritsched2);
  152. result = tf;
  153. }
  154. }
  155. cpu_set_t c1, c2;
  156. err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
  157. if (err == 0)
  158. {
  159. err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
  160. if (err)
  161. {
  162. error (0, err, "pthread_attr_getaffinity_np failed");
  163. result = tf;
  164. }
  165. else if (memcmp (&c1, &c2, sizeof (c1)))
  166. {
  167. error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
  168. result = tf;
  169. }
  170. }
  171. err = pthread_attr_destroy (&a);
  172. if (err)
  173. {
  174. error (0, err, "pthread_attr_destroy failed");
  175. result = tf;
  176. }
  177. if (ap == &a2)
  178. {
  179. err = pthread_attr_destroy (ap);
  180. if (err)
  181. {
  182. error (0, err, "pthread_attr_destroy failed");
  183. result = tf;
  184. }
  185. }
  186. return result;
  187. }
  188. static int
  189. do_test (void)
  190. {
  191. int result = 0;
  192. pthread_attr_t a;
  193. cpu_set_t c1, c2;
  194. int err = pthread_attr_init (&a);
  195. if (err)
  196. {
  197. error (0, err, "pthread_attr_init failed");
  198. result = 1;
  199. }
  200. err = pthread_attr_getaffinity_np (&a, sizeof (c1), &c1);
  201. if (err && err != ENOSYS)
  202. {
  203. error (0, err, "pthread_attr_getaffinity_np failed");
  204. result = 1;
  205. }
  206. err = pthread_attr_destroy (&a);
  207. if (err)
  208. {
  209. error (0, err, "pthread_attr_destroy failed");
  210. result = 1;
  211. }
  212. err = pthread_getattr_np (pthread_self (), &a);
  213. if (err)
  214. {
  215. error (0, err, "pthread_getattr_np failed");
  216. result = 1;
  217. }
  218. int detachstate;
  219. err = pthread_attr_getdetachstate (&a, &detachstate);
  220. if (err)
  221. {
  222. error (0, err, "pthread_attr_getdetachstate failed");
  223. result = 1;
  224. }
  225. else if (detachstate != PTHREAD_CREATE_JOINABLE)
  226. {
  227. error (0, 0, "initial thread not joinable");
  228. result = 1;
  229. }
  230. void *stackaddr;
  231. size_t stacksize;
  232. err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
  233. if (err)
  234. {
  235. error (0, err, "pthread_attr_getstack failed");
  236. result = 1;
  237. }
  238. else if ((void *) &a < stackaddr
  239. || (void *) &a >= stackaddr + stacksize)
  240. {
  241. error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
  242. result = 1;
  243. }
  244. else
  245. printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr,
  246. stackaddr + stacksize, stacksize);
  247. size_t guardsize;
  248. err = pthread_attr_getguardsize (&a, &guardsize);
  249. if (err)
  250. {
  251. error (0, err, "pthread_attr_getguardsize failed");
  252. result = 1;
  253. }
  254. else if (guardsize != 0)
  255. {
  256. error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
  257. guardsize);
  258. result = 1;
  259. }
  260. int scope;
  261. err = pthread_attr_getscope (&a, &scope);
  262. if (err)
  263. {
  264. error (0, err, "pthread_attr_getscope failed");
  265. result = 1;
  266. }
  267. else if (scope != PTHREAD_SCOPE_SYSTEM)
  268. {
  269. error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
  270. scope);
  271. result = 1;
  272. }
  273. int inheritsched;
  274. err = pthread_attr_getinheritsched (&a, &inheritsched);
  275. if (err)
  276. {
  277. error (0, err, "pthread_attr_getinheritsched failed");
  278. result = 1;
  279. }
  280. else if (inheritsched != PTHREAD_INHERIT_SCHED)
  281. {
  282. error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
  283. inheritsched);
  284. result = 1;
  285. }
  286. err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
  287. if (err == 0)
  288. {
  289. err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
  290. if (err)
  291. {
  292. error (0, err, "pthread_attr_getaffinity_np failed");
  293. result = 1;
  294. }
  295. else if (memcmp (&c1, &c2, sizeof (c1)))
  296. {
  297. error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
  298. result = 1;
  299. }
  300. }
  301. err = pthread_attr_destroy (&a);
  302. if (err)
  303. {
  304. error (0, err, "pthread_attr_destroy failed");
  305. result = 1;
  306. }
  307. pthread_t th;
  308. err = pthread_create (&th, NULL, tf, NULL);
  309. if (err)
  310. {
  311. error (0, err, "pthread_create #1 failed");
  312. result = 1;
  313. }
  314. else
  315. {
  316. void *ret;
  317. err = pthread_join (th, &ret);
  318. if (err)
  319. {
  320. error (0, err, "pthread_join #1 failed");
  321. result = 1;
  322. }
  323. else if (ret != NULL)
  324. result = 1;
  325. }
  326. err = pthread_attr_init (&a);
  327. if (err)
  328. {
  329. error (0, err, "pthread_attr_init failed");
  330. result = 1;
  331. }
  332. err = pthread_create (&th, &a, tf, &a);
  333. if (err)
  334. {
  335. error (0, err, "pthread_create #2 failed");
  336. result = 1;
  337. }
  338. else
  339. {
  340. void *ret;
  341. err = pthread_join (th, &ret);
  342. if (err)
  343. {
  344. error (0, err, "pthread_join #2 failed");
  345. result = 1;
  346. }
  347. else if (ret != NULL)
  348. result = 1;
  349. }
  350. err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE));
  351. if (err)
  352. {
  353. error (0, err, "pthread_attr_setguardsize failed");
  354. result = 1;
  355. }
  356. err = pthread_create (&th, &a, tf, &a);
  357. if (err)
  358. {
  359. error (0, err, "pthread_create #3 failed");
  360. result = 1;
  361. }
  362. else
  363. {
  364. void *ret;
  365. err = pthread_join (th, &ret);
  366. if (err)
  367. {
  368. error (0, err, "pthread_join #3 failed");
  369. result = 1;
  370. }
  371. else if (ret != NULL)
  372. result = 1;
  373. }
  374. err = pthread_attr_destroy (&a);
  375. if (err)
  376. {
  377. error (0, err, "pthread_attr_destroy failed");
  378. result = 1;
  379. }
  380. return result;
  381. }
  382. #define TEST_FUNCTION do_test ()
  383. #include "../test-skeleton.c"