pthread.c 16 KB

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