1
0

pthread.c 18 KB

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