about summary refs log tree commit diff
path: root/src/thread
Commit message (Collapse)AuthorAgeFilesLines
* overhaul pthread cancellationRich Felker2011-04-1713-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.
* change sem_trywait algorithm so it never has to call __wakeRich Felker2011-04-141-3/+2
|
* cheap trick to further optimize locking normal mutexesRich Felker2011-04-142-2/+2
|
* use a separate signal from SIGCANCEL for SIGEV_THREAD timersRich Felker2011-04-141-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.
* simplify cancellation point handlingRich Felker2011-04-132-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.
* fixed crash in new rsyscall (failure to set sa_flags for signal handler)Rich Felker2011-04-061-0/+2
|
* consistency: change all remaining syscalls to use SYS_ rather than __NR_ prefixRich Felker2011-04-067-8/+8
|
* move rsyscall out of pthread_create moduleRich Felker2011-04-062-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.
* pthread exit stuff: don't bother setting errno when we won't check it.Rich Felker2011-04-061-2/+2
|
* fix rsyscall handler: must not clobber errno from signal contextRich Felker2011-04-061-2/+4
|
* major semaphore improvements (performance and correctness)Rich Felker2011-04-065-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
* new framework to inhibit thread cancellation when neededRich Felker2011-04-052-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.
* pthread_create need not set errnoRich Felker2011-04-031-1/+1
|
* block all signals during rsyscallRich Felker2011-04-031-4/+9
| | | | | otherwise a signal handler could see an inconsistent and nonconformant program state where different threads have different uids/gids.
* fix race condition in rsyscall handlerRich Felker2011-04-031-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.
* don't trust siginfo in rsyscall handlerRich Felker2011-04-031-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.
* simplify calling of timer signal handlerRich Felker2011-04-031-7/+4
|
* simplify pthread tsd key handlingRich Felker2011-04-032-8/+6
|
* omit pthread tsd dtor code if tsd is not usedRich Felker2011-04-032-14/+24
|
* simplify setting result on thread cancellationRich Felker2011-04-011-1/+1
|
* use bss instead of mmap for main thread's pthread thread-specific dataRich Felker2011-04-012-9/+4
| | | | this simplifies code and removes a failure case
* fix misspelled PTHREAD_CANCELED constantRich Felker2011-04-011-1/+1
|
* use a_store to set cancel flag in pthread_cancel, to ensure a barrierRich Felker2011-04-011-1/+1
|
* simplify pthread_key_deleteRich Felker2011-03-311-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.
* greatly simplify pthread_key_create (~20% size reduction)Rich Felker2011-03-311-10/+9
|
* avoid crash on stupid but allowable usage of pthread_mutex_unlockRich Felker2011-03-301-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.
* streamline mutex unlock to remove a useless branch, use a_store to unlockRich Felker2011-03-301-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.
* cheap special-case optimization for normal mutexesRich Felker2011-03-301-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.
* revert mutex "optimization" that turned out to be worseRich Felker2011-03-291-1/+1
|
* major improvements to cancellation handlingRich Felker2011-03-292-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.
* revert some more spin optimizations that turned out to be pessimizationsRich Felker2011-03-282-3/+2
|
* fix broken spinlock due to miscompilationRich Felker2011-03-281-1/+1
| | | | actually this trick also seems to have made the uncontended case slower.
* match glibc/lsb cancellation abi on i386Rich Felker2011-03-256-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.
* prepare pthread_spin_unlock for archs that need memory barriersRich Felker2011-03-241-1/+2
|
* optimize contended case for pthread_spin_trylockRich Felker2011-03-241-1/+2
|
* optimize spinlock spinRich Felker2011-03-241-1/+1
|
* overhaul cancellation to fix resource leaks and dangerous behavior with signalsRich Felker2011-03-243-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...
* global cleanup to use the new syscall interfaceRich Felker2011-03-204-6/+5
|
* if returning errno value directly from a syscall, we need to negate it.Rich Felker2011-03-192-2/+2
|
* syscall overhaul part two - unify public and internal syscall interfaceRich Felker2011-03-197-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.
* overhaul syscall interfaceRich Felker2011-03-191-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.
* implement robust mutexesRich Felker2011-03-178-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.
* avoid function call to pthread_self in mutex unlockRich Felker2011-03-171-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.
* unify lock and owner fields of mutex structureRich Felker2011-03-173-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.
* optimize contended normal mutex case; add int compare-and-swap atomicRich Felker2011-03-171-1/+1
|
* simplify logic, slightly optimize contended case for non-default mutex typesRich Felker2011-03-161-4/+2
|
* correct error returns for error-checking mutexesRich Felker2011-03-162-2/+6
|
* cut out a syscall on thread creation in the case where guard size is 0Rich Felker2011-03-161-1/+1
|
* don't expose EAGAIN, etc. from timed futex wait to callerRich Felker2011-03-161-1/+4
|
* implement flockfile api, rework stdio lockingRich Felker2011-03-121-0/+1
|