attr.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /* changed for uClibc */
  15. #define __sched_get_priority_min sched_get_priority_min
  16. #define __sched_get_priority_max sched_get_priority_max
  17. /* Handling of thread attributes */
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/param.h>
  22. #include "pthread.h"
  23. #include "internals.h"
  24. libpthread_hidden_proto(pthread_attr_destroy)
  25. libpthread_hidden_proto(pthread_attr_init)
  26. libpthread_hidden_proto(pthread_attr_getdetachstate)
  27. libpthread_hidden_proto(pthread_attr_setdetachstate)
  28. libpthread_hidden_proto(pthread_attr_getinheritsched)
  29. libpthread_hidden_proto(pthread_attr_setinheritsched)
  30. libpthread_hidden_proto(pthread_attr_setschedparam)
  31. libpthread_hidden_proto(pthread_attr_getschedparam)
  32. libpthread_hidden_proto(pthread_attr_getschedpolicy)
  33. libpthread_hidden_proto(pthread_attr_setschedpolicy)
  34. libpthread_hidden_proto(pthread_attr_getscope)
  35. libpthread_hidden_proto(pthread_attr_setscope)
  36. /* NOTE: With uClibc I don't think we need this versioning stuff.
  37. * Therefore, define the function pthread_attr_init() here using
  38. * a strong symbol. */
  39. /*int __pthread_attr_init_2_1(pthread_attr_t *attr)*/
  40. int pthread_attr_init(pthread_attr_t *attr)
  41. {
  42. size_t ps = getpagesize ();
  43. attr->__detachstate = PTHREAD_CREATE_JOINABLE;
  44. attr->__schedpolicy = SCHED_OTHER;
  45. attr->__schedparam.sched_priority = 0;
  46. attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
  47. attr->__scope = PTHREAD_SCOPE_SYSTEM;
  48. attr->__guardsize = ps;
  49. attr->__stackaddr = NULL;
  50. attr->__stackaddr_set = 0;
  51. attr->__stacksize = STACK_SIZE - ps;
  52. return 0;
  53. }
  54. libpthread_hidden_def(pthread_attr_init)
  55. /* uClibc: leave out this for now. */
  56. #if defined DO_PTHREAD_VERSIONING_WITH_UCLIBC
  57. #if defined __PIC__ && defined DO_VERSIONING
  58. default_symbol_version (__pthread_attr_init_2_1, pthread_attr_init, GLIBC_2.1);
  59. int __pthread_attr_init_2_0(pthread_attr_t *attr)
  60. {
  61. attr->__detachstate = PTHREAD_CREATE_JOINABLE;
  62. attr->__schedpolicy = SCHED_OTHER;
  63. attr->__schedparam.sched_priority = 0;
  64. attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
  65. attr->__scope = PTHREAD_SCOPE_SYSTEM;
  66. return 0;
  67. }
  68. symbol_version (__pthread_attr_init_2_0, pthread_attr_init, GLIBC_2.0);
  69. #else
  70. strong_alias (__pthread_attr_init_2_1, pthread_attr_init)
  71. #endif
  72. #endif /* DO_PTHREAD_VERSIONING_WITH_UCLIBC */
  73. int pthread_attr_destroy(pthread_attr_t *attr attribute_unused)
  74. {
  75. return 0;
  76. }
  77. libpthread_hidden_def(pthread_attr_destroy)
  78. int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
  79. {
  80. if (detachstate < PTHREAD_CREATE_JOINABLE ||
  81. detachstate > PTHREAD_CREATE_DETACHED)
  82. return EINVAL;
  83. attr->__detachstate = detachstate;
  84. return 0;
  85. }
  86. libpthread_hidden_def(pthread_attr_setdetachstate)
  87. int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
  88. {
  89. *detachstate = attr->__detachstate;
  90. return 0;
  91. }
  92. libpthread_hidden_def(pthread_attr_getdetachstate)
  93. int pthread_attr_setschedparam(pthread_attr_t *attr,
  94. const struct sched_param *param)
  95. {
  96. int max_prio = __sched_get_priority_max(attr->__schedpolicy);
  97. int min_prio = __sched_get_priority_min(attr->__schedpolicy);
  98. if (param->sched_priority < min_prio || param->sched_priority > max_prio)
  99. return EINVAL;
  100. memcpy (&attr->__schedparam, param, sizeof (struct sched_param));
  101. return 0;
  102. }
  103. libpthread_hidden_def(pthread_attr_setschedparam)
  104. int pthread_attr_getschedparam(const pthread_attr_t *attr,
  105. struct sched_param *param)
  106. {
  107. memcpy (param, &attr->__schedparam, sizeof (struct sched_param));
  108. return 0;
  109. }
  110. libpthread_hidden_def(pthread_attr_getschedparam)
  111. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
  112. {
  113. if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
  114. return EINVAL;
  115. attr->__schedpolicy = policy;
  116. return 0;
  117. }
  118. libpthread_hidden_def(pthread_attr_setschedpolicy)
  119. int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
  120. {
  121. *policy = attr->__schedpolicy;
  122. return 0;
  123. }
  124. libpthread_hidden_def(pthread_attr_getschedpolicy)
  125. int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
  126. {
  127. if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
  128. return EINVAL;
  129. attr->__inheritsched = inherit;
  130. return 0;
  131. }
  132. libpthread_hidden_def(pthread_attr_setinheritsched)
  133. int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
  134. {
  135. *inherit = attr->__inheritsched;
  136. return 0;
  137. }
  138. libpthread_hidden_def(pthread_attr_getinheritsched)
  139. int pthread_attr_setscope(pthread_attr_t *attr, int scope)
  140. {
  141. switch (scope) {
  142. case PTHREAD_SCOPE_SYSTEM:
  143. attr->__scope = scope;
  144. return 0;
  145. case PTHREAD_SCOPE_PROCESS:
  146. return ENOTSUP;
  147. default:
  148. return EINVAL;
  149. }
  150. }
  151. libpthread_hidden_def(pthread_attr_setscope)
  152. int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
  153. {
  154. *scope = attr->__scope;
  155. return 0;
  156. }
  157. libpthread_hidden_def(pthread_attr_getscope)
  158. int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
  159. {
  160. size_t ps = getpagesize ();
  161. /* First round up the guard size. */
  162. guardsize = roundup (guardsize, ps);
  163. /* The guard size must not be larger than the stack itself */
  164. if (guardsize >= attr->__stacksize) return EINVAL;
  165. attr->__guardsize = guardsize;
  166. return 0;
  167. }
  168. weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
  169. int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
  170. {
  171. *guardsize = attr->__guardsize;
  172. return 0;
  173. }
  174. weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
  175. #if 0 /* uClibc: deprecated stuff disabled */
  176. int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
  177. {
  178. attr->__stackaddr = stackaddr;
  179. attr->__stackaddr_set = 1;
  180. return 0;
  181. }
  182. weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
  183. int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
  184. {
  185. /* XXX This function has a stupid definition. The standard specifies
  186. no error value but what is if no stack address was set? We simply
  187. return the value we have in the member. */
  188. *stackaddr = attr->__stackaddr;
  189. return 0;
  190. }
  191. weak_alias (__pthread_attr_getstackaddr, pthread_attr_getstackaddr)
  192. #endif
  193. int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
  194. {
  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)