pthread.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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_STAT_MASK)== 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. #ifdef RT_USING_SIGNALS
  300. return rt_thread_kill(thread, sig);
  301. #else
  302. return ENOSYS;
  303. #endif
  304. }
  305. RTM_EXPORT(pthread_kill);
  306. #ifdef RT_USING_SIGNALS
  307. int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
  308. {
  309. return sigprocmask(how, set, oset);
  310. }
  311. #endif
  312. void pthread_cleanup_pop(int execute)
  313. {
  314. _pthread_data_t *ptd;
  315. _pthread_cleanup_t *cleanup;
  316. /* get posix thread data */
  317. ptd = _pthread_get_data(rt_thread_self());
  318. RT_ASSERT(ptd != RT_NULL);
  319. if (execute)
  320. {
  321. rt_enter_critical();
  322. cleanup = ptd->cleanup;
  323. if (cleanup)
  324. ptd->cleanup = cleanup->next;
  325. rt_exit_critical();
  326. if (cleanup)
  327. {
  328. cleanup->cleanup_func(cleanup->parameter);
  329. rt_free(cleanup);
  330. }
  331. }
  332. }
  333. RTM_EXPORT(pthread_cleanup_pop);
  334. void pthread_cleanup_push(void (*routine)(void*), void *arg)
  335. {
  336. _pthread_data_t *ptd;
  337. _pthread_cleanup_t *cleanup;
  338. /* get posix thread data */
  339. ptd = _pthread_get_data(rt_thread_self());
  340. RT_ASSERT(ptd != RT_NULL);
  341. cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
  342. if (cleanup != RT_NULL)
  343. {
  344. cleanup->cleanup_func = routine;
  345. cleanup->parameter = arg;
  346. rt_enter_critical();
  347. cleanup->next = ptd->cleanup;
  348. ptd->cleanup = cleanup;
  349. rt_exit_critical();
  350. }
  351. }
  352. RTM_EXPORT(pthread_cleanup_push);
  353. /*
  354. * According to IEEE Std 1003.1, 2004 Edition , following pthreads
  355. * interface support cancellation point:
  356. * mq_receive()
  357. * mq_send()
  358. * mq_timedreceive()
  359. * mq_timedsend()
  360. * msgrcv()
  361. * msgsnd()
  362. * msync()
  363. * pthread_cond_timedwait()
  364. * pthread_cond_wait()
  365. * pthread_join()
  366. * pthread_testcancel()
  367. * sem_timedwait()
  368. * sem_wait()
  369. *
  370. * A cancellation point may also occur when a thread is
  371. * executing the following functions:
  372. * pthread_rwlock_rdlock()
  373. * pthread_rwlock_timedrdlock()
  374. * pthread_rwlock_timedwrlock()
  375. * pthread_rwlock_wrlock()
  376. *
  377. * The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
  378. * functions are defined to be async-cancel safe.
  379. */
  380. int pthread_setcancelstate(int state, int *oldstate)
  381. {
  382. _pthread_data_t *ptd;
  383. /* get posix thread data */
  384. ptd = _pthread_get_data(rt_thread_self());
  385. RT_ASSERT(ptd != RT_NULL);
  386. if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
  387. {
  388. if (oldstate)
  389. *oldstate = ptd->cancelstate;
  390. ptd->cancelstate = state;
  391. return 0;
  392. }
  393. return EINVAL;
  394. }
  395. RTM_EXPORT(pthread_setcancelstate);
  396. int pthread_setcanceltype(int type, int *oldtype)
  397. {
  398. _pthread_data_t *ptd;
  399. /* get posix thread data */
  400. ptd = _pthread_get_data(rt_thread_self());
  401. RT_ASSERT(ptd != RT_NULL);
  402. if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
  403. return EINVAL;
  404. if (oldtype)
  405. *oldtype = ptd->canceltype;
  406. ptd->canceltype = type;
  407. return 0;
  408. }
  409. RTM_EXPORT(pthread_setcanceltype);
  410. void pthread_testcancel(void)
  411. {
  412. int cancel=0;
  413. _pthread_data_t* ptd;
  414. /* get posix thread data */
  415. ptd = _pthread_get_data(rt_thread_self());
  416. RT_ASSERT(ptd != RT_NULL);
  417. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  418. cancel = ptd->canceled;
  419. if (cancel)
  420. pthread_exit((void*)PTHREAD_CANCELED);
  421. }
  422. RTM_EXPORT(pthread_testcancel);
  423. int pthread_cancel(pthread_t thread)
  424. {
  425. _pthread_data_t *ptd;
  426. /* cancel self */
  427. if (thread == rt_thread_self())
  428. return 0;
  429. /* get posix thread data */
  430. ptd = _pthread_get_data(thread);
  431. RT_ASSERT(ptd != RT_NULL);
  432. /* set canceled */
  433. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  434. {
  435. ptd->canceled = 1;
  436. if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
  437. {
  438. /*
  439. * to detach thread.
  440. * this thread will be removed from scheduler list
  441. * and because there is a cleanup function in the
  442. * thread (pthread_cleanup), it will move to defunct
  443. * thread list and wait for handling in idle thread.
  444. */
  445. rt_thread_detach(thread);
  446. }
  447. }
  448. return 0;
  449. }
  450. RTM_EXPORT(pthread_cancel);