attr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Handling of thread attributes */
  15. /* mods for uClibc */
  16. #define __getpagesize getpagesize
  17. #define __sched_get_priority_min sched_get_priority_min
  18. #define __sched_get_priority_max sched_get_priority_max
  19. #define __sched_getscheduler sched_getscheduler
  20. #define __sched_getparam sched_getparam
  21. #include <features.h>
  22. #define __USE_GNU
  23. #include <errno.h>
  24. #include <inttypes.h>
  25. #include <stdio.h>
  26. #include <stdio_ext.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/param.h>
  31. #include <sys/resource.h>
  32. #include "pthread.h"
  33. #include "internals.h"
  34. /* glibc uses strong aliases, we wont bother */
  35. #undef strong_alias
  36. #define strong_alias(sym, alias)
  37. #define __pthread_attr_destroy pthread_attr_destroy
  38. #define __pthread_attr_setdetachstate pthread_attr_setdetachstate
  39. #define __pthread_attr_getdetachstate pthread_attr_getdetachstate
  40. #define __pthread_attr_setschedparam pthread_attr_setschedparam
  41. #define __pthread_attr_getschedparam pthread_attr_getschedparam
  42. #define __pthread_attr_setschedpolicy pthread_attr_setschedpolicy
  43. #define __pthread_attr_getschedpolicy pthread_attr_getschedpolicy
  44. #define __pthread_attr_setinheritsched pthread_attr_setinheritsched
  45. #define __pthread_attr_getinheritsched pthread_attr_getinheritsched
  46. #define __pthread_attr_setscope pthread_attr_setscope
  47. #define __pthread_attr_getscope pthread_attr_getscope
  48. /* NOTE: With uClibc I don't think we need this versioning stuff.
  49. * Therefore, define the function pthread_attr_init() here using
  50. * a strong symbol. */
  51. #define __pthread_attr_init_2_1 pthread_attr_init
  52. int __pthread_attr_init_2_1(pthread_attr_t *attr)
  53. {
  54. size_t ps = __getpagesize ();
  55. attr->__detachstate = PTHREAD_CREATE_JOINABLE;
  56. attr->__schedpolicy = SCHED_OTHER;
  57. attr->__schedparam.sched_priority = 0;
  58. attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
  59. attr->__scope = PTHREAD_SCOPE_SYSTEM;
  60. #ifdef NEED_SEPARATE_REGISTER_STACK
  61. attr->__guardsize = ps + ps;
  62. #else
  63. attr->__guardsize = ps;
  64. #endif
  65. attr->__stackaddr = NULL;
  66. attr->__stackaddr_set = 0;
  67. attr->__stacksize = STACK_SIZE - ps;
  68. return 0;
  69. }
  70. /* uClibc: leave out this for now. */
  71. #if 0
  72. versioned_symbol (libpthread, __pthread_attr_init_2_1, pthread_attr_init,
  73. GLIBC_2_1);
  74. #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
  75. int __pthread_attr_init_2_0(pthread_attr_t *attr)
  76. {
  77. attr->__detachstate = PTHREAD_CREATE_JOINABLE;
  78. attr->__schedpolicy = SCHED_OTHER;
  79. attr->__schedparam.sched_priority = 0;
  80. attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
  81. attr->__scope = PTHREAD_SCOPE_SYSTEM;
  82. return 0;
  83. }
  84. compat_symbol (libpthread, __pthread_attr_init_2_0, pthread_attr_init,
  85. GLIBC_2_0);
  86. #endif
  87. #endif
  88. int __pthread_attr_destroy(pthread_attr_t *attr)
  89. {
  90. return 0;
  91. }
  92. strong_alias (__pthread_attr_destroy, pthread_attr_destroy);
  93. int __pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
  94. {
  95. if (detachstate < PTHREAD_CREATE_JOINABLE ||
  96. detachstate > PTHREAD_CREATE_DETACHED)
  97. return EINVAL;
  98. attr->__detachstate = detachstate;
  99. return 0;
  100. }
  101. strong_alias (__pthread_attr_setdetachstate, pthread_attr_setdetachstate);
  102. int __pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
  103. {
  104. *detachstate = attr->__detachstate;
  105. return 0;
  106. }
  107. strong_alias (__pthread_attr_getdetachstate, pthread_attr_getdetachstate);
  108. int __pthread_attr_setschedparam(pthread_attr_t *attr,
  109. const struct sched_param *param)
  110. {
  111. int max_prio = __sched_get_priority_max(attr->__schedpolicy);
  112. int min_prio = __sched_get_priority_min(attr->__schedpolicy);
  113. if (param->sched_priority < min_prio || param->sched_priority > max_prio)
  114. return EINVAL;
  115. memcpy (&attr->__schedparam, param, sizeof (struct sched_param));
  116. return 0;
  117. }
  118. strong_alias (__pthread_attr_setschedparam, pthread_attr_setschedparam);
  119. int __pthread_attr_getschedparam(const pthread_attr_t *attr,
  120. struct sched_param *param)
  121. {
  122. memcpy (param, &attr->__schedparam, sizeof (struct sched_param));
  123. return 0;
  124. }
  125. strong_alias (__pthread_attr_getschedparam, pthread_attr_getschedparam);
  126. int __pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
  127. {
  128. if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
  129. return EINVAL;
  130. attr->__schedpolicy = policy;
  131. return 0;
  132. }
  133. strong_alias (__pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
  134. int __pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
  135. {
  136. *policy = attr->__schedpolicy;
  137. return 0;
  138. }
  139. strong_alias (__pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
  140. int __pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
  141. {
  142. if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
  143. return EINVAL;
  144. attr->__inheritsched = inherit;
  145. return 0;
  146. }
  147. strong_alias (__pthread_attr_setinheritsched, pthread_attr_setinheritsched);
  148. int __pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
  149. {
  150. *inherit = attr->__inheritsched;
  151. return 0;
  152. }
  153. strong_alias (__pthread_attr_getinheritsched, pthread_attr_getinheritsched);
  154. int __pthread_attr_setscope(pthread_attr_t *attr, int scope)
  155. {
  156. switch (scope) {
  157. case PTHREAD_SCOPE_SYSTEM:
  158. attr->__scope = scope;
  159. return 0;
  160. case PTHREAD_SCOPE_PROCESS:
  161. return ENOTSUP;
  162. default:
  163. return EINVAL;
  164. }
  165. }
  166. strong_alias (__pthread_attr_setscope, pthread_attr_setscope);
  167. int __pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
  168. {
  169. *scope = attr->__scope;
  170. return 0;
  171. }
  172. strong_alias (__pthread_attr_getscope, pthread_attr_getscope);
  173. int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
  174. {
  175. /* The guard size must not be larger than the stack itself */
  176. if (guardsize >= attr->__stacksize) return EINVAL;
  177. attr->__guardsize = guardsize;
  178. return 0;
  179. }
  180. weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
  181. int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
  182. {
  183. *guardsize = attr->__guardsize;
  184. return 0;
  185. }
  186. weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
  187. int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
  188. {
  189. attr->__stackaddr = stackaddr;
  190. attr->__stackaddr_set = 1;
  191. return 0;
  192. }
  193. weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
  194. link_warning (pthread_attr_setstackaddr,
  195. "the use of `pthread_attr_setstackaddr' is deprecated, use `pthread_attr_setstack'")
  196. int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
  197. {
  198. /* XXX This function has a stupid definition. The standard specifies
  199. no error value but what is if no stack address was set? We simply
  200. return the value we have in the member. */
  201. *stackaddr = attr->__stackaddr;
  202. return 0;
  203. }
  204. weak_alias (__pthread_attr_getstackaddr, pthread_attr_getstackaddr)
  205. link_warning (pthread_attr_getstackaddr,
  206. "the use of `pthread_attr_getstackaddr' is deprecated, use `pthread_attr_getstack'")
  207. int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
  208. {
  209. #ifdef FLOATING_STACKS
  210. /* We have to check against the maximum allowed stack size. This is no
  211. problem if the manager is already started and we determined it. If
  212. this hasn't happened, we have to find the limit outself. */
  213. if (__pthread_max_stacksize == 0)
  214. __pthread_init_max_stacksize ();
  215. if (stacksize > __pthread_max_stacksize)
  216. return EINVAL;
  217. #else
  218. /* We have a fixed size limit. */
  219. if (stacksize > STACK_SIZE)
  220. return EINVAL;
  221. #endif
  222. /* We don't accept value smaller than PTHREAD_STACK_MIN. */
  223. if (stacksize < PTHREAD_STACK_MIN)
  224. return EINVAL;
  225. attr->__stacksize = stacksize;
  226. return 0;
  227. }
  228. /* Don't bother with this version stuff in uClibc */
  229. #if 1 /*PTHREAD_STACK_MIN == 16384*/
  230. weak_alias (__pthread_attr_setstacksize, pthread_attr_setstacksize)
  231. #else
  232. versioned_symbol (libpthread, __pthread_attr_setstacksize,
  233. pthread_attr_setstacksize, GLIBC_2_3_3);
  234. # if SHLIB_COMPAT(libpthread, GLIBC_2_1, GLIBC_2_3_3)
  235. int __old_pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
  236. {
  237. # ifdef FLOATING_STACKS
  238. /* We have to check against the maximum allowed stack size. This is no
  239. problem if the manager is already started and we determined it. If
  240. this hasn't happened, we have to find the limit outself. */
  241. if (__pthread_max_stacksize == 0)
  242. __pthread_init_max_stacksize ();
  243. if (stacksize > __pthread_max_stacksize)
  244. return EINVAL;
  245. # else
  246. /* We have a fixed size limit. */
  247. if (stacksize > STACK_SIZE)
  248. return EINVAL;
  249. # endif
  250. /* We don't accept value smaller than old PTHREAD_STACK_MIN. */
  251. if (stacksize < 16384)
  252. return EINVAL;
  253. attr->__stacksize = stacksize;
  254. return 0;
  255. }
  256. compat_symbol (libpthread, __old_pthread_attr_setstacksize,
  257. pthread_attr_setstacksize, GLIBC_2_1);
  258. # endif
  259. #endif
  260. int __pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
  261. {
  262. *stacksize = attr->__stacksize;
  263. return 0;
  264. }
  265. weak_alias (__pthread_attr_getstacksize, pthread_attr_getstacksize)
  266. int __pthread_attr_setstack (pthread_attr_t *attr, void *stackaddr,
  267. size_t stacksize)
  268. {
  269. int err;
  270. if ((((uintptr_t) stackaddr)
  271. & (__alignof__ (struct _pthread_descr_struct) - 1)) != 0)
  272. err = EINVAL;
  273. else
  274. err = __pthread_attr_setstacksize (attr, stacksize);
  275. if (err == 0)
  276. {
  277. #ifndef _STACK_GROWS_UP
  278. attr->__stackaddr = (char *) stackaddr + stacksize;
  279. #else
  280. attr->__stackaddr = stackaddr;
  281. #endif
  282. attr->__stackaddr_set = 1;
  283. }
  284. return err;
  285. }
  286. /* Don't bother with this version stuff in uClibc */
  287. #if 1 /*PTHREAD_STACK_MIN == 16384*/
  288. weak_alias (__pthread_attr_setstack, pthread_attr_setstack)
  289. #else
  290. versioned_symbol (libpthread, __pthread_attr_setstack, pthread_attr_setstack,
  291. GLIBC_2_3_3);
  292. # if SHLIB_COMPAT(libpthread, GLIBC_2_2, GLIBC_2_3_3)
  293. int __old_pthread_attr_setstack (pthread_attr_t *attr, void *stackaddr,
  294. size_t stacksize)
  295. {
  296. int err;
  297. if ((((uintptr_t) stackaddr)
  298. & (__alignof__ (struct _pthread_descr_struct) - 1)) != 0)
  299. err = EINVAL;
  300. else
  301. err = __old_pthread_attr_setstacksize (attr, stacksize);
  302. if (err == 0)
  303. {
  304. # ifndef _STACK_GROWS_UP
  305. attr->__stackaddr = (char *) stackaddr + stacksize;
  306. # else
  307. attr->__stackaddr = stackaddr;
  308. # endif
  309. attr->__stackaddr_set = 1;
  310. }
  311. return err;
  312. }
  313. compat_symbol (libpthread, __old_pthread_attr_setstack, pthread_attr_setstack,
  314. GLIBC_2_2);
  315. # endif
  316. #endif
  317. int __pthread_attr_getstack (const pthread_attr_t *attr, void **stackaddr,
  318. size_t *stacksize)
  319. {
  320. /* XXX This function has a stupid definition. The standard specifies
  321. no error value but what is if no stack address was set? We simply
  322. return the value we have in the member. */
  323. #ifndef _STACK_GROWS_UP
  324. *stackaddr = (char *) attr->__stackaddr - attr->__stacksize;
  325. #else
  326. *stackaddr = attr->__stackaddr;
  327. #endif
  328. *stacksize = attr->__stacksize;
  329. return 0;
  330. }
  331. weak_alias (__pthread_attr_getstack, pthread_attr_getstack)
  332. /* can't fully support this just yet */
  333. #if 0
  334. int pthread_getattr_np (pthread_t thread, pthread_attr_t *attr)
  335. {
  336. pthread_handle handle = thread_handle (thread);
  337. pthread_descr descr;
  338. int ret = 0;
  339. if (handle == NULL)
  340. return ENOENT;
  341. descr = handle->h_descr;
  342. attr->__detachstate = (descr->p_detached
  343. ? PTHREAD_CREATE_DETACHED
  344. : PTHREAD_CREATE_JOINABLE);
  345. attr->__schedpolicy = __sched_getscheduler (descr->p_pid);
  346. if (attr->__schedpolicy == -1)
  347. return errno;
  348. if (__sched_getparam (descr->p_pid,
  349. (struct sched_param *) &attr->__schedparam) != 0)
  350. return errno;
  351. attr->__inheritsched = descr->p_inheritsched;
  352. attr->__scope = PTHREAD_SCOPE_SYSTEM;
  353. #ifdef _STACK_GROWS_DOWN
  354. # ifdef USE_TLS
  355. attr->__stacksize = descr->p_stackaddr - (char *)descr->p_guardaddr
  356. - descr->p_guardsize;
  357. # else
  358. attr->__stacksize = (char *)(descr + 1) - (char *)descr->p_guardaddr
  359. - descr->p_guardsize;
  360. # endif
  361. #else
  362. # ifdef USE_TLS
  363. attr->__stacksize = (char *)descr->p_guardaddr - descr->p_stackaddr;
  364. # else
  365. attr->__stacksize = (char *)descr->p_guardaddr - (char *)descr;
  366. # endif
  367. #endif
  368. attr->__guardsize = descr->p_guardsize;
  369. attr->__stackaddr_set = descr->p_userstack;
  370. #ifdef NEED_SEPARATE_REGISTER_STACK
  371. if (descr->p_userstack == 0)
  372. attr->__stacksize *= 2;
  373. /* XXX This is awkward. The guard pages are in the middle of the
  374. two stacks. We must count the guard size in the stack size since
  375. otherwise the range of the stack area cannot be computed. */
  376. attr->__stacksize += attr->__guardsize;
  377. #endif
  378. #ifdef USE_TLS
  379. attr->__stackaddr = descr->p_stackaddr;
  380. #else
  381. # ifndef _STACK_GROWS_UP
  382. attr->__stackaddr = (char *)(descr + 1);
  383. # else
  384. attr->__stackaddr = (char *)descr;
  385. # endif
  386. #endif
  387. #ifdef USE_TLS
  388. if (attr->__stackaddr == NULL)
  389. #else
  390. if (descr == &__pthread_initial_thread)
  391. #endif
  392. {
  393. /* Stack size limit. */
  394. struct rlimit rl;
  395. /* The safest way to get the top of the stack is to read
  396. /proc/self/maps and locate the line into which
  397. __libc_stack_end falls. */
  398. FILE *fp = fopen ("/proc/self/maps", "rc");
  399. if (fp == NULL)
  400. ret = errno;
  401. /* We need the limit of the stack in any case. */
  402. else if (getrlimit (RLIMIT_STACK, &rl) != 0)
  403. ret = errno;
  404. else
  405. {
  406. /* We need no locking. */
  407. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  408. /* Until we found an entry (which should always be the case)
  409. mark the result as a failure. */
  410. ret = ENOENT;
  411. char *line = NULL;
  412. size_t linelen = 0;
  413. uintptr_t last_to = 0;
  414. while (! feof_unlocked (fp))
  415. {
  416. if (__getdelim (&line, &linelen, '\n', fp) <= 0)
  417. break;
  418. uintptr_t from;
  419. uintptr_t to;
  420. if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
  421. continue;
  422. if (from <= (uintptr_t) __libc_stack_end
  423. && (uintptr_t) __libc_stack_end < to)
  424. {
  425. /* Found the entry. Now we have the info we need. */
  426. attr->__stacksize = rl.rlim_cur;
  427. #ifdef _STACK_GROWS_UP
  428. /* Don't check to enforce a limit on the __stacksize */
  429. attr->__stackaddr = (void *) from;
  430. #else
  431. attr->__stackaddr = (void *) to;
  432. /* The limit might be too high. */
  433. if ((size_t) attr->__stacksize
  434. > (size_t) attr->__stackaddr - last_to)
  435. attr->__stacksize = (size_t) attr->__stackaddr - last_to;
  436. #endif
  437. /* We succeed and no need to look further. */
  438. ret = 0;
  439. break;
  440. }
  441. last_to = to;
  442. }
  443. fclose (fp);
  444. free (line);
  445. }
  446. }
  447. return 0;
  448. }
  449. #endif