pthread.c 10 KB

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