pthread.c 9.8 KB

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