summaryrefslogtreecommitdiff
path: root/src/thread
AgeCommit message (Collapse)AuthorLines
2014-07-05eliminate use of cached pid from thread structureRich Felker-8/+5
the main motivation for this change is to remove the assumption that the tid of the main thread is also the pid of the process. (the value returned by the set_tid_address syscall was used to fill both fields despite it semantically being the tid.) this is historically and presently true on linux and unlikely to change, but it conceivably could be false on other systems that otherwise reproduce the linux syscall api/abi. only a few parts of the code were actually still using the cached pid. in a couple places (aio and synccall) it was a minor optimization to avoid a syscall. caching could be reintroduced, but lazily as part of the public getpid function rather than at program startup, if it's deemed important for performance later. in other places (cancellation and pthread_kill) the pid was completely unnecessary; the tkill syscall can be used instead of tgkill. this is actually a rather subtle issue, since tgkill is supposedly a solution to race conditions that can affect use of tkill. however, as documented in the commit message for commit 7779dbd2663269b465951189b4f43e70839bc073, tgkill does not actually solve this race; it just limits it to happening within one process rather than between processes. we use a lock that avoids the race in pthread_kill, and the use in the cancellation signal handler is self-targeted and thus not subject to tid reuse races, so both are safe regardless of which syscall (tgkill or tkill) is used.
2014-07-02add locale frameworkRich Felker-0/+7
this commit adds non-stub implementations of setlocale, duplocale, newlocale, and uselocale, along with the data structures and minimal code needed for representing the active locale on a per-thread basis and optimizing the common case where thread-local locale settings are not in use. at this point, the data structures only contain what is necessary to represent LC_CTYPE (a single flag) and LC_MESSAGES (a name for use in finding message translation files). representation for the other categories will be added later; the expectation is that a single pointer will suffice for each. for LC_CTYPE, the strings "C" and "POSIX" are treated as special; any other string is accepted and treated as "C.UTF-8". for other categories, any string is accepted after being truncated to a maximum supported length (currently 15 bytes). for LC_MESSAGES, the name is kept regardless of whether libc itself can use such a message translation locale, since applications using catgets or gettext should be able to use message locales libc is not aware of. for other categories, names which are not successfully loaded as locales (which, at present, means all names) are treated as aliases for "C". setlocale never fails. locale settings are not yet used anywhere, so this commit should have no visible effects except for the contents of the string returned by setlocale.
2014-06-19separate __tls_get_addr implementation from dynamic linker/init_tlsRich Felker-0/+17
such separation serves multiple purposes: - by having the common path for __tls_get_addr alone in its own function with a tail call to the slow case, code generation is greatly improved. - by having __tls_get_addr in it own file, it can be replaced on a per-arch basis as needed, for optimization or ABI-specific purposes. - by removing __tls_get_addr from __init_tls.c, a few bytes of code are shaved off of static binaries (which are unlikely to use this function unless the linker messed up).
2014-06-19optimize i386 ___tls_get_addr asmRich Felker-1/+8
2014-06-10simplify errno implementationRich Felker-1/+0
the motivation for the errno_ptr field in the thread structure, which this commit removes, was to allow the main thread's errno to keep its address when lazy thread pointer initialization was used. &errno was evaluated prior to setting up the thread pointer and stored in errno_ptr for the main thread; subsequently created threads would have errno_ptr pointing to their own errno_val in the thread structure. since lazy initialization was removed, there is no need for this extra level of indirection; __errno_location can simply return the address of the thread's errno_val directly. this does cause &errno to change, but the change happens before entry to application code, and thus is not observable.
2014-06-10replace all remaining internal uses of pthread_self with __pthread_selfRich Felker-10/+10
prior to version 1.1.0, the difference between pthread_self (the public function) and __pthread_self (the internal macro or inline function) was that the former would lazily initialize the thread pointer if it was not already initialized, whereas the latter would crash in this case. since lazy initialization is no longer supported, use of pthread_self no longer makes sense; it simply generates larger, slower code.
2014-06-10add thread-pointer support for pre-2.6 kernels on i386Rich Felker-4/+18
such kernels cannot support threads, but the thread pointer is also important for other purposes, most notably stack protector. without a valid thread pointer, all code compiled with stack protector will crash. the same applies to any use of thread-local storage by applications or libraries. the concept of this patch is to fall back to using the modify_ldt syscall, which has been around since linux 1.0, to setup the gs segment register. since the kernel does not have a way to automatically assign ldt entries, use of slot zero is hard-coded. if this fallback path is used, __set_thread_area returns a positive value (rather than the usual zero for success, or negative for error) indicating to the caller that the thread pointer was successfully set, but only for the main thread, and that thread creation will not work properly. the code in __init_tp has been changed accordingly to record this result for later use by pthread_create.
2014-04-15fix deadlock race in pthread_onceRich Felker-2/+1
at the end of successful pthread_once, there was a race window during which another thread calling pthread_once would momentarily change the state back from 2 (finished) to 1 (in-progress). in this case, the status was immediately changed back, but with no wake call, meaning that waiters which arrived during this short window could block forever. there are two possible fixes. one would be adding the wake to the code path where it was missing. but it's better just to avoid reverting the status at all, by using compare-and-swap instead of swap.
2014-03-24fix pointer type mismatch and misplacement of constRich Felker-2/+2
2014-03-24always initialize thread pointer at program startRich Felker-52/+23
this is the first step in an overhaul aimed at greatly simplifying and optimizing everything dealing with thread-local state. previously, the thread pointer was initialized lazily on first access, or at program startup if stack protector was in use, or at certain random places where inconsistent state could be reached if it were not initialized early. while believed to be fully correct, the logic was fragile and non-obvious. in the first phase of the thread pointer overhaul, support is retained (and in some cases improved) for systems/situation where loading the thread pointer fails, e.g. old kernels. some notes on specific changes: - the confusing use of libc.main_thread as an indicator that the thread pointer is initialized is eliminated in favor of an explicit has_thread_pointer predicate. - sigaction no longer needs to ensure that the thread pointer is initialized before installing a signal handler (this was needed to prevent a situation where the signal handler caused the thread pointer to be initialized and the subsequent sigreturn cleared it again) but it still needs to ensure that implementation-internal thread-related signals are not blocked. - pthread tsd initialization for the main thread is deferred in a new manner to minimize bloat in the static-linked __init_tp code. - pthread_setcancelstate no longer needs special handling for the situation before the thread pointer is initialized. it simply fails on systems that cannot support a thread pointer, which are non-conforming anyway. - pthread_cleanup_push/pop now check for missing thread pointer and nop themselves out in this case, so stdio no longer needs to avoid the cancellable path when the thread pointer is not available. a number of cases remain where certain interfaces may crash if the system does not support a thread pointer. at this point, these should be limited to pthread interfaces, and the number of such cases should be fewer than before.
2014-02-27rename superh port to "sh" for consistencyRich Felker-0/+0
linux, gcc, etc. all use "sh" as the name for the superh arch. there was already some inconsistency internally in musl: the dynamic linker was searching for "ld-musl-sh.path" as its path file despite its own name being "ld-musl-superh.so.1". there was some sentiment in both directions as to how to resolve the inconsistency, but overall "sh" was favored.
2014-02-23superh portBobby Bingham-0/+113
2014-02-23mostly-cosmetic fixups to x32 port mergeRich Felker-6/+9
2014-02-23x32 port (diff against vanilla x86_64)rofl0r-10/+8
2014-02-23import vanilla x86_64 code as x32rofl0r-0/+70
2014-02-22use syscall_arg_t type for syscall prototypes in pthread coderofl0r-3/+8
2014-02-09clone: make clone a wrapper around __cloneBobby Bingham-18/+3
The architecture-specific assembly versions of clone did not set errno on failure, which is inconsistent with glibc. __clone still returns the error via its return value, and clone is now a wrapper that sets errno as needed. The public clone has also been moved to src/linux, as it's not directly related to the pthreads API. __clone is called by pthread_create, which does not report errors via errno. Though not strictly necessary, it's nice to avoid clobbering errno here.
2014-01-06eliminate explicit (long) casts when making syscallsRich Felker-1/+1
this practice came from very early, before internal/syscall.h defined macros that could accept pointer arguments directly and handle them correctly. aside from being ugly and unnecessary, it looks like it will be problematic when we add support for 32-bit ABIs on archs where registers (and syscall arguments) are 64-bit, e.g. x32 and mips n32.
2013-12-12include cleanups: remove unused headers and add feature test macrosSzabolcs Nagy-3/+0
2013-10-04fix invalid implicit pointer conversion in pthread_key_createRich Felker-1/+1
2013-09-20fix potential deadlock bug in libc-internal locking logicRich Felker-3/+6
if a multithreaded program became non-multithreaded (i.e. all other threads exited) while one thread held an internal lock, the remaining thread would fail to release the lock. the the program then became multithreaded again at a later time, any further attempts to obtain the lock would deadlock permanently. the underlying cause is that the value of libc.threads_minus_1 at unlock time might not match the value at lock time. one solution would be returning a flag to the caller indicating whether the lock was taken and needs to be unlocked, but there is a simpler solution: using the lock itself as such a flag. note that this flag is not needed anyway for correctness; if the lock is not held, the unlock code is harmless. however, the memory synchronization properties associated with a_store are costly on some archs, so it's best to avoid executing the unlock code when it is unnecessary.
2013-09-16fix clobbering of caller's stack in mips __clone functionRich Felker-0/+3
this was resulting in crashes in posix_spawn on mips, and would have affected applications calling clone too. since the prototype for __clone has it as a variadic function, it may not assume that 16($sp) is writable for use in making the syscall. instead, it needs to allocate additional stack space, and then adjust the stack pointer back in both of the code paths for the parent process/thread.
2013-09-16omit CLONE_PARENT flag to clone in pthread_createRich Felker-1/+1
CLONE_PARENT is not necessary (CLONE_THREAD provides all the useful parts of it) and Linux treats CLONE_PARENT as an error in certain situations, without noticing that it would be a no-op due to CLONE_THREAD. this error case prevents, for example, use of a multi-threaded init process and certain usages with containers.
2013-09-16use symbolic names for clone flags in pthread_createRich Felker-2/+5
2013-09-15support configurable page size on mips, powerpc and microblazeSzabolcs Nagy-0/+2
PAGE_SIZE was hardcoded to 4096, which is historically what most systems use, but on several archs it is a kernel config parameter, user space can only know it at execution time from the aux vector. PAGE_SIZE and PAGESIZE are not defined on archs where page size is a runtime parameter, applications should use sysconf(_SC_PAGE_SIZE) to query it. Internally libc code defines PAGE_SIZE to libc.page_size, which is set to aux[AT_PAGESZ] in __init_libc and early in __dynlink as well. (Note that libc.page_size can be accessed without GOT, ie. before relocations are done) Some fpathconf settings are hardcoded to 4096, these should be actually queried from the filesystem using statfs.
2013-09-14fix child stack alignment on mips cloneRich Felker-0/+1
unlike other archs, the mips version of clone was not doing anything to align the stack pointer. this seems to have been the cause for some SIGBUS crashes that were observed in posix_spawn.
2013-09-02fix mips-specific bug in synccall (too little space for signal mask)Rich Felker-5/+3
switch to the new __block_all_sigs/__restore_sigs internal API to clean up the code too.
2013-09-02in synccall, ignore the signal before any threads' signal handlers returnRich Felker-4/+4
this protects against deadlock from spurious signals (e.g. sent by another process) arriving after the controlling thread releases the other threads from the sync operation.
2013-09-02fix invalid pointer in synccall (multithread setuid, etc.)Rich Felker-0/+1
the head pointer was not being reset between calls to synccall, so any use of this interface more than once would build the linked list incorrectly, keeping the (now invalid) list nodes from the previous call.
2013-07-31in pthread_getattr_np, use mremap rather than madvise to measure stackRich Felker-1/+2
the original motivation for this patch was that qemu (and possibly other syscall emulators) nop out madvise, resulting in an infinite loop. however, there is another benefit to this change: madvise may actually undo an explicit madvise the application intended for its stack, whereas the mremap operation is a true nop. the logic here is that mremap must fail if it cannot resize the mapping in-place, and the caller knows that it cannot resize in-place because it knows the next page of virtual memory is already occupied.
2013-07-22make pthread attribute types structs, even when they just have one fieldRich Felker-22/+22
this change is to get the right tags for C++ ABI matching. it should have no other effects.
2013-06-26fix syscall argument bug in pthread_getschedparamRich Felker-1/+1
the address of the pointer to the sched param, rather than the pointer, was being passed to the kernel.
2013-06-26fix temp file leak in sem_open on successful creation of new semaphoreRich Felker-2/+2
2013-06-26fix bug whereby sem_open leaked its own internal slots on failureRich Felker-3/+6
2013-06-26in sem_open, don't leak vm mapping if fstat failsRich Felker-2/+2
fstat should not fail under normal circumstances, so this fix is mostly theoretical.
2013-06-26fix failure of pthread_setschedparam to pass correct param to kernelRich Felker-1/+1
the address of the pointer, rather than the pointer, was being passed. this was probably a copy-and-paste error from corresponding get code.
2013-06-08support cputime clocks for processes/threads other than selfRich Felker-1/+2
apparently these features have been in Linux for a while now, so it makes sense to support them. the bit twiddling seems utterly illogical and wasteful, especially the negation, but that's how the kernel folks chose to encode pids/tids into the clock id.
2013-06-03ensure that thread dtv pointer is never null to optimize __tls_get_addrRich Felker-0/+2
2013-04-26transition to using functions for internal signal blocking/restoringRich Felker-8/+5
there are several reasons for this change. one is getting rid of the repetition of the syscall signature all over the place. another is sharing the constant masks without costly GOT accesses in PIC. the main motivation, however, is accurately representing whether we want to block signals that might be handled by the application, or all signals.
2013-04-26prevent code from running under a thread id which already gave ESRCHRich Felker-1/+7
2013-04-26synccall signal handler need not handle dead threads anymoreRich Felker-9/+0
they have already blocked signals before decrementing the thread count, so the code being removed is unreachable in the case where the thread is no longer counted.
2013-04-26fix clobbering of signal mask when creating thread with sched attributesRich Felker-1/+1
this was simply a case of saving the state in the wrong place.
2013-04-26make last thread's pthread_exit give exit(0) a consistent stateRich Felker-3/+13
the previous few commits ended up leaving the thread count and signal mask wrong for atexit handlers and stdio cleanup.
2013-04-26use atomic decrement rather than cas in pthread_exit thread countRich Felker-4/+1
now that blocking signals prevents any application code from running while the last thread is exiting, the cas logic is no longer needed to prevent decrementing below zero.
2013-04-26add comments on some of the pthread_exit logicRich Felker-2/+15
2013-04-26always block signals in pthread_exit before decrementing thread countRich Felker-2/+2
the thread count (1+libc.threads_minus_1) must always be greater than or equal to the number of threads which could have application code running, even in an async-signal-safe sense. there is at least one dangerous race condition if this invariant fails to hold: dlopen could allocate too little TLS for existing threads, and a signal handler running in the exiting thread could claim the allocated TLS for itself (via __tls_get_addr), leaving too little for the other threads it was allocated for and thereby causing out-of-bounds access. there may be other situations where it's dangerous for the thread count to be too low, particularly in the case where only one thread should be left, in which case locking may be omitted. however, all such code paths seem to arise from undefined behavior, since async-signal-unsafe functions are not permitted to be called from a signal handler that interrupts pthread_exit (which is itself async-signal-unsafe). this change may also simplify logic in __synccall and improve the chances of making __synccall async-signal-safe.
2013-04-06fix type error in pthread_create, introduced with pthread_getattr_npRich Felker-1/+1
2013-03-31implement pthread_getattr_npRich Felker-2/+29
this function is mainly (purely?) for obtaining stack address information, but we also provide the detach state since it's easy to do anyway.
2013-03-26remove __SYSCALL_SSLEN arch macro in favor of using public _NSIGRich Felker-9/+9
the issue at hand is that many syscalls require as an argument the kernel-ABI size of sigset_t, intended to allow the kernel to switch to a larger sigset_t in the future. previously, each arch was defining this size in syscall_arch.h, which was redundant with the definition of _NSIG in bits/signal.h. as it's used in some not-quite-portable application code as well, _NSIG is much more likely to be recognized and understood immediately by someone reading the code, and it's also shorter and less cluttered. note that _NSIG is actually 65/129, not 64/128, but the division takes care of throwing away the off-by-one part.
2013-02-01fix stale locks left behind when pthread_create failsRich Felker-3/+6
this bug seems to have been around a long time.