pthread.c 6.7 KB

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