attr.c 6.3 KB

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