pthread.c 18 KB

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