pthread_create.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "pthread_impl.h"
  2. static void dummy_0()
  3. {
  4. }
  5. weak_alias(dummy_0, __rsyscall_lock);
  6. weak_alias(dummy_0, __rsyscall_unlock);
  7. weak_alias(dummy_0, __pthread_tsd_run_dtors);
  8. #ifdef __pthread_unwind_next
  9. #undef __pthread_unwind_next
  10. #define __pthread_unwind_next __pthread_unwind_next_3
  11. #endif
  12. void __pthread_unwind_next(struct __ptcb *cb)
  13. {
  14. pthread_t self = pthread_self();
  15. int n;
  16. if (cb->__next) {
  17. self->cancelbuf = cb->__next->__next;
  18. longjmp((void *)cb->__next->__jb, 1);
  19. }
  20. LOCK(&self->exitlock);
  21. __pthread_tsd_run_dtors();
  22. /* Mark this thread dead before decrementing count */
  23. self->dead = 1;
  24. do n = libc.threads_minus_1;
  25. while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
  26. if (!n) exit(0);
  27. if (self->detached && self->map_base) {
  28. __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (uint64_t[]){-1},0,8);
  29. __unmapself(self->map_base, self->map_size);
  30. }
  31. __syscall(SYS_exit, 0);
  32. }
  33. static int start(void *p)
  34. {
  35. struct pthread *self = p;
  36. if (self->unblock_cancel)
  37. __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
  38. pthread_exit(self->start(self->start_arg));
  39. return 0;
  40. }
  41. int __uniclone(void *, int (*)(), void *);
  42. #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
  43. /* pthread_key_create.c overrides this */
  44. static const size_t dummy = 0;
  45. weak_alias(dummy, __pthread_tsd_size);
  46. int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
  47. {
  48. int ret;
  49. size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
  50. size_t guard = DEFAULT_GUARD_SIZE;
  51. struct pthread *self = pthread_self(), *new;
  52. unsigned char *map, *stack, *tsd;
  53. if (!self) return ENOSYS;
  54. if (!libc.threaded) {
  55. __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
  56. libc.threaded = 1;
  57. }
  58. if (attr) {
  59. guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
  60. size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
  61. }
  62. size += __pthread_tsd_size;
  63. map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
  64. if (!map) return EAGAIN;
  65. if (guard) mprotect(map, guard, PROT_NONE);
  66. tsd = map + size - __pthread_tsd_size;
  67. new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
  68. new->map_base = map;
  69. new->map_size = size;
  70. new->pid = self->pid;
  71. new->errno_ptr = &new->errno_val;
  72. new->start = entry;
  73. new->start_arg = arg;
  74. new->self = new;
  75. new->tsd = (void *)tsd;
  76. if (attr) new->detached = attr->_a_detach;
  77. new->unblock_cancel = self->cancel;
  78. memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
  79. new->tlsdesc[1] = (uintptr_t)new;
  80. stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
  81. __rsyscall_lock();
  82. a_inc(&libc.threads_minus_1);
  83. ret = __uniclone(stack, start, new);
  84. __rsyscall_unlock();
  85. if (ret < 0) {
  86. a_dec(&libc.threads_minus_1);
  87. munmap(map, size);
  88. return EAGAIN;
  89. }
  90. *res = new;
  91. return 0;
  92. }
  93. void pthread_exit(void *result)
  94. {
  95. struct pthread *self = pthread_self();
  96. struct __ptcb cb = { .__next = self->cancelbuf };
  97. self->result = result;
  98. __pthread_unwind_next(&cb);
  99. }