1
0

pthread.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. /* detach joinable semaphore */
  156. rt_sem_delete(ptd->joinable_sem);
  157. ptd->joinable_sem = RT_NULL;
  158. rt_exit_critical();
  159. }
  160. return 0;
  161. }
  162. int pthread_join (pthread_t thread, void **value_ptr)
  163. {
  164. _pthread_data_t* ptd;
  165. rt_err_t result;
  166. if (thread == rt_thread_self())
  167. {
  168. /* join self */
  169. return EDEADLK;
  170. }
  171. ptd = _pthread_get_data(thread);
  172. if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
  173. return EINVAL; /* join on a detached pthread */
  174. result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
  175. if (result == RT_EOK)
  176. {
  177. /* get return value */
  178. if (value_ptr != RT_NULL) *value_ptr = ptd->return_value;
  179. /* release resource */
  180. pthread_detach(thread);
  181. }
  182. else return ESRCH;
  183. return 0;
  184. }
  185. void pthread_exit (void* value)
  186. {
  187. _pthread_data_t* ptd;
  188. _pthread_cleanup_t* cleanup;
  189. extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
  190. ptd = _pthread_get_data(rt_thread_self());
  191. rt_enter_critical();
  192. /* disable cancel */
  193. ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
  194. /* set return value */
  195. ptd->return_value = value;
  196. rt_exit_critical();
  197. /* invoke pushed cleanup */
  198. while (ptd->cleanup != RT_NULL)
  199. {
  200. cleanup = ptd->cleanup;
  201. ptd->cleanup = cleanup->next;
  202. cleanup->cleanup_func(cleanup->parameter);
  203. /* release this cleanup function */
  204. rt_free(cleanup);
  205. }
  206. /* destruct thread local key */
  207. if (ptd->tls != RT_NULL)
  208. {
  209. void* data;
  210. rt_uint32_t index;
  211. for (index = 0; index < PTHREAD_KEY_MAX; index ++)
  212. {
  213. if (_thread_keys[index].is_used)
  214. {
  215. data = ptd->tls[index];
  216. if (data)
  217. _thread_keys[index].destructor(data);
  218. }
  219. }
  220. /* release tls area */
  221. rt_free(ptd->tls);
  222. ptd->tls = RT_NULL;
  223. }
  224. if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
  225. {
  226. /* release the joinable pthread */
  227. rt_sem_release(ptd->joinable_sem);
  228. }
  229. /* detach thread */
  230. rt_thread_detach(ptd->tid);
  231. /* reschedule thread */
  232. rt_schedule();
  233. }
  234. int pthread_once(pthread_once_t * once_control, void (*init_routine) (void))
  235. {
  236. RT_ASSERT(once_control != RT_NULL);
  237. RT_ASSERT(init_routine != RT_NULL);
  238. rt_enter_critical();
  239. if (!(*once_control))
  240. {
  241. /* call routine once */
  242. *once_control = 1;
  243. rt_exit_critical();
  244. init_routine();
  245. }
  246. rt_exit_critical();
  247. return 0;
  248. }
  249. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
  250. {
  251. return ENOTSUP;
  252. }
  253. int pthread_kill(pthread_t thread, int sig)
  254. {
  255. return ENOTSUP;
  256. }
  257. void pthread_cleanup_pop(int execute)
  258. {
  259. _pthread_data_t* ptd;
  260. _pthread_cleanup_t* cleanup;
  261. /* get posix thread data */
  262. ptd = _pthread_get_data(rt_thread_self());
  263. RT_ASSERT(ptd != RT_NULL);
  264. if (execute)
  265. {
  266. rt_enter_critical();
  267. cleanup = ptd->cleanup;
  268. if (cleanup)
  269. ptd->cleanup = cleanup->next;
  270. rt_exit_critical();
  271. if (cleanup)
  272. {
  273. cleanup->cleanup_func(cleanup->parameter);
  274. rt_free(cleanup);
  275. }
  276. }
  277. }
  278. void pthread_cleanup_push(void (*routine)(void*), void *arg)
  279. {
  280. _pthread_data_t* ptd;
  281. _pthread_cleanup_t* cleanup;
  282. /* get posix thread data */
  283. ptd = _pthread_get_data(rt_thread_self());
  284. RT_ASSERT(ptd != RT_NULL);
  285. cleanup = (_pthread_cleanup_t*)rt_malloc(sizeof(_pthread_cleanup_t));
  286. if (cleanup != RT_NULL)
  287. {
  288. cleanup->cleanup_func = routine;
  289. cleanup->parameter = arg;
  290. rt_enter_critical();
  291. cleanup->next = ptd->cleanup;
  292. ptd->cleanup = cleanup;
  293. rt_exit_critical();
  294. }
  295. }
  296. /*
  297. * According to IEEE Std 1003.1, 2004 Edition , following pthreads
  298. * interface support cancellation point:
  299. * mq_receive()
  300. * mq_send()
  301. * mq_timedreceive()
  302. * mq_timedsend()
  303. * msgrcv()
  304. * msgsnd()
  305. * msync()
  306. * pthread_cond_timedwait()
  307. * pthread_cond_wait()
  308. * pthread_join()
  309. * pthread_testcancel()
  310. * sem_timedwait()
  311. * sem_wait()
  312. *
  313. * A cancellation point may also occur when a thread is
  314. * executing the following functions:
  315. * pthread_rwlock_rdlock()
  316. * pthread_rwlock_timedrdlock()
  317. * pthread_rwlock_timedwrlock()
  318. * pthread_rwlock_wrlock()
  319. *
  320. * The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
  321. * functions are defined to be async-cancel safe.
  322. */
  323. int pthread_setcancelstate(int state, int *oldstate)
  324. {
  325. _pthread_data_t* ptd;
  326. /* get posix thread data */
  327. ptd = _pthread_get_data(rt_thread_self());
  328. RT_ASSERT(ptd != RT_NULL);
  329. if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
  330. {
  331. if (oldstate) *oldstate = ptd->cancelstate;
  332. ptd->cancelstate = state;
  333. return 0;
  334. }
  335. return EINVAL;
  336. }
  337. int pthread_setcanceltype(int type, int *oldtype)
  338. {
  339. _pthread_data_t* ptd;
  340. /* get posix thread data */
  341. ptd = _pthread_get_data(rt_thread_self());
  342. RT_ASSERT(ptd != RT_NULL);
  343. if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
  344. return EINVAL;
  345. if (oldtype) *oldtype = ptd->canceltype;
  346. ptd->canceltype = type;
  347. return 0;
  348. }
  349. void pthread_testcancel(void)
  350. {
  351. int cancel=0;
  352. _pthread_data_t* ptd;
  353. /* get posix thread data */
  354. ptd = _pthread_get_data(rt_thread_self());
  355. RT_ASSERT(ptd != RT_NULL);
  356. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE) cancel = ptd->canceled;
  357. if (cancel) pthread_exit((void*)PTHREAD_CANCELED);
  358. }
  359. int pthread_cancel(pthread_t thread)
  360. {
  361. _pthread_data_t* ptd;
  362. /* cancel self */
  363. if (thread == rt_thread_self()) return 0;
  364. /* get posix thread data */
  365. ptd = _pthread_get_data(thread);
  366. RT_ASSERT(ptd != RT_NULL);
  367. /* set canceled */
  368. if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
  369. {
  370. ptd->canceled = 1;
  371. if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
  372. {
  373. /*
  374. * to detach thread.
  375. * this thread will be removed from scheduler list
  376. * and because there is a cleanup function in the
  377. * thread (pthread_cleanup), it will move to defunct
  378. * thread list and wait for handling in idle thread.
  379. */
  380. rt_thread_detach(thread);
  381. }
  382. }
  383. return 0;
  384. }