pthread.c 15 KB

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