pthread.c 18 KB

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