pthread.c 10.0 KB

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