summaryrefslogtreecommitdiff
path: root/src/thread
AgeCommit message (Collapse)AuthorFilesLines
2011-04-19fix uninitialized waiters field in semaphoresRich Felker1-0/+1
2011-04-18recheck cancellation disabled flag after syscall returns EINTRRich Felker1-1/+1
we already checked before making the syscall, but it's possible that a signal handler interrupted the blocking syscall and disabled cancellation, and that this is the cause of EINTR. in this case, the old behavior was testably wrong.
2011-04-17fix typo in x86_64 cancellable syscall asmRich Felker1-1/+1
2011-04-17pthread_exit is not supposed to affect cancellabilityRich Felker1-2/+0
if the exit was caused by cancellation, __cancel has already set these flags anyway.
2011-04-17fix pthread_exit from cancellation handlerRich Felker1-5/+5
cancellation frames were not correctly popped, so this usage would not only loop, but also reuse discarded and invalid parts of the stack.
2011-04-17clean up handling of thread/nothread mode, lockingRich Felker3-16/+10
2011-04-17debloat: use __syscall instead of syscall where possibleRich Felker2-2/+2
don't waste time (and significant code size due to function call overhead!) setting errno when the result of a syscall does not matter or when it can't fail.
2011-04-17fix bugs in cancellable syscall asmRich Felker3-11/+12
x86_64 was just plain wrong in the cancel-flag-already-set path, and crashing. the more subtle error was not clearing the saved stack pointer before returning to c code. this could result in the signal handler misidentifying c code as the pre-syscall part of the asm, and acting on cancellation at the wrong time, and thus resource leak race conditions. also, now __cancel (in the c code) is responsible for clearing the saved sp in the already-cancelled branch. this means we have to use call rather than jmp to ensure the stack pointer in the c will never match what the asm saved.
2011-04-17optimize cancellation enable/disable codeRich Felker3-4/+10
the goal is to be able to use pthread_setcancelstate internally in the implementation, whenever a function might want to use functions which are cancellation points but avoid becoming a cancellation point itself. i could have just used a separate internal function for temporarily inhibiting cancellation, but the solution in this commit is better because (1) it's one less implementation-specific detail in functions that need to use it, and (2) application code can also get the same benefit. previously, pthread_setcancelstate dependend on pthread_self, which would pull in unwanted thread setup overhead for non-threaded programs. now, it temporarily stores the state in the global libc struct if threads have not been initialized, and later moves it if needed. this way we can instead use __pthread_self, which has no dependencies and assumes that the thread register is already valid.
2011-04-17don't use pthread_once when there is no danger in raceRich Felker1-2/+5
2011-04-17fix some minor issues in cancellation handling patchRich Felker3-11/+19
signals were wrongly left masked, and cancellability state was not switched to disabled, during the execution of cleanup handlers.
2011-04-17overhaul pthread cancellationRich Felker13-59/+182
this patch improves the correctness, simplicity, and size of cancellation-related code. modulo any small errors, it should now be completely conformant, safe, and resource-leak free. the notion of entering and exiting cancellation-point context has been completely eliminated and replaced with alternative syscall assembly code for cancellable syscalls. the assembly is responsible for setting up execution context information (stack pointer and address of the syscall instruction) which the cancellation signal handler can use to determine whether the interrupted code was in a cancellable state. these changes eliminate race conditions in the previous generation of cancellation handling code (whereby a cancellation request received just prior to the syscall would not be processed, leaving the syscall to block, potentially indefinitely), and remedy an issue where non-cancellable syscalls made from signal handlers became cancellable if the signal handler interrupted a cancellation point. x86_64 asm is untested and may need a second try to get it right.
2011-04-14change sem_trywait algorithm so it never has to call __wakeRich Felker1-3/+2
2011-04-14cheap trick to further optimize locking normal mutexesRich Felker2-2/+2
2011-04-14use a separate signal from SIGCANCEL for SIGEV_THREAD timersRich Felker1-2/+0
otherwise we cannot support an application's desire to use asynchronous cancellation within the callback function. this change also slightly debloats pthread_create.c.
2011-04-13simplify cancellation point handlingRich Felker2-16/+5
we take advantage of the fact that unless self->cancelpt is 1, cancellation cannot happen. so just increment it by 2 to temporarily block cancellation. this drops pthread_create.o well under 1k.
2011-04-06fixed crash in new rsyscall (failure to set sa_flags for signal handler)Rich Felker1-0/+2
2011-04-06consistency: change all remaining syscalls to use SYS_ rather than __NR_ prefixRich Felker7-8/+8
2011-04-06move rsyscall out of pthread_create moduleRich Felker2-96/+122
this is something of a tradeoff, as now set*id() functions, rather than pthread_create, are what pull in the code overhead for dealing with linux's refusal to implement proper POSIX thread-vs-process semantics. my motivations are: 1. it's cleaner this way, especially cleaner to optimize out the rsyscall locking overhead from pthread_create when it's not needed. 2. it's expected that only a tiny number of core system programs will ever use set*id() functions, whereas many programs may want to use threads, and making thread overhead tiny is an incentive for "light" programs to try threads.
2011-04-06pthread exit stuff: don't bother setting errno when we won't check it.Rich Felker1-2/+2
2011-04-06fix rsyscall handler: must not clobber errno from signal contextRich Felker1-2/+4
2011-04-06major semaphore improvements (performance and correctness)Rich Felker5-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 Felker2-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 Felker1-1/+1
2011-04-03block all signals during rsyscallRich Felker1-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 Felker1-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 Felker1-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 Felker1-7/+4
2011-04-03simplify pthread tsd key handlingRich Felker2-8/+6
2011-04-03omit pthread tsd dtor code if tsd is not usedRich Felker2-14/+24
2011-04-01simplify setting result on thread cancellationRich Felker1-1/+1
2011-04-01use bss instead of mmap for main thread's pthread thread-specific dataRich Felker2-9/+4
this simplifies code and removes a failure case
2011-04-01fix misspelled PTHREAD_CANCELED constantRich Felker1-1/+1
2011-04-01use a_store to set cancel flag in pthread_cancel, to ensure a barrierRich Felker1-1/+1
2011-03-31simplify pthread_key_deleteRich Felker1-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 Felker1-10/+9
2011-03-30avoid crash on stupid but allowable usage of pthread_mutex_unlockRich Felker1-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 Felker1-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 Felker1-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 Felker1-1/+1
2011-03-29major improvements to cancellation handlingRich Felker2-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 Felker2-3/+2
2011-03-28fix broken spinlock due to miscompilationRich Felker1-1/+1
actually this trick also seems to have made the uncontended case slower.
2011-03-25match glibc/lsb cancellation abi on i386Rich Felker6-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 Felker1-1/+2
2011-03-24optimize contended case for pthread_spin_trylockRich Felker1-1/+2
2011-03-24optimize spinlock spinRich Felker1-1/+1
2011-03-24overhaul cancellation to fix resource leaks and dangerous behavior with signalsRich Felker3-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 Felker4-6/+5
2011-03-19if returning errno value directly from a syscall, we need to negate it.Rich Felker2-2/+2