tst-attr3.c 9.4 KB

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