1
0

pthread.c 17 KB

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