1
0

pthread.c 12 KB

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