1
0

pthread.c 13 KB

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