attr.c 6.3 KB

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