pthread_create.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #define _GNU_SOURCE
  2. #include "pthread_impl.h"
  3. #include "stdio_impl.h"
  4. #include "libc.h"
  5. #include <sys/mman.h>
  6. #include <string.h>
  7. void *__mmap(void *, size_t, int, int, int, off_t);
  8. int __munmap(void *, size_t);
  9. int __mprotect(void *, size_t, int);
  10. static void dummy_0()
  11. {
  12. }
  13. weak_alias(dummy_0, __acquire_ptc);
  14. weak_alias(dummy_0, __release_ptc);
  15. weak_alias(dummy_0, __pthread_tsd_run_dtors);
  16. weak_alias(dummy_0, __do_private_robust_list);
  17. weak_alias(dummy_0, __do_orphaned_stdio_locks);
  18. _Noreturn void __pthread_exit(void *result)
  19. {
  20. pthread_t self = __pthread_self();
  21. sigset_t set;
  22. self->result = result;
  23. while (self->cancelbuf) {
  24. void (*f)(void *) = self->cancelbuf->__f;
  25. void *x = self->cancelbuf->__x;
  26. self->cancelbuf = self->cancelbuf->__next;
  27. f(x);
  28. }
  29. __pthread_tsd_run_dtors();
  30. __lock(self->exitlock);
  31. /* Mark this thread dead before decrementing count */
  32. __lock(self->killlock);
  33. self->dead = 1;
  34. /* Block all signals before decrementing the live thread count.
  35. * This is important to ensure that dynamically allocated TLS
  36. * is not under-allocated/over-committed, and possibly for other
  37. * reasons as well. */
  38. __block_all_sigs(&set);
  39. /* Wait to unlock the kill lock, which governs functions like
  40. * pthread_kill which target a thread id, until signals have
  41. * been blocked. This precludes observation of the thread id
  42. * as a live thread (with application code running in it) after
  43. * the thread was reported dead by ESRCH being returned. */
  44. __unlock(self->killlock);
  45. /* It's impossible to determine whether this is "the last thread"
  46. * until performing the atomic decrement, since multiple threads
  47. * could exit at the same time. For the last thread, revert the
  48. * decrement and unblock signals to give the atexit handlers and
  49. * stdio cleanup code a consistent state. */
  50. if (a_fetch_add(&libc.threads_minus_1, -1)==0) {
  51. libc.threads_minus_1 = 0;
  52. __restore_sigs(&set);
  53. exit(0);
  54. }
  55. if (self->locale != &libc.global_locale) {
  56. a_dec(&libc.uselocale_cnt);
  57. if (self->locale->ctype_utf8)
  58. a_dec(&libc.bytelocale_cnt_minus_1);
  59. }
  60. __do_private_robust_list();
  61. __do_orphaned_stdio_locks();
  62. if (self->detached && self->map_base) {
  63. /* Detached threads must avoid the kernel clear_child_tid
  64. * feature, since the virtual address will have been
  65. * unmapped and possibly already reused by a new mapping
  66. * at the time the kernel would perform the write. In
  67. * the case of threads that started out detached, the
  68. * initial clone flags are correct, but if the thread was
  69. * detached later (== 2), we need to clear it here. */
  70. if (self->detached == 2) __syscall(SYS_set_tid_address, 0);
  71. /* The following call unmaps the thread's stack mapping
  72. * and then exits without touching the stack. */
  73. __unmapself(self->map_base, self->map_size);
  74. }
  75. for (;;) __syscall(SYS_exit, 0);
  76. }
  77. void __do_cleanup_push(struct __ptcb *cb)
  78. {
  79. if (!libc.has_thread_pointer) return;
  80. struct pthread *self = __pthread_self();
  81. cb->__next = self->cancelbuf;
  82. self->cancelbuf = cb;
  83. }
  84. void __do_cleanup_pop(struct __ptcb *cb)
  85. {
  86. if (!libc.has_thread_pointer) return;
  87. __pthread_self()->cancelbuf = cb->__next;
  88. }
  89. static int start(void *p)
  90. {
  91. pthread_t self = p;
  92. if (self->startlock[0]) {
  93. __wait(self->startlock, 0, 1, 1);
  94. if (self->startlock[0]) {
  95. self->detached = 2;
  96. pthread_exit(0);
  97. }
  98. __restore_sigs(self->sigmask);
  99. }
  100. if (self->unblock_cancel)
  101. __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
  102. SIGPT_SET, 0, _NSIG/8);
  103. __pthread_exit(self->start(self->start_arg));
  104. return 0;
  105. }
  106. static int start_c11(void *p)
  107. {
  108. pthread_t self = p;
  109. int (*start)(void*) = (int(*)(void*)) self->start;
  110. __pthread_exit((void *)(uintptr_t)start(self->start_arg));
  111. return 0;
  112. }
  113. #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
  114. /* pthread_key_create.c overrides this */
  115. static volatile size_t dummy = 0;
  116. weak_alias(dummy, __pthread_tsd_size);
  117. static void *dummy_tsd[1] = { 0 };
  118. weak_alias(dummy_tsd, __pthread_tsd_main);
  119. static FILE *volatile dummy_file = 0;
  120. weak_alias(dummy_file, __stdin_used);
  121. weak_alias(dummy_file, __stdout_used);
  122. weak_alias(dummy_file, __stderr_used);
  123. static void init_file_lock(FILE *f)
  124. {
  125. if (f && f->lock<0) f->lock = 0;
  126. }
  127. void *__copy_tls(unsigned char *);
  128. int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
  129. {
  130. int ret, c11 = (attrp == __ATTRP_C11_THREAD);
  131. size_t size, guard;
  132. struct pthread *self, *new;
  133. unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
  134. unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
  135. | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
  136. | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
  137. int do_sched = 0;
  138. pthread_attr_t attr = {0};
  139. if (!libc.can_do_threads) return ENOSYS;
  140. self = __pthread_self();
  141. if (!libc.threaded) {
  142. for (FILE *f=libc.ofl_head; f; f=f->next)
  143. init_file_lock(f);
  144. init_file_lock(__stdin_used);
  145. init_file_lock(__stdout_used);
  146. init_file_lock(__stderr_used);
  147. __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
  148. self->tsd = (void **)__pthread_tsd_main;
  149. libc.threaded = 1;
  150. }
  151. if (attrp && !c11) attr = *attrp;
  152. __acquire_ptc();
  153. if (attr._a_stackaddr) {
  154. size_t need = libc.tls_size + __pthread_tsd_size;
  155. size = attr._a_stacksize + DEFAULT_STACK_SIZE;
  156. stack = (void *)(attr._a_stackaddr & -16);
  157. stack_limit = (void *)(attr._a_stackaddr - size);
  158. /* Use application-provided stack for TLS only when
  159. * it does not take more than ~12% or 2k of the
  160. * application's stack space. */
  161. if (need < size/8 && need < 2048) {
  162. tsd = stack - __pthread_tsd_size;
  163. stack = tsd - libc.tls_size;
  164. memset(stack, 0, need);
  165. } else {
  166. size = ROUND(need);
  167. guard = 0;
  168. }
  169. } else {
  170. guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
  171. size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize
  172. + libc.tls_size + __pthread_tsd_size);
  173. }
  174. if (!tsd) {
  175. if (guard) {
  176. map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
  177. if (map == MAP_FAILED) goto fail;
  178. if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
  179. __munmap(map, size);
  180. goto fail;
  181. }
  182. } else {
  183. map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
  184. if (map == MAP_FAILED) goto fail;
  185. }
  186. tsd = map + size - __pthread_tsd_size;
  187. if (!stack) {
  188. stack = tsd - libc.tls_size;
  189. stack_limit = map + guard;
  190. }
  191. }
  192. new = __copy_tls(tsd - libc.tls_size);
  193. new->map_base = map;
  194. new->map_size = size;
  195. new->stack = stack;
  196. new->stack_size = stack - stack_limit;
  197. new->start = entry;
  198. new->start_arg = arg;
  199. new->self = new;
  200. new->tsd = (void *)tsd;
  201. new->locale = &libc.global_locale;
  202. if (attr._a_detach) {
  203. new->detached = 1;
  204. flags -= CLONE_CHILD_CLEARTID;
  205. }
  206. if (attr._a_sched) {
  207. do_sched = new->startlock[0] = 1;
  208. __block_app_sigs(new->sigmask);
  209. }
  210. new->unblock_cancel = self->cancel;
  211. new->canary = self->canary;
  212. a_inc(&libc.threads_minus_1);
  213. ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
  214. __release_ptc();
  215. if (do_sched) {
  216. __restore_sigs(new->sigmask);
  217. }
  218. if (ret < 0) {
  219. a_dec(&libc.threads_minus_1);
  220. if (map) __munmap(map, size);
  221. return EAGAIN;
  222. }
  223. if (do_sched) {
  224. ret = __syscall(SYS_sched_setscheduler, new->tid,
  225. attr._a_policy, &attr._a_prio);
  226. a_store(new->startlock, ret<0 ? 2 : 0);
  227. __wake(new->startlock, 1, 1);
  228. if (ret < 0) return -ret;
  229. }
  230. *res = new;
  231. return 0;
  232. fail:
  233. __release_ptc();
  234. return EAGAIN;
  235. }
  236. weak_alias(__pthread_exit, pthread_exit);
  237. weak_alias(__pthread_create, pthread_create);