pthread.c 13 KB

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