pthread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * File : pthread.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, 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. */
  23. #include <pthread.h>
  24. #include <sched.h>
  25. #include "pthread_internal.h"
  26. int pthread_system_init(void)
  27. {
  28. /* initialize clock and time */
  29. clock_time_system_init();
  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. ptd->attr = *attr;
  88. else
  89. {
  90. /* use default attribute */
  91. pthread_attr_init(&ptd->attr);
  92. }
  93. rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
  94. if (ptd->attr.stack_base == 0)
  95. {
  96. stack = (void*)rt_malloc(ptd->attr.stack_size);
  97. }
  98. else
  99. stack = (void*)(ptd->attr.stack_base);
  100. if (stack == RT_NULL)
  101. {
  102. rt_free(ptd);
  103. return ENOMEM;
  104. }
  105. /* pthread is a static thread object */
  106. ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
  107. if (ptd->tid == RT_NULL)
  108. {
  109. if (ptd->attr.stack_base == 0)
  110. rt_free(stack);
  111. rt_free(ptd);
  112. return ENOMEM;
  113. }
  114. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  115. {
  116. ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  117. if (ptd->joinable_sem == RT_NULL)
  118. {
  119. if (ptd->attr.stack_base != 0)
  120. rt_free(stack);
  121. rt_free(ptd);
  122. return ENOMEM;
  123. }
  124. }
  125. else
  126. ptd->joinable_sem = RT_NULL;
  127. /* set parameter */
  128. ptd->thread_entry = start;
  129. ptd->thread_parameter = parameter;
  130. /* initial this pthread to system */
  131. if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
  132. stack, ptd->attr.stack_size,
  133. ptd->attr.priority, 5) != RT_EOK)
  134. {
  135. if (ptd->attr.stack_base == 0)
  136. rt_free(stack);
  137. if (ptd->joinable_sem != RT_NULL)
  138. rt_sem_delete(ptd->joinable_sem);
  139. rt_free(ptd);
  140. return EINVAL;
  141. }
  142. /* set pthread id */
  143. *tid = ptd->tid;
  144. /* set pthread cleanup function and ptd data */
  145. (*tid)->cleanup = _pthread_cleanup;
  146. (*tid)->user_data = (rt_uint32_t)ptd;
  147. /* start thread */
  148. result = rt_thread_startup(*tid);
  149. if (result == RT_EOK)
  150. return 0;
  151. /* start thread failed */
  152. rt_thread_detach(ptd->tid);
  153. if (ptd->attr.stack_base == 0)
  154. rt_free(stack);
  155. if (ptd->joinable_sem != RT_NULL)
  156. rt_sem_delete(ptd->joinable_sem);
  157. rt_free(ptd);
  158. return EINVAL;
  159. }
  160. RTM_EXPORT(pthread_create);
  161. int pthread_detach(pthread_t thread)
  162. {
  163. _pthread_data_t* ptd;
  164. ptd = _pthread_get_data(thread);
  165. if (thread->stat == RT_THREAD_CLOSE)
  166. {
  167. /* delete joinable semaphore */
  168. if (ptd->joinable_sem != RT_NULL)
  169. rt_sem_delete(ptd->joinable_sem);
  170. /* detach thread object */
  171. rt_thread_detach(ptd->tid);
  172. /* release thread resource */
  173. if (ptd->attr.stack_base == RT_NULL)
  174. {
  175. /* release thread allocated stack */
  176. rt_free(ptd->tid->stack_addr);
  177. }
  178. /*
  179. * if this thread create the local thread data,
  180. * delete it
  181. */
  182. if (ptd->tls != RT_NULL)
  183. rt_free(ptd->tls);
  184. rt_free(ptd->tid);
  185. rt_free(ptd);
  186. }
  187. else
  188. {
  189. rt_enter_critical();
  190. /* change to detach state */
  191. ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
  192. /* detach joinable semaphore */
  193. rt_sem_delete(ptd->joinable_sem);
  194. ptd->joinable_sem = RT_NULL;
  195. rt_exit_critical();
  196. }
  197. return 0;
  198. }
  199. RTM_EXPORT(pthread_detach);
  200. int pthread_join (pthread_t thread, void **value_ptr)
  201. {
  202. _pthread_data_t* ptd;
  203. rt_err_t result;
  204. if (thread == rt_thread_self())
  205. {
  206. /* join self */
  207. return EDEADLK;
  208. }
  209. ptd = _pthread_get_data(thread);
  210. if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
  211. return EINVAL; /* join on a detached pthread */
  212. result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
  213. if (result == RT_EOK)
  214. {
  215. /* get return value */
  216. if (value_ptr != RT_NULL)
  217. *value_ptr = ptd->return_value;
  218. /* release resource */
  219. pthread_detach(thread);
  220. }
  221. else
  222. return ESRCH;
  223. return 0;
  224. }
  225. RTM_EXPORT(pthread_join);
  226. void pthread_exit (void *value)
  227. {
  228. _pthread_data_t *ptd;
  229. _pthread_cleanup_t *cleanup;
  230. extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
  231. ptd = _pthread_get_data(rt_thread_self());
  232. rt_enter_critical();
  233. /* disable cancel */
  234. ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
  235. /* set return value */
  236. ptd->return_value = value;
  237. rt_exit_critical();
  238. /* invoke pushed cleanup */
  239. while (ptd->cleanup != RT_NULL)
  240. {
  241. cleanup = ptd->cleanup;
  242. ptd->cleanup = cleanup->next;
  243. cleanup->cleanup_func(cleanup->parameter);
  244. /* release this cleanup function */
  245. rt_free(cleanup);
  246. }
  247. /* destruct thread local key */
  248. if (ptd->tls != RT_NULL)
  249. {
  250. void *data;
  251. rt_uint32_t index;
  252. for (index = 0; index < PTHREAD_KEY_MAX; index ++)
  253. {
  254. if (_thread_keys[index].is_used)
  255. {
  256. data = ptd->tls[index];
  257. if (data)
  258. _thread_keys[index].destructor(data);
  259. }
  260. }
  261. /* release tls area */
  262. rt_free(ptd->tls);
  263. ptd->tls = RT_NULL;
  264. }
  265. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  266. {
  267. /* release the joinable pthread */
  268. rt_sem_release(ptd->joinable_sem);
  269. }
  270. /* detach thread */
  271. rt_thread_detach(ptd->tid);
  272. /* reschedule thread */
  273. rt_schedule();
  274. }
  275. RTM_EXPORT(pthread_exit);
  276. int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
  277. {
  278. RT_ASSERT(once_control != RT_NULL);
  279. RT_ASSERT(init_routine != RT_NULL);
  280. rt_enter_critical();
  281. if (!(*once_control))
  282. {
  283. /* call routine once */
  284. *once_control = 1;
  285. rt_exit_critical();
  286. init_routine();
  287. }
  288. rt_exit_critical();
  289. return 0;
  290. }
  291. RTM_EXPORT(pthread_once);
  292. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
  293. {
  294. return EOPNOTSUPP;
  295. }
  296. RTM_EXPORT(pthread_atfork);
  297. int pthread_kill(pthread_t thread, int sig)
  298. {
  299. return EOPNOTSUPP;
  300. }
  301. RTM_EXPORT(pthread_kill);
  302. void pthread_cleanup_pop(int execute)
  303. {
  304. _pthread_data_t *ptd;
  305. _pthread_cleanup_t *cleanup;
  306. /* get posix thread data */
  307. ptd = _pthread_get_data(rt_thread_self());
  308. RT_ASSERT(ptd != RT_NULL);
  309. if (execute)
  310. {
  311. rt_enter_critical();
  312. cleanup = ptd->cleanup;
  313. if (cleanup)
  314. ptd->cleanup = cleanup->next;
  315. rt_exit_critical();
  316. if (cleanup)
  317. {
  318. cleanup->cleanup_func(cleanup->parameter);
  319. rt_free(cleanup);
  320. }
  321. }
  322. }
  323. RTM_EXPORT(pthread_cleanup_pop);
  324. void pthread_cleanup_push(void (*routine)(void*), void *arg)
  325. {
  326. _pthread_data_t *ptd;
  327. _pthread_cleanup_t *cleanup;
  328. /* get posix thread data */
  329. ptd = _pthread_get_data(rt_thread_self());
  330. RT_ASSERT(ptd != RT_NULL);
  331. cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
  332. if (cleanup != RT_NULL)
  333. {
  334. cleanup->cleanup_func = routine;
  335. cleanup->parameter = arg;
  336. rt_enter_critical();
  337. cleanup->next = ptd->cleanup;
  338. ptd->cleanup = cleanup;
  339. rt_exit_critical();
  340. }
  341. }
  342. RTM_EXPORT(pthread_cleanup_push);
  343. /*
  344. * According to IEEE Std 1003.1, 2004 Edition , following pthreads
  345. * interface support cancellation point:
  346. * mq_receive()
  347. * mq_send()
  348. * mq_timedreceive()
  349. * mq_timedsend()
  350. * msgrcv()
  351. * msgsnd()
  352. * msync()
  353. * pthread_cond_timedwait()
  354. * pthread_cond_wait()
  355. * pthread_join()
  356. * pthread_testcancel()
  357. * sem_timedwait()
  358. * sem_wait()
  359. *
  360. * A cancellation point may also occur when a thread is
  361. * executing the following functions:
  362. * pthread_rwlock_rdlock()
  363. * pthread_rwlock_timedrdlock()
  364. * pthread_rwlock_timedwrlock()
  365. * pthread_rwlock_wrlock()
  366. *
  367. * The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
  368. * functions are defined to be async-cancel safe.
  369. */
  370. int pthread_setcancelstate(int state, int *oldstate)
  371. {
  372. _pthread_data_t *ptd;
  373. /* get posix thread data */
  374. ptd = _pthread_get_data(rt_thread_self());
  375. RT_ASSERT(ptd != RT_NULL);
  376. if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
  377. {
  378. if (oldstate)
  379. *oldstate = ptd->cancelstate;
  380. ptd->cancelstate = state;
  381. return 0;
  382. }
  383. return EINVAL;
  384. }
  385. RTM_EXPORT(pthread_setcancelstate);
  386. int pthread_setcanceltype(int type, int *oldtype)
  387. {
  388. _pthread_data_t *ptd;
  389. /* get posix thread data */
  390. ptd = _pthread_get_data(rt_thread_self());
  391. RT_ASSERT(ptd != RT_NULL);
  392. if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
  393. return EINVAL;
  394. if (oldtype)
  395. *oldtype = ptd->canceltype;
  396. ptd->canceltype = type;
  397. return 0;
  398. }
  399. RTM_EXPORT(pthread_setcanceltype);
  400. void pthread_testcancel(void)
  401. {
  402. int cancel=0;
  403. _pthread_data_t* ptd;
  404. /* get posix thread data */
  405. ptd = _pthread_get_data(rt_thread_self());
  406. RT_ASSERT(ptd != RT_NULL);
  407. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  408. cancel = ptd->canceled;
  409. if (cancel)
  410. pthread_exit((void*)PTHREAD_CANCELED);
  411. }
  412. RTM_EXPORT(pthread_testcancel);
  413. int pthread_cancel(pthread_t thread)
  414. {
  415. _pthread_data_t *ptd;
  416. /* cancel self */
  417. if (thread == rt_thread_self())
  418. return 0;
  419. /* get posix thread data */
  420. ptd = _pthread_get_data(thread);
  421. RT_ASSERT(ptd != RT_NULL);
  422. /* set canceled */
  423. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  424. {
  425. ptd->canceled = 1;
  426. if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
  427. {
  428. /*
  429. * to detach thread.
  430. * this thread will be removed from scheduler list
  431. * and because there is a cleanup function in the
  432. * thread (pthread_cleanup), it will move to defunct
  433. * thread list and wait for handling in idle thread.
  434. */
  435. rt_thread_detach(thread);
  436. }
  437. }
  438. return 0;
  439. }
  440. RTM_EXPORT(pthread_cancel);