1
0

pthread.c 9.9 KB

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