pthread.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. ptd->canceled = 0;
  51. ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
  52. ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
  53. if (attr != RT_NULL) ptd->attr = *attr;
  54. else
  55. {
  56. /* use default attribute */
  57. pthread_attr_init(&ptd->attr);
  58. }
  59. rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
  60. if (ptd->attr.stack_base == 0)
  61. {
  62. stack = (void*)rt_malloc(ptd->attr.stack_size);
  63. }
  64. else stack = (void*)(ptd->attr.stack_base);
  65. if (stack == RT_NULL)
  66. {
  67. rt_free(ptd);
  68. return ENOMEM;
  69. }
  70. /* pthread is a static thread object */
  71. ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
  72. if (ptd->tid == RT_NULL)
  73. {
  74. if (ptd->attr.stack_base ==0) rt_free(stack);
  75. rt_free(ptd);
  76. return ENOMEM;
  77. }
  78. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  79. {
  80. ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  81. if (ptd->joinable_sem == RT_NULL)
  82. {
  83. if (ptd->attr.stack_base !=0) rt_free(stack);
  84. rt_free(ptd);
  85. return ENOMEM;
  86. }
  87. }
  88. else ptd->joinable_sem = RT_NULL;
  89. /* set parameter */
  90. ptd->thread_entry = start;
  91. ptd->thread_parameter = parameter;
  92. /* initial this pthread to system */
  93. if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
  94. stack, ptd->attr.stack_size,
  95. ptd->attr.priority, 5) != RT_EOK)
  96. {
  97. if (ptd->attr.stack_base ==0) rt_free(stack);
  98. if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem);
  99. rt_free(ptd);
  100. return EINVAL;
  101. }
  102. /* set pthread id */
  103. *tid = ptd->tid;
  104. /* set pthread cleanup function and ptd data */
  105. (*tid)->cleanup = _pthread_cleanup;
  106. (*tid)->user_data = (rt_uint32_t)ptd;
  107. /* start thread */
  108. result = rt_thread_startup(*tid);
  109. if (result == RT_EOK) return 0;
  110. /* start thread failed */
  111. rt_thread_detach(ptd->tid);
  112. if (ptd->attr.stack_base ==0) rt_free(stack);
  113. if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem);
  114. rt_free(ptd);
  115. return EINVAL;
  116. }
  117. int pthread_detach(pthread_t thread)
  118. {
  119. _pthread_data_t* ptd;
  120. ptd = _pthread_get_data(thread);
  121. if (thread->stat == RT_THREAD_CLOSE)
  122. {
  123. /* delete joinable semaphore */
  124. if (ptd->joinable_sem != RT_NULL)
  125. rt_sem_delete(ptd->joinable_sem);
  126. /* detach thread object */
  127. rt_thread_detach(ptd->tid);
  128. /* release thread resource */
  129. if (ptd->attr.stack_base == RT_NULL)
  130. {
  131. /* release thread allocated stack */
  132. rt_free(ptd->tid->stack_addr);
  133. }
  134. /*
  135. * if this thread create the local thread data,
  136. * delete it
  137. */
  138. if (ptd->tls != RT_NULL) rt_free(ptd->tls);
  139. rt_free(ptd->tid);
  140. rt_free(ptd);
  141. }
  142. else
  143. {
  144. rt_enter_critical();
  145. /* change to detach state */
  146. ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
  147. rt_exit_critical();
  148. /* detach joinable semaphore */
  149. rt_sem_delete(ptd->joinable_sem);
  150. }
  151. return 0;
  152. }
  153. int pthread_join (pthread_t thread, void **value_ptr)
  154. {
  155. _pthread_data_t* ptd;
  156. rt_err_t result;
  157. if (thread == rt_thread_self())
  158. {
  159. /* join self */
  160. return EDEADLK;
  161. }
  162. ptd = _pthread_get_data(thread);
  163. if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
  164. return EINVAL; /* join on a detached pthread */
  165. result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
  166. if (result == RT_EOK)
  167. {
  168. /* get return value */
  169. if (value_ptr != RT_NULL) *value_ptr = ptd->return_value;
  170. /* release resource */
  171. pthread_detach(thread);
  172. }
  173. else return ESRCH;
  174. }
  175. void pthread_exit (void* value)
  176. {
  177. _pthread_data_t* ptd;
  178. _pthread_cleanup_t* cleanup;
  179. _pthread_key_data_t* key;
  180. extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
  181. ptd = _pthread_get_data(rt_thread_self());
  182. rt_enter_critical();
  183. /* disable cancel */
  184. ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
  185. /* set return value */
  186. ptd->return_value = value;
  187. rt_exit_critical();
  188. /* invoke pushed cleanup */
  189. while (ptd->cleanup != RT_NULL)
  190. {
  191. cleanup = ptd->cleanup;
  192. ptd->cleanup = cleanup->next;
  193. cleanup->cleanup_func(cleanup->parameter);
  194. /* release this cleanup function */
  195. rt_free(cleanup);
  196. }
  197. /* destruct thread local key */
  198. if (ptd->tls != RT_NULL)
  199. {
  200. void* data;
  201. rt_uint32_t index;
  202. for (index = 0; index < PTHREAD_KEY_MAX; index ++)
  203. {
  204. if (_thread_keys[index].is_used)
  205. {
  206. data = ptd->tls[index];
  207. if (data)
  208. _thread_keys[index].destructor(data);
  209. }
  210. }
  211. /* release tls area */
  212. rt_free(ptd->tls);
  213. ptd->tls = RT_NULL;
  214. }
  215. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  216. {
  217. /* release the joinable pthread */
  218. rt_sem_release(ptd->joinable_sem);
  219. }
  220. /* detach thread */
  221. rt_thread_detach(ptd->tid);
  222. /* reschedule thread */
  223. rt_schedule();
  224. }
  225. int pthread_once(pthread_once_t * once_control, void (*init_routine) (void))
  226. {
  227. RT_ASSERT(once_control != RT_NULL);
  228. RT_ASSERT(init_routine != RT_NULL);
  229. rt_enter_critical();
  230. if (!(*once_control))
  231. {
  232. /* call routine once */
  233. *once_control = 1;
  234. rt_exit_critical();
  235. init_routine();
  236. }
  237. rt_exit_critical();
  238. return 0;
  239. }
  240. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
  241. {
  242. return ENOTSUP;
  243. }
  244. int pthread_kill(pthread_t thread, int sig)
  245. {
  246. return ENOTSUP;
  247. }
  248. void pthread_cleanup_pop(int execute)
  249. {
  250. _pthread_data_t* ptd;
  251. _pthread_cleanup_t* cleanup;
  252. /* get posix thread data */
  253. ptd = _pthread_get_data(rt_thread_self());
  254. RT_ASSERT(ptd != RT_NULL);
  255. if (execute)
  256. {
  257. rt_enter_critical();
  258. cleanup = ptd->cleanup;
  259. if (cleanup)
  260. ptd->cleanup = cleanup->next;
  261. rt_exit_critical();
  262. if (cleanup)
  263. {
  264. cleanup->cleanup_func(cleanup->parameter);
  265. rt_free(cleanup);
  266. }
  267. }
  268. }
  269. void pthread_cleanup_push(void (*routine)(void*), void *arg)
  270. {
  271. _pthread_data_t* ptd;
  272. _pthread_cleanup_t* cleanup;
  273. /* get posix thread data */
  274. ptd = _pthread_get_data(rt_thread_self());
  275. RT_ASSERT(ptd != RT_NULL);
  276. cleanup = (_pthread_cleanup_t*)rt_malloc(sizeof(_pthread_cleanup_t));
  277. if (cleanup != RT_NULL)
  278. {
  279. cleanup->cleanup_func = routine;
  280. cleanup->parameter = arg;
  281. rt_enter_critical();
  282. cleanup->next = ptd->cleanup;
  283. ptd->cleanup = cleanup;
  284. rt_exit_critical();
  285. }
  286. }
  287. /*
  288. * According to IEEE Std 1003.1, 2004 Edition , following pthreads
  289. * interface support cancellation point:
  290. * mq_receive()
  291. * mq_send()
  292. * mq_timedreceive()
  293. * mq_timedsend()
  294. * msgrcv()
  295. * msgsnd()
  296. * msync()
  297. * pthread_cond_timedwait()
  298. * pthread_cond_wait()
  299. * pthread_join()
  300. * pthread_testcancel()
  301. * sem_timedwait()
  302. * sem_wait()
  303. *
  304. * A cancellation point may also occur when a thread is
  305. * executing the following functions:
  306. * pthread_rwlock_rdlock()
  307. * pthread_rwlock_timedrdlock()
  308. * pthread_rwlock_timedwrlock()
  309. * pthread_rwlock_wrlock()
  310. *
  311. * The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
  312. * functions are defined to be async-cancel safe.
  313. */
  314. int pthread_setcancelstate(int state, int *oldstate)
  315. {
  316. _pthread_data_t* ptd;
  317. /* get posix thread data */
  318. ptd = _pthread_get_data(rt_thread_self());
  319. RT_ASSERT(ptd != RT_NULL);
  320. if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
  321. {
  322. if (oldstate) *oldstate = ptd->cancelstate;
  323. ptd->cancelstate = state;
  324. return 0;
  325. }
  326. return EINVAL;
  327. }
  328. int pthread_setcanceltype(int type, int *oldtype)
  329. {
  330. _pthread_data_t* ptd;
  331. /* get posix thread data */
  332. ptd = _pthread_get_data(rt_thread_self());
  333. RT_ASSERT(ptd != RT_NULL);
  334. if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
  335. return EINVAL;
  336. if (oldtype) *oldtype = ptd->canceltype;
  337. ptd->canceltype = type;
  338. return 0;
  339. }
  340. void pthread_testcancel(void)
  341. {
  342. int cancel=0;
  343. _pthread_data_t* ptd;
  344. /* get posix thread data */
  345. ptd = _pthread_get_data(rt_thread_self());
  346. RT_ASSERT(ptd != RT_NULL);
  347. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE) cancel = ptd->canceled;
  348. if (cancel) pthread_exit((void*)PTHREAD_CANCELED);
  349. }
  350. int pthread_cancel(pthread_t thread)
  351. {
  352. _pthread_data_t* ptd;
  353. /* cancel self */
  354. if (thread == rt_thread_self()) return 0;
  355. /* get posix thread data */
  356. ptd = _pthread_get_data(thread);
  357. RT_ASSERT(ptd != RT_NULL);
  358. /* set canceled */
  359. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  360. {
  361. ptd->canceled = 1;
  362. if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
  363. {
  364. /*
  365. * to detach thread.
  366. * this thread will be removed from scheduler list
  367. * and because there is a cleanup function in the
  368. * thread (pthread_cleanup), it will move to defunct
  369. * thread list and wait for handling in idle thread.
  370. */
  371. rt_thread_detach(thread);
  372. }
  373. }
  374. return 0;
  375. }