1
0

pthread_attr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * File : pthread_attr.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-10-26 Bernard the first version
  23. */
  24. #include <rtthread.h>
  25. #include "pthread.h"
  26. #include "sched.h"
  27. #include <string.h>
  28. #define DEFAULT_STACK_SIZE 2048
  29. #define DEFAULT_PRIORITY (RT_THREAD_PRIORITY_MAX/2 + RT_THREAD_PRIORITY_MAX/4)
  30. const pthread_attr_t pthread_default_attr =
  31. {
  32. 0, /* stack base */
  33. DEFAULT_STACK_SIZE, /* stack size */
  34. DEFAULT_PRIORITY, /* priority */
  35. PTHREAD_CREATE_JOINABLE, /* detach state */
  36. SCHED_FIFO, /* scheduler policy */
  37. PTHREAD_INHERIT_SCHED /* Inherit parent prio/policy */
  38. };
  39. int pthread_attr_init(pthread_attr_t *attr)
  40. {
  41. RT_ASSERT(attr != RT_NULL);
  42. *attr = pthread_default_attr;
  43. return 0;
  44. }
  45. RTM_EXPORT(pthread_attr_init);
  46. int pthread_attr_destroy(pthread_attr_t *attr)
  47. {
  48. RT_ASSERT(attr != RT_NULL);
  49. memset(attr, 0, sizeof(pthread_attr_t));
  50. return 0;
  51. }
  52. RTM_EXPORT(pthread_attr_destroy);
  53. int pthread_attr_setdetachstate(pthread_attr_t *attr, int state)
  54. {
  55. RT_ASSERT(attr != RT_NULL);
  56. if (state != PTHREAD_CREATE_JOINABLE && state != PTHREAD_CREATE_DETACHED)
  57. return EINVAL;
  58. attr->detachstate = state;
  59. return 0;
  60. }
  61. RTM_EXPORT(pthread_attr_setdetachstate);
  62. int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state)
  63. {
  64. RT_ASSERT(attr != RT_NULL);
  65. *state = (int)attr->detachstate;
  66. return 0;
  67. }
  68. RTM_EXPORT(pthread_attr_getdetachstate);
  69. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
  70. {
  71. RT_ASSERT(attr != RT_NULL);
  72. attr->policy = policy;
  73. return 0;
  74. }
  75. RTM_EXPORT(pthread_attr_setschedpolicy);
  76. int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
  77. {
  78. RT_ASSERT(attr != RT_NULL);
  79. *policy = (int)attr->policy;
  80. return 0;
  81. }
  82. RTM_EXPORT(pthread_attr_getschedpolicy);
  83. int pthread_attr_setschedparam(pthread_attr_t *attr,
  84. struct sched_param const *param)
  85. {
  86. RT_ASSERT(attr != RT_NULL);
  87. RT_ASSERT(param != RT_NULL);
  88. attr->priority = param->sched_priority;
  89. return 0;
  90. }
  91. RTM_EXPORT(pthread_attr_setschedparam);
  92. int pthread_attr_getschedparam(pthread_attr_t const *attr,
  93. struct sched_param *param)
  94. {
  95. RT_ASSERT(attr != RT_NULL);
  96. RT_ASSERT(param != RT_NULL);
  97. param->sched_priority = attr->priority;
  98. return 0;
  99. }
  100. RTM_EXPORT(pthread_attr_getschedparam);
  101. int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
  102. {
  103. RT_ASSERT(attr != RT_NULL);
  104. attr->stack_size = stack_size;
  105. return 0;
  106. }
  107. RTM_EXPORT(pthread_attr_setstacksize);
  108. int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
  109. {
  110. RT_ASSERT(attr != RT_NULL);
  111. *stack_size = attr->stack_size;
  112. return 0;
  113. }
  114. RTM_EXPORT(pthread_attr_getstacksize);
  115. int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr)
  116. {
  117. RT_ASSERT(attr != RT_NULL);
  118. return EOPNOTSUPP;
  119. }
  120. RTM_EXPORT(pthread_attr_setstackaddr);
  121. int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr)
  122. {
  123. RT_ASSERT(attr != RT_NULL);
  124. return EOPNOTSUPP;
  125. }
  126. RTM_EXPORT(pthread_attr_getstackaddr);
  127. int pthread_attr_setstack(pthread_attr_t *attr,
  128. void *stack_base,
  129. size_t stack_size)
  130. {
  131. RT_ASSERT(attr != RT_NULL);
  132. attr->stack_base = stack_base;
  133. attr->stack_size = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
  134. return 0;
  135. }
  136. RTM_EXPORT(pthread_attr_setstack);
  137. int pthread_attr_getstack(pthread_attr_t const *attr,
  138. void **stack_base,
  139. size_t *stack_size)
  140. {
  141. RT_ASSERT(attr != RT_NULL);
  142. *stack_base = attr->stack_base;
  143. *stack_size = attr->stack_size;
  144. return 0;
  145. }
  146. RTM_EXPORT(pthread_attr_getstack);
  147. int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard_size)
  148. {
  149. return EOPNOTSUPP;
  150. }
  151. int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size)
  152. {
  153. return EOPNOTSUPP;
  154. }
  155. RTM_EXPORT(pthread_attr_getguardsize);
  156. int pthread_attr_setscope(pthread_attr_t *attr, int scope)
  157. {
  158. if (scope == PTHREAD_SCOPE_SYSTEM)
  159. return 0;
  160. if (scope == PTHREAD_SCOPE_PROCESS)
  161. return EOPNOTSUPP;
  162. return EINVAL;
  163. }
  164. RTM_EXPORT(pthread_attr_setscope);
  165. int pthread_attr_getscope(pthread_attr_t const *attr)
  166. {
  167. return PTHREAD_SCOPE_SYSTEM;
  168. }
  169. RTM_EXPORT(pthread_attr_getscope);