pthread.c 15 KB

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