attr.c 13 KB

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