tst-attr3.c 9.1 KB

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