summaryrefslogtreecommitdiff
path: root/src/thread
AgeCommit message (Collapse)AuthorLines
2011-04-06major semaphore improvements (performance and correctness)Rich Felker-21/+37
1. make sem_[timed]wait interruptible by signals, per POSIX 2. keep a waiter count in order to avoid unnecessary futex wake syscalls
2011-04-05new framework to inhibit thread cancellation when neededRich Felker-5/+15
with these small changes, libc functions which need to call functions which are cancellation points, but which themselves must not be cancellation points, can use the CANCELPT_INHIBIT and CANCELPT_RESUME macros to temporarily inhibit all cancellation.
2011-04-03pthread_create need not set errnoRich Felker-1/+1
2011-04-03block all signals during rsyscallRich Felker-4/+9
otherwise a signal handler could see an inconsistent and nonconformant program state where different threads have different uids/gids.
2011-04-03fix race condition in rsyscall handlerRich Felker-1/+1
the problem: there is a (single-instruction) race condition window between a thread flagging itself dead and decrementing itself from the thread count. if it receives the rsyscall signal at this exact moment, the rsyscall caller will never succeed in signalling enough flags to succeed, and will deadlock forever. in previous versions of musl, the about-to-terminate thread masked all signals prior to decrementing the thread count, but this cost a whole syscall just to account for extremely rare races. the solution is a huge hack: rather than blocking in the signal handler if the thread is dead, modify the signal mask of the saved context and return in order to prevent further signal handling by the dead thread. this allows the dead thread to continue decrementing the thread count (if it had not yet done so) and exiting, even while the live part of the program blocks for rsyscall.
2011-04-03don't trust siginfo in rsyscall handlerRich Felker-3/+2
for some inexplicable reason, linux allows the sender of realtime signals to spoof its identity. permission checks for sending signals should limit the impact to same-user processes, but just to be safe, we avoid trusting the siginfo structure and instead simply examine the program state to see if we're in the middle of a legitimate rsyscall.
2011-04-03simplify calling of timer signal handlerRich Felker-7/+4
2011-04-03simplify pthread tsd key handlingRich Felker-8/+6
2011-04-03omit pthread tsd dtor code if tsd is not usedRich Felker-14/+24
2011-04-01simplify setting result on thread cancellationRich Felker-1/+1
2011-04-01use bss instead of mmap for main thread's pthread thread-specific dataRich Felker-9/+4
this simplifies code and removes a failure case
2011-04-01fix misspelled PTHREAD_CANCELED constantRich Felker-1/+1
2011-04-01use a_store to set cancel flag in pthread_cancel, to ensure a barrierRich Felker-1/+1
2011-03-31simplify pthread_key_deleteRich Felker-1/+1
calling this function on an uninitialized key value is UB, so there is no need to check that the table pointer was initialized.
2011-03-31greatly simplify pthread_key_create (~20% size reduction)Rich Felker-10/+9
2011-03-30avoid crash on stupid but allowable usage of pthread_mutex_unlockRich Felker-1/+3
unlocking an unlocked mutex is not UB for robust or error-checking mutexes, so we must avoid calling __pthread_self (which might crash due to lack of thread-register initialization) until after checking that the mutex is locked.
2011-03-30streamline mutex unlock to remove a useless branch, use a_store to unlockRich Felker-2/+6
this roughly halves the cost of pthread_mutex_unlock, at least for non-robust, normal-type mutexes. the a_store change is in preparation for future support of archs which require a memory barrier or special atomic store operation, and also should prevent the possibility of the compiler misordering writes.
2011-03-30cheap special-case optimization for normal mutexesRich Felker-0/+4
cycle-level benchmark on atom cpu showed typical pthread_mutex_lock call dropping from ~120 cycles to ~90 cycles with this change. benefit may vary with compiler options and version, but this optimization is very cheap to make and should always help some.
2011-03-29revert mutex "optimization" that turned out to be worseRich Felker-1/+1
2011-03-29major improvements to cancellation handlingRich Felker-6/+13
- there is no longer any risk of spoofing cancellation requests, since the cancel flag is set in pthread_cancel rather than in the signal handler. - cancellation signal is no longer unblocked when running the cancellation handlers. instead, pthread_create will cause any new threads created from a cancellation handler to unblock their own cancellation signal. - various tweaks in preparation for POSIX timer support.
2011-03-28revert some more spin optimizations that turned out to be pessimizationsRich Felker-3/+2
2011-03-28fix broken spinlock due to miscompilationRich Felker-1/+1
actually this trick also seems to have made the uncontended case slower.
2011-03-25match glibc/lsb cancellation abi on i386Rich Felker-0/+36
glibc made the ridiculous choice to use pass-by-register calling convention for these functions, which is impossible to duplicate directly on non-gcc compilers. instead, we use ugly asm to wrap and convert the calling convention. presumably this works with every compiler anyone could potentially want to use.
2011-03-24prepare pthread_spin_unlock for archs that need memory barriersRich Felker-1/+2
2011-03-24optimize contended case for pthread_spin_trylockRich Felker-1/+2
2011-03-24optimize spinlock spinRich Felker-1/+1
2011-03-24overhaul cancellation to fix resource leaks and dangerous behavior with signalsRich Felker-10/+21
this commit addresses two issues: 1. a race condition, whereby a cancellation request occurring after a syscall returned from kernelspace but before the subsequent CANCELPT_END would cause cancellable resource-allocating syscalls (like open) to leak resources. 2. signal handlers invoked while the thread was blocked at a cancellation point behaved as if asynchronous cancellation mode wer in effect, resulting in potentially dangerous state corruption if a cancellation request occurs. the glibc/nptl implementation of threads shares both of these issues. with this commit, both are fixed. however, cancellation points encountered in a signal handler will not be acted upon if the signal was received while the thread was already at a cancellation point. they will of course be acted upon after the signal handler returns, so in real-world usage where signal handlers quickly return, it should not be a problem. it's possible to solve this problem too by having sigaction() wrap all signal handlers with a function that uses a pthread_cleanup handler to catch cancellation, patch up the saved context, and return into the cancellable function that will catch and act upon the cancellation. however that would be a lot of complexity for minimal if any benefit...
2011-03-20global cleanup to use the new syscall interfaceRich Felker-6/+5
2011-03-19if returning errno value directly from a syscall, we need to negate it.Rich Felker-2/+2
2011-03-19syscall overhaul part two - unify public and internal syscall interfaceRich Felker-15/+9
with this patch, the syscallN() functions are no longer needed; a variadic syscall() macro allows syscalls with anywhere from 0 to 6 arguments to be made with a single macro name. also, manually casting each non-integer argument with (long) is no longer necessary; the casts are hidden in the macros. some source files which depended on being able to define the old macro SYSCALL_RETURNS_ERRNO have been modified to directly use __syscall() instead of syscall(). references to SYSCALL_SIGSET_SIZE and SYSCALL_LL have also been changed. x86_64 has not been tested, and may need a follow-up commit to fix any minor bugs/oversights.
2011-03-19overhaul syscall interfaceRich Felker-2/+2
this commit shuffles around the location of syscall definitions so that we can make a syscall() library function with both SYS_* and __NR_* style syscall names available to user applications, provides the syscall() library function, and optimizes the code that performs the actual inline syscalls in the library itself. previously on i386 when built as PIC (shared library), syscalls were incurring bus lock (lock prefix) overhead at entry and exit, due to the way the ebx register was being loaded (xchg instruction with a memory operand). now the xchg takes place between two registers. further cleanup to arch/$(ARCH)/syscall.h is planned.
2011-03-17implement robust mutexesRich Felker-10/+78
some of this code should be cleaned up, e.g. using macros for some of the bit flags, masks, etc. nonetheless, the code is believed to be working and correct at this point.
2011-03-17avoid function call to pthread_self in mutex unlockRich Felker-1/+1
if the mutex was previously locked, we can assume pthread_self was already called at the time of locking, and thus that the thread pointer is initialized.
2011-03-17unify lock and owner fields of mutex structureRich Felker-7/+6
this change is necessary to free up one slot in the mutex structure so that we can use doubly-linked lists in the implementation of robust mutexes.
2011-03-17optimize contended normal mutex case; add int compare-and-swap atomicRich Felker-1/+1
2011-03-16simplify logic, slightly optimize contended case for non-default mutex typesRich Felker-4/+2
2011-03-16correct error returns for error-checking mutexesRich Felker-2/+6
2011-03-16cut out a syscall on thread creation in the case where guard size is 0Rich Felker-1/+1
2011-03-16don't expose EAGAIN, etc. from timed futex wait to callerRich Felker-1/+4
2011-03-12implement flockfile api, rework stdio lockingRich Felker-0/+1
2011-03-11implement dummy pthread_attr_[gs]etschedparam functionsRich Felker-0/+14
for some reason these functions are not shaded by the PS/TPS option in POSIX, so presumably they are mandatory, even though the functionality they offer is optional. for now, provide them in case any programs depend on their existence, but disallow any priority except the default.
2011-03-11fix pthread_attr_* implementations to match corrected prototypesRich Felker-4/+4
2011-03-10fix failure behavior of sem_open when sem does not existRich Felker-1/+5
2011-03-10fix some semaphore wait semantics (race condition deadlock and error checking)Rich Felker-1/+7
2011-03-10fix sem_open and sem_close to obey posix semanticsRich Felker-33/+80
multiple opens of the same named semaphore must return the same pointer, and only the last close can unmap it. thus the ugly global state keeping track of mappings. the maximum number of distinct named semaphores that can be opened is limited sufficiently small that the linear searches take trivial time, especially compared to the syscall overhead of these functions.
2011-03-10optimize pthread termination in the non-detached caseRich Felker-4/+15
we can avoid blocking signals by simply using a flag to mark that the thread has exited and prevent it from getting counted in the rsyscall signal-pingpong. this restores the original pthread create/join throughput from before the sigprocmask call was added.
2011-03-10security fix: check that cancel/rsyscall signal was sent by the process itselfRich Felker-0/+3
2011-03-09fix error handling for pthread_sigmaskRich Felker-0/+10
it must return errno, not -1, and should reject invalud values for how.
2011-03-09optimize pthread initializationRich Felker-2/+2
the set_tid_address returns the tid (which is also the pid when called from the initial thread) so there is no need to make a separate syscall to get pid/tid.
2011-03-08simplify and optimize pthread_mutex_trylockRich Felker-17/+16