pthread.c 16 KB

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