pthread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-01-26 Bernard Fix pthread_detach issue for a none-joinable
  9. * thread.
  10. * 2019-02-07 Bernard Add _pthread_destroy to release pthread resource.
  11. */
  12. #include <rthw.h>
  13. #include <pthread.h>
  14. #include <sched.h>
  15. #include "pthread_internal.h"
  16. RT_DEFINE_SPINLOCK(pth_lock);
  17. _pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL};
  18. _pthread_data_t *_pthread_get_data(pthread_t thread)
  19. {
  20. RT_DECLARE_SPINLOCK(pth_lock);
  21. _pthread_data_t *ptd;
  22. if (thread >= PTHREAD_NUM_MAX) return NULL;
  23. rt_hw_spin_lock(&pth_lock);
  24. ptd = pth_table[thread];
  25. rt_hw_spin_unlock(&pth_lock);
  26. if (ptd && ptd->magic == PTHREAD_MAGIC) return ptd;
  27. return NULL;
  28. }
  29. pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
  30. {
  31. int index;
  32. RT_DECLARE_SPINLOCK(pth_lock);
  33. rt_hw_spin_lock(&pth_lock);
  34. for (index = 0; index < PTHREAD_NUM_MAX; index ++)
  35. {
  36. if (pth_table[index] == ptd) break;
  37. }
  38. rt_hw_spin_unlock(&pth_lock);
  39. return index;
  40. }
  41. pthread_t _pthread_data_create(void)
  42. {
  43. int index;
  44. _pthread_data_t *ptd = NULL;
  45. RT_DECLARE_SPINLOCK(pth_lock);
  46. ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
  47. if (!ptd) return PTHREAD_NUM_MAX;
  48. memset(ptd, 0x0, sizeof(_pthread_data_t));
  49. ptd->canceled = 0;
  50. ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
  51. ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
  52. ptd->magic = PTHREAD_MAGIC;
  53. rt_hw_spin_lock(&pth_lock);
  54. for (index = 0; index < PTHREAD_NUM_MAX; index ++)
  55. {
  56. if (pth_table[index] == NULL)
  57. {
  58. pth_table[index] = ptd;
  59. break;
  60. }
  61. }
  62. rt_hw_spin_unlock(&pth_lock);
  63. /* full of pthreads, clean magic and release ptd */
  64. if (index == PTHREAD_NUM_MAX)
  65. {
  66. ptd->magic = 0x0;
  67. rt_free(ptd);
  68. }
  69. return index;
  70. }
  71. void _pthread_data_destroy(pthread_t pth)
  72. {
  73. RT_DECLARE_SPINLOCK(pth_lock);
  74. _pthread_data_t *ptd = _pthread_get_data(pth);
  75. if (ptd)
  76. {
  77. /* remove from pthread table */
  78. rt_hw_spin_lock(&pth_lock);
  79. pth_table[pth] = NULL;
  80. rt_hw_spin_unlock(&pth_lock);
  81. /* delete joinable semaphore */
  82. if (ptd->joinable_sem != RT_NULL)
  83. rt_sem_delete(ptd->joinable_sem);
  84. /* release thread resource */
  85. if (ptd->attr.stackaddr == RT_NULL)
  86. {
  87. /* release thread allocated stack */
  88. rt_free(ptd->tid->stack_addr);
  89. }
  90. /* clean stack addr pointer */
  91. ptd->tid->stack_addr = RT_NULL;
  92. /*
  93. * if this thread create the local thread data,
  94. * delete it
  95. */
  96. if (ptd->tls != RT_NULL) rt_free(ptd->tls);
  97. rt_free(ptd->tid);
  98. /* clean magic */
  99. ptd->magic = 0x0;
  100. /* free ptd */
  101. rt_free(ptd);
  102. }
  103. }
  104. int pthread_system_init(void)
  105. {
  106. /* initialize key area */
  107. pthread_key_system_init();
  108. /* initialize posix mqueue */
  109. posix_mq_system_init();
  110. /* initialize posix semaphore */
  111. posix_sem_system_init();
  112. return 0;
  113. }
  114. INIT_COMPONENT_EXPORT(pthread_system_init);
  115. static void _pthread_destroy(_pthread_data_t *ptd)
  116. {
  117. pthread_t pth = _pthread_data_get_pth(ptd);
  118. if (pth != PTHREAD_NUM_MAX)
  119. {
  120. _pthread_data_destroy(pth);
  121. }
  122. return;
  123. }
  124. static void _pthread_cleanup(rt_thread_t tid)
  125. {
  126. _pthread_data_t *ptd;
  127. /* get pthread data from user data of thread */
  128. ptd = (_pthread_data_t *)tid->user_data;
  129. RT_ASSERT(ptd != RT_NULL);
  130. /* clear cleanup function */
  131. tid->cleanup = RT_NULL;
  132. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  133. {
  134. rt_sem_release(ptd->joinable_sem);
  135. }
  136. else
  137. {
  138. /* release pthread resource */
  139. _pthread_destroy(ptd);
  140. }
  141. }
  142. static void pthread_entry_stub(void *parameter)
  143. {
  144. void *value;
  145. _pthread_data_t *ptd;
  146. ptd = (_pthread_data_t *)parameter;
  147. /* execute pthread entry */
  148. value = ptd->thread_entry(ptd->thread_parameter);
  149. /* set value */
  150. ptd->return_value = value;
  151. }
  152. int pthread_create(pthread_t *pid,
  153. const pthread_attr_t *attr,
  154. void *(*start)(void *), void *parameter)
  155. {
  156. int ret = 0;
  157. void *stack;
  158. char name[RT_NAME_MAX];
  159. static rt_uint16_t pthread_number = 0;
  160. pthread_t pth_id;
  161. _pthread_data_t *ptd;
  162. /* pid shall be provided */
  163. RT_ASSERT(pid != RT_NULL);
  164. /* allocate posix thread data */
  165. pth_id = _pthread_data_create();
  166. if (pth_id == PTHREAD_NUM_MAX)
  167. {
  168. ret = ENOMEM;
  169. goto __exit;
  170. }
  171. /* get pthread data */
  172. ptd = _pthread_get_data(pth_id);
  173. if (attr != RT_NULL)
  174. {
  175. ptd->attr = *attr;
  176. }
  177. else
  178. {
  179. /* use default attribute */
  180. pthread_attr_init(&ptd->attr);
  181. }
  182. rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
  183. if (ptd->attr.stackaddr == 0)
  184. {
  185. stack = (void *)rt_malloc(ptd->attr.stacksize);
  186. }
  187. else
  188. {
  189. stack = (void *)(ptd->attr.stackaddr);
  190. }
  191. if (stack == RT_NULL)
  192. {
  193. ret = ENOMEM;
  194. goto __exit;
  195. }
  196. /* pthread is a static thread object */
  197. ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
  198. if (ptd->tid == RT_NULL)
  199. {
  200. ret = ENOMEM;
  201. goto __exit;
  202. }
  203. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  204. {
  205. ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  206. if (ptd->joinable_sem == RT_NULL)
  207. {
  208. ret = ENOMEM;
  209. goto __exit;
  210. }
  211. }
  212. else
  213. {
  214. ptd->joinable_sem = RT_NULL;
  215. }
  216. /* set parameter */
  217. ptd->thread_entry = start;
  218. ptd->thread_parameter = parameter;
  219. /* initial this pthread to system */
  220. if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
  221. stack, ptd->attr.stacksize,
  222. ptd->attr.schedparam.sched_priority, 5) != RT_EOK)
  223. {
  224. ret = EINVAL;
  225. goto __exit;
  226. }
  227. /* set pthread id */
  228. *pid = pth_id;
  229. /* set pthread cleanup function and ptd data */
  230. ptd->tid->cleanup = _pthread_cleanup;
  231. ptd->tid->user_data = (rt_uint32_t)ptd;
  232. /* start thread */
  233. if (rt_thread_startup(ptd->tid) == RT_EOK)
  234. return 0;
  235. /* start thread failed */
  236. rt_thread_detach(ptd->tid);
  237. ret = EINVAL;
  238. __exit:
  239. if (pth_id != PTHREAD_NUM_MAX)
  240. _pthread_data_destroy(pth_id);
  241. return ret;
  242. }
  243. RTM_EXPORT(pthread_create);
  244. int pthread_detach(pthread_t thread)
  245. {
  246. int ret = 0;
  247. _pthread_data_t *ptd = _pthread_get_data(thread);
  248. rt_enter_critical();
  249. if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
  250. {
  251. /* The implementation has detected that the value specified by thread does not refer
  252. * to a joinable thread.
  253. */
  254. ret = EINVAL;
  255. goto __exit;
  256. }
  257. if ((ptd->tid->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  258. {
  259. /* this defunct pthread is not handled by idle */
  260. if (rt_sem_trytake(ptd->joinable_sem) != RT_EOK)
  261. {
  262. rt_sem_release(ptd->joinable_sem);
  263. /* change to detach state */
  264. ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
  265. /* detach joinable semaphore */
  266. if (ptd->joinable_sem)
  267. {
  268. rt_sem_delete(ptd->joinable_sem);
  269. ptd->joinable_sem = RT_NULL;
  270. }
  271. }
  272. else
  273. {
  274. /* destroy this pthread */
  275. _pthread_destroy(ptd);
  276. }
  277. goto __exit;
  278. }
  279. else
  280. {
  281. /* change to detach state */
  282. ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
  283. /* detach joinable semaphore */
  284. if (ptd->joinable_sem)
  285. {
  286. rt_sem_delete(ptd->joinable_sem);
  287. ptd->joinable_sem = RT_NULL;
  288. }
  289. }
  290. __exit:
  291. rt_exit_critical();
  292. return ret;
  293. }
  294. RTM_EXPORT(pthread_detach);
  295. int pthread_join(pthread_t thread, void **value_ptr)
  296. {
  297. _pthread_data_t *ptd;
  298. rt_err_t result;
  299. ptd = _pthread_get_data(thread);
  300. if (ptd && ptd->tid == rt_thread_self())
  301. {
  302. /* join self */
  303. return EDEADLK;
  304. }
  305. if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
  306. return EINVAL; /* join on a detached pthread */
  307. result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
  308. if (result == RT_EOK)
  309. {
  310. /* get return value */
  311. if (value_ptr != RT_NULL)
  312. *value_ptr = ptd->return_value;
  313. /* destroy this pthread */
  314. _pthread_destroy(ptd);
  315. }
  316. else
  317. {
  318. return ESRCH;
  319. }
  320. return 0;
  321. }
  322. RTM_EXPORT(pthread_join);
  323. pthread_t pthread_self (void)
  324. {
  325. rt_thread_t tid;
  326. _pthread_data_t *ptd;
  327. tid = rt_thread_self();
  328. if (tid == NULL) return PTHREAD_NUM_MAX;
  329. /* get pthread data from user data of thread */
  330. ptd = (_pthread_data_t *)rt_thread_self()->user_data;
  331. RT_ASSERT(ptd != RT_NULL);
  332. return _pthread_data_get_pth(ptd);
  333. }
  334. RTM_EXPORT(pthread_self);
  335. void pthread_exit(void *value)
  336. {
  337. _pthread_data_t *ptd;
  338. _pthread_cleanup_t *cleanup;
  339. extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
  340. if (rt_thread_self() == NULL) return;
  341. /* get pthread data from user data of thread */
  342. ptd = (_pthread_data_t *)rt_thread_self()->user_data;
  343. rt_enter_critical();
  344. /* disable cancel */
  345. ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
  346. /* set return value */
  347. ptd->return_value = value;
  348. rt_exit_critical();
  349. /* invoke pushed cleanup */
  350. while (ptd->cleanup != RT_NULL)
  351. {
  352. cleanup = ptd->cleanup;
  353. ptd->cleanup = cleanup->next;
  354. cleanup->cleanup_func(cleanup->parameter);
  355. /* release this cleanup function */
  356. rt_free(cleanup);
  357. }
  358. /* destruct thread local key */
  359. if (ptd->tls != RT_NULL)
  360. {
  361. void *data;
  362. rt_uint32_t index;
  363. for (index = 0; index < PTHREAD_KEY_MAX; index ++)
  364. {
  365. if (_thread_keys[index].is_used)
  366. {
  367. data = ptd->tls[index];
  368. if (data)
  369. _thread_keys[index].destructor(data);
  370. }
  371. }
  372. /* release tls area */
  373. rt_free(ptd->tls);
  374. ptd->tls = RT_NULL;
  375. }
  376. /* detach thread */
  377. rt_thread_detach(ptd->tid);
  378. /* reschedule thread */
  379. rt_schedule();
  380. }
  381. RTM_EXPORT(pthread_exit);
  382. int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  383. {
  384. RT_ASSERT(once_control != RT_NULL);
  385. RT_ASSERT(init_routine != RT_NULL);
  386. rt_enter_critical();
  387. if (!(*once_control))
  388. {
  389. /* call routine once */
  390. *once_control = 1;
  391. rt_exit_critical();
  392. init_routine();
  393. }
  394. rt_exit_critical();
  395. return 0;
  396. }
  397. RTM_EXPORT(pthread_once);
  398. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
  399. {
  400. return EOPNOTSUPP;
  401. }
  402. RTM_EXPORT(pthread_atfork);
  403. int pthread_kill(pthread_t thread, int sig)
  404. {
  405. #ifdef RT_USING_SIGNALS
  406. _pthread_data_t *ptd;
  407. ptd = _pthread_get_data(thread);
  408. if (ptd)
  409. {
  410. return rt_thread_kill(ptd->tid, sig);
  411. }
  412. return EINVAL;
  413. #else
  414. return ENOSYS;
  415. #endif
  416. }
  417. RTM_EXPORT(pthread_kill);
  418. #ifdef RT_USING_SIGNALS
  419. int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
  420. {
  421. return sigprocmask(how, set, oset);
  422. }
  423. #endif
  424. void pthread_cleanup_pop(int execute)
  425. {
  426. _pthread_data_t *ptd;
  427. _pthread_cleanup_t *cleanup;
  428. if (rt_thread_self() == NULL) return;
  429. /* get pthread data from user data of thread */
  430. ptd = (_pthread_data_t *)rt_thread_self()->user_data;
  431. RT_ASSERT(ptd != RT_NULL);
  432. if (execute)
  433. {
  434. rt_enter_critical();
  435. cleanup = ptd->cleanup;
  436. if (cleanup)
  437. ptd->cleanup = cleanup->next;
  438. rt_exit_critical();
  439. if (cleanup)
  440. {
  441. cleanup->cleanup_func(cleanup->parameter);
  442. rt_free(cleanup);
  443. }
  444. }
  445. }
  446. RTM_EXPORT(pthread_cleanup_pop);
  447. void pthread_cleanup_push(void (*routine)(void *), void *arg)
  448. {
  449. _pthread_data_t *ptd;
  450. _pthread_cleanup_t *cleanup;
  451. if (rt_thread_self() == NULL) return;
  452. /* get pthread data from user data of thread */
  453. ptd = (_pthread_data_t *)rt_thread_self()->user_data;
  454. RT_ASSERT(ptd != RT_NULL);
  455. cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
  456. if (cleanup != RT_NULL)
  457. {
  458. cleanup->cleanup_func = routine;
  459. cleanup->parameter = arg;
  460. rt_enter_critical();
  461. cleanup->next = ptd->cleanup;
  462. ptd->cleanup = cleanup;
  463. rt_exit_critical();
  464. }
  465. }
  466. RTM_EXPORT(pthread_cleanup_push);
  467. /*
  468. * According to IEEE Std 1003.1, 2004 Edition , following pthreads
  469. * interface support cancellation point:
  470. * mq_receive()
  471. * mq_send()
  472. * mq_timedreceive()
  473. * mq_timedsend()
  474. * msgrcv()
  475. * msgsnd()
  476. * msync()
  477. * pthread_cond_timedwait()
  478. * pthread_cond_wait()
  479. * pthread_join()
  480. * pthread_testcancel()
  481. * sem_timedwait()
  482. * sem_wait()
  483. *
  484. * A cancellation point may also occur when a thread is
  485. * executing the following functions:
  486. * pthread_rwlock_rdlock()
  487. * pthread_rwlock_timedrdlock()
  488. * pthread_rwlock_timedwrlock()
  489. * pthread_rwlock_wrlock()
  490. *
  491. * The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
  492. * functions are defined to be async-cancel safe.
  493. */
  494. int pthread_setcancelstate(int state, int *oldstate)
  495. {
  496. _pthread_data_t *ptd;
  497. if (rt_thread_self() == NULL) return EINVAL;
  498. /* get pthread data from user data of thread */
  499. ptd = (_pthread_data_t *)rt_thread_self()->user_data;
  500. RT_ASSERT(ptd != RT_NULL);
  501. if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
  502. {
  503. if (oldstate)
  504. *oldstate = ptd->cancelstate;
  505. ptd->cancelstate = state;
  506. return 0;
  507. }
  508. return EINVAL;
  509. }
  510. RTM_EXPORT(pthread_setcancelstate);
  511. int pthread_setcanceltype(int type, int *oldtype)
  512. {
  513. _pthread_data_t *ptd;
  514. if (rt_thread_self() == NULL) return EINVAL;
  515. /* get pthread data from user data of thread */
  516. ptd = (_pthread_data_t *)rt_thread_self()->user_data;
  517. RT_ASSERT(ptd != RT_NULL);
  518. if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
  519. return EINVAL;
  520. if (oldtype)
  521. *oldtype = ptd->canceltype;
  522. ptd->canceltype = type;
  523. return 0;
  524. }
  525. RTM_EXPORT(pthread_setcanceltype);
  526. void pthread_testcancel(void)
  527. {
  528. int cancel = 0;
  529. _pthread_data_t *ptd;
  530. if (rt_thread_self() == NULL) return;
  531. /* get pthread data from user data of thread */
  532. ptd = (_pthread_data_t *)rt_thread_self()->user_data;
  533. RT_ASSERT(ptd != RT_NULL);
  534. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  535. cancel = ptd->canceled;
  536. if (cancel)
  537. pthread_exit((void *)PTHREAD_CANCELED);
  538. }
  539. RTM_EXPORT(pthread_testcancel);
  540. int pthread_cancel(pthread_t thread)
  541. {
  542. _pthread_data_t *ptd;
  543. /* get posix thread data */
  544. ptd = _pthread_get_data(thread);
  545. RT_ASSERT(ptd != RT_NULL);
  546. /* cancel self */
  547. if (ptd->tid == rt_thread_self())
  548. return 0;
  549. /* set canceled */
  550. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  551. {
  552. ptd->canceled = 1;
  553. if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
  554. {
  555. /*
  556. * to detach thread.
  557. * this thread will be removed from scheduler list
  558. * and because there is a cleanup function in the
  559. * thread (pthread_cleanup), it will move to defunct
  560. * thread list and wait for handling in idle thread.
  561. */
  562. rt_thread_detach(ptd->tid);
  563. }
  564. }
  565. return 0;
  566. }
  567. RTM_EXPORT(pthread_cancel);