12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- #include <assert.h>
- #include <errno.h>
- #include <signal.h>
- #include <stdint.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <sys/param.h>
- #include <tls.h>
- #include <lowlevellock.h>
- #include <link.h>
- #include <bits/kernel-features.h>
- #ifndef NEED_SEPARATE_REGISTER_STACK
- # define STACK_VARIABLES void *stackaddr = NULL
- # define STACK_VARIABLES_ARGS stackaddr
- # define STACK_VARIABLES_PARMS void *stackaddr
- # define ALLOCATE_STACK_PARMS void **stack
- # define ALLOCATE_STACK(attr, pd) allocate_stack (attr, pd, &stackaddr)
- #else
- # define STACK_VARIABLES void *stackaddr = NULL; size_t stacksize = 0
- # define STACK_VARIABLES_ARGS stackaddr, stacksize
- # define STACK_VARIABLES_PARMS void *stackaddr, size_t stacksize
- # define ALLOCATE_STACK_PARMS void **stack, size_t *stacksize
- # define ALLOCATE_STACK(attr, pd) \
- allocate_stack (attr, pd, &stackaddr, &stacksize)
- #endif
- #ifndef STACK_ALIGN
- # define STACK_ALIGN __alignof__ (long double)
- #endif
- #ifndef MINIMAL_REST_STACK
- # define MINIMAL_REST_STACK 4096
- #endif
- #ifndef MAP_STACK
- # define MAP_STACK 0
- #endif
- #if defined(TLS_TCB_AT_TP)
- # define TLS_TPADJ(pd) (pd)
- #elif defined(TLS_DTV_AT_TP)
- # define TLS_TPADJ(pd) ((struct pthread *)((char *) (pd) + TLS_PRE_TCB_SIZE))
- #endif
- static size_t stack_cache_maxsize = 16 * 1024 * 1024;
- static size_t stack_cache_actsize;
- static int stack_cache_lock = LLL_LOCK_INITIALIZER;
- static LIST_HEAD (stack_cache);
- static LIST_HEAD (stack_used);
- static uintptr_t in_flight_stack;
- list_t __stack_user __attribute__ ((nocommon));
- hidden_data_def (__stack_user)
- #if defined COLORING_INCREMENT && COLORING_INCREMENT != 0
- static unsigned int nptl_ncreated;
- #endif
- #define FREE_P(descr) ((descr)->tid <= 0)
- static void
- stack_list_del (list_t *elem)
- {
- in_flight_stack = (uintptr_t) elem;
- atomic_write_barrier ();
- list_del (elem);
- atomic_write_barrier ();
- in_flight_stack = 0;
- }
- static void
- stack_list_add (list_t *elem, list_t *list)
- {
- in_flight_stack = (uintptr_t) elem | 1;
- atomic_write_barrier ();
- list_add (elem, list);
- atomic_write_barrier ();
- in_flight_stack = 0;
- }
- static struct pthread *
- get_cached_stack (size_t *sizep, void **memp)
- {
- size_t size = *sizep;
- struct pthread *result = NULL;
- list_t *entry;
- lll_lock (stack_cache_lock, LLL_PRIVATE);
-
- list_for_each (entry, &stack_cache)
- {
- struct pthread *curr;
- curr = list_entry (entry, struct pthread, list);
- if (FREE_P (curr) && curr->stackblock_size >= size)
- {
- if (curr->stackblock_size == size)
- {
- result = curr;
- break;
- }
- if (result == NULL
- || result->stackblock_size > curr->stackblock_size)
- result = curr;
- }
- }
- if (__builtin_expect (result == NULL, 0)
-
- || __builtin_expect (result->stackblock_size > 4 * size, 0))
- {
-
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
- return NULL;
- }
-
- stack_list_del (&result->list);
-
- stack_list_add (&result->list, &stack_used);
-
- stack_cache_actsize -= result->stackblock_size;
-
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
-
- *sizep = result->stackblock_size;
- *memp = result->stackblock;
-
- result->cancelhandling = 0;
- result->cleanup = NULL;
-
- result->nextevent = NULL;
-
- dtv_t *dtv = GET_DTV (TLS_TPADJ (result));
- memset (dtv, '\0', (dtv[-1].counter + 1) * sizeof (dtv_t));
-
- _dl_allocate_tls_init (TLS_TPADJ (result));
- return result;
- }
- void
- __free_stacks (size_t limit)
- {
-
- list_t *entry;
- list_t *prev;
-
- list_for_each_prev_safe (entry, prev, &stack_cache)
- {
- struct pthread *curr;
- curr = list_entry (entry, struct pthread, list);
- if (FREE_P (curr))
- {
-
- stack_list_del (entry);
-
- stack_cache_actsize -= curr->stackblock_size;
-
- _dl_deallocate_tls (TLS_TPADJ (curr), false);
-
- if (munmap (curr->stackblock, curr->stackblock_size) != 0)
- abort ();
-
- if (stack_cache_actsize <= limit)
- break;
- }
- }
- }
- static inline void
- __attribute ((always_inline))
- queue_stack (struct pthread *stack)
- {
-
- stack_list_add (&stack->list, &stack_cache);
- stack_cache_actsize += stack->stackblock_size;
- if (__builtin_expect (stack_cache_actsize > stack_cache_maxsize, 0))
- __free_stacks (stack_cache_maxsize);
- }
- static int
- internal_function
- change_stack_perm (struct pthread *pd
- #ifdef NEED_SEPARATE_REGISTER_STACK
- , size_t pagemask
- #endif
- )
- {
- #ifdef NEED_SEPARATE_REGISTER_STACK
- void *stack = (pd->stackblock
- + (((((pd->stackblock_size - pd->guardsize) / 2)
- & pagemask) + pd->guardsize) & pagemask));
- size_t len = pd->stackblock + pd->stackblock_size - stack;
- #elif defined _STACK_GROWS_DOWN
- void *stack = pd->stackblock + pd->guardsize;
- size_t len = pd->stackblock_size - pd->guardsize;
- #elif defined _STACK_GROWS_UP
- void *stack = pd->stackblock;
- size_t len = (uintptr_t) pd - pd->guardsize - (uintptr_t) pd->stackblock;
- #else
- # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
- #endif
- #ifdef __ARCH_USE_MMU__
- if (mprotect (stack, len, PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
- return errno;
- #endif
- return 0;
- }
- static int
- allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
- ALLOCATE_STACK_PARMS)
- {
- struct pthread *pd;
- size_t size;
- size_t pagesize_m1 = __getpagesize () - 1;
- void *stacktop;
- assert (attr != NULL);
- assert (powerof2 (pagesize_m1 + 1));
- assert (TCB_ALIGNMENT >= STACK_ALIGN);
-
- size = attr->stacksize ?: __default_stacksize;
-
- if (__builtin_expect (attr->flags & ATTR_FLAG_STACKADDR, 0))
- {
- uintptr_t adj;
-
- if (attr->stacksize != 0
- && attr->stacksize < (__static_tls_size + MINIMAL_REST_STACK))
- return EINVAL;
-
- #if defined(TLS_TCB_AT_TP)
- adj = ((uintptr_t) attr->stackaddr - TLS_TCB_SIZE)
- & __static_tls_align_m1;
- assert (size > adj + TLS_TCB_SIZE);
- #elif defined(TLS_DTV_AT_TP)
- adj = ((uintptr_t) attr->stackaddr - __static_tls_size)
- & __static_tls_align_m1;
- assert (size > adj);
- #endif
-
- #if defined(TLS_TCB_AT_TP)
- pd = (struct pthread *) ((uintptr_t) attr->stackaddr
- - TLS_TCB_SIZE - adj);
- #elif defined(TLS_DTV_AT_TP)
- pd = (struct pthread *) (((uintptr_t) attr->stackaddr
- - __static_tls_size - adj)
- - TLS_PRE_TCB_SIZE);
- #endif
-
- memset (pd, '\0', sizeof (struct pthread));
-
- pd->specific[0] = pd->specific_1stblock;
-
- pd->stackblock = (char *) attr->stackaddr - size;
- pd->stackblock_size = size;
-
- pd->user_stack = true;
-
- pd->header.multiple_threads = 1;
- #ifndef TLS_MULTIPLE_THREADS_IN_TCB
- __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
- #endif
- #ifndef __ASSUME_PRIVATE_FUTEX
-
- pd->header.private_futex = THREAD_GETMEM (THREAD_SELF,
- header.private_futex);
- #endif
- #ifdef NEED_DL_SYSINFO
-
- THREAD_SYSINFO(pd) = THREAD_SELF_SYSINFO;
- #endif
-
- if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
- {
-
- assert (errno == ENOMEM);
- return EAGAIN;
- }
-
- lll_lock (stack_cache_lock, LLL_PRIVATE);
-
- list_add (&pd->list, &__stack_user);
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
- }
- else
- {
-
- size_t guardsize;
- size_t reqsize;
- void *mem = 0;
- const int prot = (PROT_READ | PROT_WRITE);
- #if defined COLORING_INCREMENT && COLORING_INCREMENT != 0
-
- if (size <= 16 * pagesize_m1)
- size += pagesize_m1 + 1;
- #endif
-
- size &= ~__static_tls_align_m1;
- assert (size != 0);
-
- guardsize = (attr->guardsize + pagesize_m1) & ~pagesize_m1;
- if (__builtin_expect (size < ((guardsize + __static_tls_size
- + MINIMAL_REST_STACK + pagesize_m1)
- & ~pagesize_m1),
- 0))
-
- return EINVAL;
-
- reqsize = size;
- pd = get_cached_stack (&size, &mem);
- if (pd == NULL)
- {
-
- #if defined MULTI_PAGE_ALIASING && MULTI_PAGE_ALIASING != 0
- if ((size % MULTI_PAGE_ALIASING) == 0)
- size += pagesize_m1 + 1;
- #endif
- mem = mmap (NULL, size, prot,
- MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
- if (__builtin_expect (mem == MAP_FAILED, 0))
- {
- if (errno == ENOMEM)
- __set_errno (EAGAIN);
- return errno;
- }
-
- assert (mem != NULL);
- #if defined COLORING_INCREMENT && COLORING_INCREMENT != 0
-
- unsigned int ncreated = atomic_increment_val (&nptl_ncreated);
-
- size_t coloring = (ncreated * COLORING_INCREMENT) & pagesize_m1;
-
- if (__builtin_expect ((coloring & __static_tls_align_m1) != 0, 0))
- coloring = (((coloring + __static_tls_align_m1)
- & ~(__static_tls_align_m1))
- & ~pagesize_m1);
- #else
-
- # define coloring 0
- #endif
-
- #if defined(TLS_TCB_AT_TP)
- pd = (struct pthread *) ((char *) mem + size - coloring) - 1;
- #elif defined(TLS_DTV_AT_TP)
- pd = (struct pthread *) ((((uintptr_t) mem + size - coloring
- - __static_tls_size)
- & ~__static_tls_align_m1)
- - TLS_PRE_TCB_SIZE);
- #endif
-
- pd->stackblock = mem;
- pd->stackblock_size = size;
-
- pd->specific[0] = pd->specific_1stblock;
-
- pd->header.multiple_threads = 1;
- #ifndef TLS_MULTIPLE_THREADS_IN_TCB
- __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
- #endif
- #ifndef __ASSUME_PRIVATE_FUTEX
-
- pd->header.private_futex = THREAD_GETMEM (THREAD_SELF,
- header.private_futex);
- #endif
- #ifdef NEED_DL_SYSINFO
-
- THREAD_SYSINFO(pd) = THREAD_SELF_SYSINFO;
- #endif
-
- if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
- {
-
- assert (errno == ENOMEM);
-
- (void) munmap (mem, size);
- return EAGAIN;
- }
-
- lll_lock (stack_cache_lock, LLL_PRIVATE);
-
- stack_list_add (&pd->list, &stack_used);
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
-
- }
-
- if (__builtin_expect (guardsize > pd->guardsize, 0))
- {
- #ifdef NEED_SEPARATE_REGISTER_STACK
- char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
- #elif defined _STACK_GROWS_DOWN
- char *guard = mem;
- #elif defined _STACK_GROWS_UP
- char *guard = (char *) (((uintptr_t) pd - guardsize) & ~pagesize_m1);
- #endif
- #ifdef __ARCH_USE_MMU__
- if (mprotect (guard, guardsize, PROT_NONE) != 0)
- {
- int err;
- #ifdef NEED_SEPARATE_REGISTER_STACK
- mprot_error:
- #endif
- err = errno;
- lll_lock (stack_cache_lock, LLL_PRIVATE);
-
- stack_list_del (&pd->list);
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
-
- _dl_deallocate_tls (TLS_TPADJ (pd), false);
-
- (void) munmap (mem, size);
- return err;
- }
- #endif
- pd->guardsize = guardsize;
- }
- else if (__builtin_expect (pd->guardsize - guardsize > size - reqsize,
- 0))
- {
-
- #ifdef NEED_SEPARATE_REGISTER_STACK
- char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
- char *oldguard = mem + (((size - pd->guardsize) / 2) & ~pagesize_m1);
- #ifdef __ARCH_USE_MMU__
- if (oldguard < guard
- && mprotect (oldguard, guard - oldguard, prot) != 0)
- goto mprot_error;
- if (mprotect (guard + guardsize,
- oldguard + pd->guardsize - guard - guardsize,
- prot) != 0)
- goto mprot_error;
- #elif defined _STACK_GROWS_DOWN
- if (mprotect ((char *) mem + guardsize, pd->guardsize - guardsize,
- prot) != 0)
- goto mprot_error;
- #elif defined _STACK_GROWS_UP
- if (mprotect ((char *) (((uintptr_t) pd - pd->guardsize) & ~pagesize_m1),
- pd->guardsize - guardsize, prot) != 0)
- goto mprot_error;
- #endif
- #endif
- pd->guardsize = guardsize;
- }
-
- pd->reported_guardsize = guardsize;
- }
-
- pd->lock = LLL_LOCK_INITIALIZER;
-
- pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
- - offsetof (pthread_mutex_t,
- __data.__list.__next));
- pd->robust_head.list_op_pending = NULL;
- #ifdef __PTHREAD_MUTEX_HAVE_PREV
- pd->robust_prev = &pd->robust_head;
- #endif
- pd->robust_head.list = &pd->robust_head;
-
- *pdp = pd;
- #if defined(TLS_TCB_AT_TP)
-
- stacktop = ((char *) (pd + 1) - __static_tls_size);
- #elif defined(TLS_DTV_AT_TP)
- stacktop = (char *) (pd - 1);
- #endif
- #ifdef NEED_SEPARATE_REGISTER_STACK
- *stack = pd->stackblock;
- *stacksize = stacktop - *stack;
- #elif defined _STACK_GROWS_DOWN
- *stack = stacktop;
- #elif defined _STACK_GROWS_UP
- *stack = pd->stackblock;
- assert (*stack > 0);
- #endif
- return 0;
- }
- void
- internal_function
- __deallocate_stack (struct pthread *pd)
- {
- lll_lock (stack_cache_lock, LLL_PRIVATE);
-
- stack_list_del (&pd->list);
-
- if (__builtin_expect (! pd->user_stack, 1))
- (void) queue_stack (pd);
- else
-
- _dl_deallocate_tls (TLS_TPADJ (pd), false);
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
- }
- int
- internal_function
- __make_stacks_executable (void **stack_endp)
- {
-
- int err = EPERM;
- if (err != 0)
- return err;
- #ifdef NEED_SEPARATE_REGISTER_STACK
- const size_t pagemask = ~(__getpagesize () - 1);
- #endif
- lll_lock (stack_cache_lock, LLL_PRIVATE);
- list_t *runp;
- list_for_each (runp, &stack_used)
- {
- err = change_stack_perm (list_entry (runp, struct pthread, list)
- #ifdef NEED_SEPARATE_REGISTER_STACK
- , pagemask
- #endif
- );
- if (err != 0)
- break;
- }
-
- if (err == 0)
- list_for_each (runp, &stack_cache)
- {
- err = change_stack_perm (list_entry (runp, struct pthread, list)
- #ifdef NEED_SEPARATE_REGISTER_STACK
- , pagemask
- #endif
- );
- if (err != 0)
- break;
- }
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
- return err;
- }
- void
- __reclaim_stacks (void)
- {
- struct pthread *self = (struct pthread *) THREAD_SELF;
-
- if (in_flight_stack != 0)
- {
- bool add_p = in_flight_stack & 1;
- list_t *elem = (list_t *)(uintptr_t)(in_flight_stack & ~UINTMAX_C (1));
- if (add_p)
- {
-
- int check_list (list_t *l)
- {
- if (l->next->prev != l)
- {
- assert (l->next->prev == elem);
- elem->next = l->next;
- elem->prev = l;
- l->next = elem;
- return 1;
- }
- return 0;
- }
- if (check_list (&stack_used) == 0)
- (void) check_list (&stack_cache);
- }
- else
- {
-
- elem->next->prev = elem->prev;
- elem->prev->next = elem->next;
- }
- }
-
- list_t *runp;
- list_for_each (runp, &stack_used)
- {
- struct pthread *curp = list_entry (runp, struct pthread, list);
- if (curp != self)
- {
-
- curp->tid = 0;
-
- stack_cache_actsize += curp->stackblock_size;
- if (curp->specific_used)
- {
-
- memset (curp->specific_1stblock, '\0',
- sizeof (curp->specific_1stblock));
- curp->specific_used = false;
- size_t cnt;
- for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
- if (curp->specific[cnt] != NULL)
- {
- memset (curp->specific[cnt], '\0',
- sizeof (curp->specific_1stblock));
-
- curp->specific_used = true;
- }
- }
- }
- }
-
- list_splice (&stack_used, &stack_cache);
-
- stack_list_del (&self->list);
-
- INIT_LIST_HEAD (&stack_used);
- INIT_LIST_HEAD (&__stack_user);
- if (__builtin_expect (THREAD_GETMEM (self, user_stack), 0))
- list_add (&self->list, &__stack_user);
- else
- list_add (&self->list, &stack_used);
-
- __nptl_nthreads = 1;
- in_flight_stack = 0;
-
- stack_cache_lock = LLL_LOCK_INITIALIZER;
- }
- static void
- internal_function
- setxid_mark_thread (struct xid_command *cmdp, struct pthread *t)
- {
- int ch;
-
- t->setxid_futex = 0;
- do
- {
- ch = t->cancelhandling;
-
- if ((ch & EXITING_BITMASK) != 0)
- return;
- }
- while (atomic_compare_and_exchange_bool_acq (&t->cancelhandling,
- ch | SETXID_BITMASK, ch));
- }
- static void
- internal_function
- setxid_unmark_thread (struct xid_command *cmdp, struct pthread *t)
- {
- int ch;
- do
- {
- ch = t->cancelhandling;
- if ((ch & SETXID_BITMASK) == 0)
- return;
- }
- while (atomic_compare_and_exchange_bool_acq (&t->cancelhandling,
- ch & ~SETXID_BITMASK, ch));
-
- t->setxid_futex = 1;
- lll_futex_wake (&t->setxid_futex, 1, LLL_PRIVATE);
- }
- static int
- internal_function
- setxid_signal_thread (struct xid_command *cmdp, struct pthread *t)
- {
- if ((t->cancelhandling & SETXID_BITMASK) == 0)
- return 0;
- int val;
- pid_t pid = getpid ();
- INTERNAL_SYSCALL_DECL (err);
- val = INTERNAL_SYSCALL (tgkill, err, 3, pid, t->tid, SIGSETXID);
-
- if (!INTERNAL_SYSCALL_ERROR_P (val, err))
- {
- atomic_increment (&cmdp->cntr);
- return 1;
- }
- else
- return 0;
- }
- int
- attribute_hidden
- __nptl_setxid (struct xid_command *cmdp)
- {
- int signalled;
- int result;
- lll_lock (stack_cache_lock, LLL_PRIVATE);
- __xidcmd = cmdp;
- cmdp->cntr = 0;
- struct pthread *self = THREAD_SELF;
-
- list_t *runp;
- list_for_each (runp, &stack_used)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self)
- continue;
- setxid_mark_thread (cmdp, t);
- }
-
- list_for_each (runp, &__stack_user)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self)
- continue;
- setxid_mark_thread (cmdp, t);
- }
-
- do
- {
- signalled = 0;
- list_for_each (runp, &stack_used)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self)
- continue;
- signalled += setxid_signal_thread (cmdp, t);
- }
- list_for_each (runp, &__stack_user)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self)
- continue;
- signalled += setxid_signal_thread (cmdp, t);
- }
- int cur = cmdp->cntr;
- while (cur != 0)
- {
- lll_futex_wait (&cmdp->cntr, cur, LLL_PRIVATE);
- cur = cmdp->cntr;
- }
- }
- while (signalled != 0);
-
- list_for_each (runp, &stack_used)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self)
- continue;
- setxid_unmark_thread (cmdp, t);
- }
- list_for_each (runp, &__stack_user)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self)
- continue;
- setxid_unmark_thread (cmdp, t);
- }
-
- INTERNAL_SYSCALL_DECL (err);
- result = INTERNAL_SYSCALL_NCS (cmdp->syscall_no, err, 3,
- cmdp->id[0], cmdp->id[1], cmdp->id[2]);
- if (INTERNAL_SYSCALL_ERROR_P (result, err))
- {
- __set_errno (INTERNAL_SYSCALL_ERRNO (result, err));
- result = -1;
- }
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
- return result;
- }
- static inline void __attribute__((always_inline))
- init_one_static_tls (struct pthread *curp, struct link_map *map)
- {
- dtv_t *dtv = GET_DTV (TLS_TPADJ (curp));
- # if defined(TLS_TCB_AT_TP)
- void *dest = (char *) curp - map->l_tls_offset;
- # elif defined(TLS_DTV_AT_TP)
- void *dest = (char *) curp + map->l_tls_offset + TLS_PRE_TCB_SIZE;
- # else
- # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
- # endif
-
- dtv[map->l_tls_modid].pointer.val = dest;
- dtv[map->l_tls_modid].pointer.is_static = true;
-
- memset (mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
- '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
- }
- void
- attribute_hidden
- __pthread_init_static_tls (struct link_map *map)
- {
- lll_lock (stack_cache_lock, LLL_PRIVATE);
-
- list_t *runp;
- list_for_each (runp, &stack_used)
- init_one_static_tls (list_entry (runp, struct pthread, list), map);
-
- list_for_each (runp, &__stack_user)
- init_one_static_tls (list_entry (runp, struct pthread, list), map);
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
- }
- void
- attribute_hidden
- __wait_lookup_done (void)
- {
- lll_lock (stack_cache_lock, LLL_PRIVATE);
- struct pthread *self = THREAD_SELF;
-
- list_t *runp;
- list_for_each (runp, &stack_used)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
- continue;
- int *const gscope_flagp = &t->header.gscope_flag;
-
- if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
- THREAD_GSCOPE_FLAG_WAIT,
- THREAD_GSCOPE_FLAG_USED))
- continue;
- do
- lll_futex_wait (gscope_flagp, THREAD_GSCOPE_FLAG_WAIT, LLL_PRIVATE);
- while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
- }
-
- list_for_each (runp, &__stack_user)
- {
- struct pthread *t = list_entry (runp, struct pthread, list);
- if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
- continue;
- int *const gscope_flagp = &t->header.gscope_flag;
-
- if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
- THREAD_GSCOPE_FLAG_WAIT,
- THREAD_GSCOPE_FLAG_USED))
- continue;
- do
- lll_futex_wait (gscope_flagp, THREAD_GSCOPE_FLAG_WAIT, LLL_PRIVATE);
- while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
- }
- lll_unlock (stack_cache_lock, LLL_PRIVATE);
- }
|