pthread.c 19 KB

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