pthread.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "pthread.h"
  2. #define PTHREAD_MAGIC 0x70746873
  3. struct _pthread_data
  4. {
  5. rt_uint32_t magic;
  6. pthread_attr_t attr;
  7. rt_thread_t tid;
  8. void* (*thread_entry)(void* parameter);
  9. void* thread_parameter;
  10. /* return value */
  11. void* return_value;
  12. /* semaphore for joinable thread */
  13. rt_sem_t joinable_sem;
  14. void** tls; /* thread-local storage area */
  15. };
  16. typedef struct _pthread_data _pthread_data_t;
  17. rt_inline _pthread_data_t* get_pthread_data(pthread_t thread)
  18. {
  19. RT_ASSERT(thread != RT_NULL);
  20. return (_pthread_data_t*)thread->user_data;
  21. }
  22. int pthread_system_init(void)
  23. {
  24. return 0;
  25. }
  26. static void _pthread_cleanup(rt_thread_t tid)
  27. {
  28. _pthread_data_t *ptd;
  29. ptd = get_pthread_data(tid);
  30. /* clear cleanup function */
  31. tid->cleanup = RT_NULL;
  32. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  33. {
  34. rt_sem_release(ptd->joinable_sem);
  35. }
  36. else
  37. {
  38. /* release pthread resource */
  39. pthread_detach(tid);
  40. }
  41. }
  42. static void pthread_entry_stub(void* parameter)
  43. {
  44. _pthread_data_t *ptd;
  45. void* value;
  46. ptd = (_pthread_data_t*)parameter;
  47. /* execute pthread entry */
  48. value = ptd->thread_entry(ptd->thread_parameter);
  49. /* set value */
  50. ptd->return_value = value;
  51. }
  52. int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
  53. void *(*start) (void *), void *parameter)
  54. {
  55. int result;
  56. void* stack;
  57. char name[RT_NAME_MAX];
  58. static rt_uint16_t pthread_number = 0;
  59. _pthread_data_t *ptd;
  60. /* tid shall be provided */
  61. RT_ASSERT(tid != RT_NULL);
  62. /* allocate pthread data */
  63. ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
  64. if (ptd == RT_NULL) return ENOMEM;
  65. /* clean memory */
  66. rt_memset(ptd, 0, sizeof(_pthread_data_t));
  67. if (attr != RT_NULL) ptd->attr = *attr;
  68. else
  69. {
  70. /* use default attribute */
  71. pthread_attr_init(&ptd->attr);
  72. }
  73. rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
  74. if (ptd->attr.stack_base == 0)
  75. {
  76. stack = (void*)rt_malloc(ptd->attr.stack_size);
  77. }
  78. else stack = (void*)(ptd->attr.stack_base);
  79. if (stack == RT_NULL)
  80. {
  81. rt_free(ptd);
  82. return ENOMEM;
  83. }
  84. /* pthread is a static thread object */
  85. ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
  86. if (ptd->tid == RT_NULL)
  87. {
  88. if (ptd->attr.stack_base ==0) rt_free(stack);
  89. rt_free(ptd);
  90. return ENOMEM;
  91. }
  92. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  93. {
  94. ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  95. if (ptd->joinable_sem == RT_NULL)
  96. {
  97. if (ptd->attr.stack_base !=0) rt_free(stack);
  98. rt_free(ptd);
  99. return ENOMEM;
  100. }
  101. }
  102. else ptd->joinable_sem = RT_NULL;
  103. /* set parameter */
  104. ptd->thread_entry = start;
  105. ptd->thread_parameter = parameter;
  106. /* initial this pthread to system */
  107. if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
  108. stack, ptd->attr.stack_size,
  109. ptd->attr.priority, 5) != RT_EOK)
  110. {
  111. if (ptd->attr.stack_base ==0) rt_free(stack);
  112. if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem);
  113. rt_free(ptd);
  114. return EINVAL;
  115. }
  116. /* set pthread id */
  117. *tid = ptd->tid;
  118. /* set pthread cleanup function and ptd data */
  119. (*tid)->cleanup = _pthread_cleanup;
  120. (*tid)->user_data = (rt_uint32_t)ptd;
  121. /* start thread */
  122. result = rt_thread_startup(*tid);
  123. if (result == RT_EOK) return 0;
  124. /* start thread failed */
  125. rt_thread_detach(ptd->tid);
  126. if (ptd->attr.stack_base ==0) rt_free(stack);
  127. if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem);
  128. rt_free(ptd);
  129. return EINVAL;
  130. }
  131. int pthread_detach(pthread_t thread)
  132. {
  133. _pthread_data_t* ptd;
  134. ptd = get_pthread_data(thread);
  135. if (thread->stat == RT_THREAD_CLOSE)
  136. {
  137. /* delete joinable semaphore */
  138. if (ptd->joinable_sem != RT_NULL)
  139. rt_sem_delete(ptd->joinable_sem);
  140. /* detach thread object */
  141. rt_thread_detach(ptd->tid);
  142. /* release thread resource */
  143. if (ptd->attr.stack_base == RT_NULL)
  144. {
  145. /* release thread allocated stack */
  146. rt_free(ptd->tid->stack_addr);
  147. }
  148. rt_free(ptd->tid);
  149. rt_free(ptd);
  150. }
  151. else
  152. {
  153. rt_enter_critical();
  154. /* change to detach state */
  155. ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
  156. rt_exit_critical();
  157. /* detach joinable semaphore */
  158. rt_sem_delete(ptd->joinable_sem);
  159. }
  160. return 0;
  161. }
  162. int pthread_join (pthread_t thread, void **value_ptr)
  163. {
  164. _pthread_data_t* ptd;
  165. rt_err_t result;
  166. if (thread == rt_thread_self())
  167. {
  168. /* join self */
  169. return EDEADLK;
  170. }
  171. ptd = get_pthread_data(thread);
  172. if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
  173. return EINVAL; /* join on a detached pthread */
  174. result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
  175. if (result == RT_EOK)
  176. {
  177. /* get return value */
  178. if (value_ptr != RT_NULL) *value_ptr = ptd->return_value;
  179. /* release resource */
  180. pthread_detach(thread);
  181. }
  182. else return ESRCH;
  183. }
  184. int pthread_cancel (pthread_t thread)
  185. {
  186. _pthread_data_t* ptd;
  187. ptd = get_pthread_data(thread);
  188. /* check cancel point */
  189. }
  190. void pthread_exit (void* value)
  191. {
  192. _pthread_data_t* ptd;
  193. ptd = get_pthread_data(rt_thread_self());
  194. /* set return value */
  195. ptd->return_value = value;
  196. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  197. {
  198. /* release the joinable pthread */
  199. rt_sem_release(ptd->joinable_sem);
  200. }
  201. /* detach thread */
  202. rt_thread_detach(ptd->tid);
  203. /* reschedule thread */
  204. rt_schedule();
  205. }
  206. int pthread_once(pthread_once_t * once_control, void (*init_routine) (void))
  207. {
  208. RT_ASSERT(once_control != RT_NULL);
  209. RT_ASSERT(init_routine != RT_NULL);
  210. rt_enter_critical();
  211. if (!(*once_control))
  212. {
  213. /* call routine once */
  214. *once_control = 1;
  215. rt_exit_critical();
  216. init_routine();
  217. }
  218. rt_exit_critical();
  219. return 0;
  220. }
  221. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
  222. {
  223. return ENOTSUP;
  224. }