attr.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include <features.h>
  20. #define __USE_GNU
  21. #include <errno.h>
  22. #include <inttypes.h>
  23. #include <stdio.h>
  24. #include <stdio_ext.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <sys/param.h>
  29. #include <sys/resource.h>
  30. #include "pthread.h"
  31. #include "internals.h"
  32. /* NOTE: With uClibc I don't think we need this versioning stuff.
  33. * Therefore, define the function pthread_attr_init() here using
  34. * a strong symbol. */
  35. /*int __pthread_attr_init_2_1(pthread_attr_t *attr)*/
  36. int pthread_attr_init(pthread_attr_t *attr)
  37. {
  38. size_t ps = __getpagesize ();
  39. attr->__detachstate = PTHREAD_CREATE_JOINABLE;
  40. attr->__schedpolicy = SCHED_OTHER;
  41. attr->__schedparam.sched_priority = 0;
  42. attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
  43. attr->__scope = PTHREAD_SCOPE_SYSTEM;
  44. #ifdef NEED_SEPARATE_REGISTER_STACK
  45. attr->__guardsize = ps + ps;
  46. #else
  47. attr->__guardsize = ps;
  48. #endif
  49. attr->__stackaddr = NULL;
  50. attr->__stackaddr_set = 0;
  51. attr->__stacksize = STACK_SIZE - ps;
  52. return 0;
  53. }
  54. /* uClibc: leave out this for now. */
  55. #if DO_PTHREAD_VERSIONING_WITH_UCLIBC
  56. #if defined __HAVE_ELF__ && defined __PIC__ && defined DO_VERSIONING
  57. default_symbol_version (__pthread_attr_init_2_1, pthread_attr_init, GLIBC_2.1);
  58. int __pthread_attr_init_2_0(pthread_attr_t *attr)
  59. {
  60. attr->__detachstate = PTHREAD_CREATE_JOINABLE;
  61. attr->__schedpolicy = SCHED_OTHER;
  62. attr->__schedparam.sched_priority = 0;
  63. attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
  64. attr->__scope = PTHREAD_SCOPE_SYSTEM;
  65. return 0;
  66. }
  67. symbol_version (__pthread_attr_init_2_0, pthread_attr_init, GLIBC_2.0);
  68. #else
  69. strong_alias (__pthread_attr_init_2_1, pthread_attr_init)
  70. #endif
  71. #endif /* DO_PTHREAD_VERSIONING_WITH_UCLIBC */
  72. int pthread_attr_destroy(pthread_attr_t *attr)
  73. {
  74. return 0;
  75. }
  76. int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
  77. {
  78. if (detachstate < PTHREAD_CREATE_JOINABLE ||
  79. detachstate > PTHREAD_CREATE_DETACHED)
  80. return EINVAL;
  81. attr->__detachstate = detachstate;
  82. return 0;
  83. }
  84. int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
  85. {
  86. *detachstate = attr->__detachstate;
  87. return 0;
  88. }
  89. int pthread_attr_setschedparam(pthread_attr_t *attr,
  90. const struct sched_param *param)
  91. {
  92. int max_prio = __sched_get_priority_max(attr->__schedpolicy);
  93. int min_prio = __sched_get_priority_min(attr->__schedpolicy);
  94. if (param->sched_priority < min_prio || param->sched_priority > max_prio)
  95. return EINVAL;
  96. memcpy (&attr->__schedparam, param, sizeof (struct sched_param));
  97. return 0;
  98. }
  99. int pthread_attr_getschedparam(const pthread_attr_t *attr,
  100. struct sched_param *param)
  101. {
  102. memcpy (param, &attr->__schedparam, sizeof (struct sched_param));
  103. return 0;
  104. }
  105. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
  106. {
  107. if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
  108. return EINVAL;
  109. attr->__schedpolicy = policy;
  110. return 0;
  111. }
  112. int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
  113. {
  114. *policy = attr->__schedpolicy;
  115. return 0;
  116. }
  117. int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
  118. {
  119. if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
  120. return EINVAL;
  121. attr->__inheritsched = inherit;
  122. return 0;
  123. }
  124. int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
  125. {
  126. *inherit = attr->__inheritsched;
  127. return 0;
  128. }
  129. int pthread_attr_setscope(pthread_attr_t *attr, int scope)
  130. {
  131. switch (scope) {
  132. case PTHREAD_SCOPE_SYSTEM:
  133. attr->__scope = scope;
  134. return 0;
  135. case PTHREAD_SCOPE_PROCESS:
  136. return ENOTSUP;
  137. default:
  138. return EINVAL;
  139. }
  140. }
  141. int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
  142. {
  143. *scope = attr->__scope;
  144. return 0;
  145. }
  146. int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
  147. {
  148. /* The guard size must not be larger than the stack itself */
  149. if (guardsize >= attr->__stacksize) return EINVAL;
  150. attr->__guardsize = guardsize;
  151. return 0;
  152. }
  153. weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
  154. int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
  155. {
  156. *guardsize = attr->__guardsize;
  157. return 0;
  158. }
  159. weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
  160. int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
  161. {
  162. attr->__stackaddr = stackaddr;
  163. attr->__stackaddr_set = 1;
  164. return 0;
  165. }
  166. weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
  167. link_warning (pthread_attr_setstackaddr,
  168. "the use of `pthread_attr_setstackaddr' is deprecated, use `pthread_attr_setstack'")
  169. int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
  170. {
  171. /* XXX This function has a stupid definition. The standard specifies
  172. no error value but what is if no stack address was set? We simply
  173. return the value we have in the member. */
  174. *stackaddr = attr->__stackaddr;
  175. return 0;
  176. }
  177. weak_alias (__pthread_attr_getstackaddr, pthread_attr_getstackaddr)
  178. link_warning (pthread_attr_getstackaddr,
  179. "the use of `pthread_attr_getstackaddr' is deprecated, use `pthread_attr_getstack'")
  180. int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
  181. {
  182. #ifdef FLOATING_STACKS
  183. /* We have to check against the maximum allowed stack size. This is no
  184. problem if the manager is already started and we determined it. If
  185. this hasn't happened, we have to find the limit outself. */
  186. if (__pthread_max_stacksize == 0)
  187. __pthread_init_max_stacksize ();
  188. if (stacksize > __pthread_max_stacksize)
  189. return EINVAL;
  190. #else
  191. /* We have a fixed size limit. */
  192. if (stacksize > STACK_SIZE)
  193. return EINVAL;
  194. #endif
  195. /* We don't accept value smaller than PTHREAD_STACK_MIN. */
  196. if (stacksize < PTHREAD_STACK_MIN)
  197. return EINVAL;
  198. attr->__stacksize = stacksize;
  199. return 0;
  200. }
  201. weak_alias (__pthread_attr_setstacksize, pthread_attr_setstacksize)
  202. int __pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
  203. {
  204. *stacksize = attr->__stacksize;
  205. return 0;
  206. }
  207. weak_alias (__pthread_attr_getstacksize, pthread_attr_getstacksize)
  208. int __pthread_attr_setstack (pthread_attr_t *attr, void *stackaddr,
  209. size_t stacksize)
  210. {
  211. int err;
  212. if ((((uintptr_t) stackaddr)
  213. & (__alignof__ (struct _pthread_descr_struct) - 1)) != 0)
  214. err = EINVAL;
  215. else
  216. err = __pthread_attr_setstacksize (attr, stacksize);
  217. if (err == 0)
  218. {
  219. #ifndef _STACK_GROWS_UP
  220. attr->__stackaddr = (char *) stackaddr + stacksize;
  221. #else
  222. attr->__stackaddr = stackaddr;
  223. #endif
  224. attr->__stackaddr_set = 1;
  225. }
  226. return err;
  227. }
  228. int __pthread_attr_getstack (const pthread_attr_t *attr, void **stackaddr,
  229. size_t *stacksize)
  230. {
  231. /* XXX This function has a stupid definition. The standard specifies
  232. no error value but what is if no stack address was set? We simply
  233. return the value we have in the member. */
  234. #ifndef _STACK_GROWS_UP
  235. *stackaddr = (char *) attr->__stackaddr - attr->__stacksize;
  236. #else
  237. *stackaddr = attr->__stackaddr;
  238. #endif
  239. *stacksize = attr->__stacksize;
  240. return 0;
  241. }
  242. weak_alias (__pthread_attr_getstack, pthread_attr_getstack)