1
0

pthread.c 13 KB

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