about summary refs log tree commit diff
path: root/src/thread
Commit message (Collapse)AuthorAgeFilesLines
* fix extremely rare but dangerous race condition in robust mutexesRich Felker2012-08-173-20/+33
| | | | | | | | | | if new shared mappings of files/devices/shared memory can be made between the time a robust mutex is unlocked and its subsequent removal from the pending slot in the robustlist header, the kernel can inadvertently corrupt data in the newly-mapped pages when the process terminates. i am fixing the bug by using the same global vm lock mechanism that was used to fix the race condition with unmapping barriers after pthread_barrier_wait returns.
* fix (hopefully) all hard-coded 8's for kernel sigset_t sizeRich Felker2012-08-094-7/+12
| | | | | | | | | | some minor changes to how hard-coded sets for thread-related purposes are handled were also needed, since the old object sizes were not necessarily sufficient. things have gotten a bit ugly in this area, and i think a cleanup is in order at some point, but for now the goal is just to get the code working on all supported archs including mips, which was badly broken by linux rejecting syscalls with the wrong sigset_t size.
* retry on cas failures in sem_trywaitRich Felker2012-07-241-2/+2
| | | | | | | | | this seems counter-intuitive since sem_trywait is supposed to just try once, not wait for the semaphore. however, the retry loop is not a wait. instead, it's to handle the case where the value changes due to a simultaneous post or wait from another thread while the semaphore value remains positive. in such a case, it's absolutely wrong for sem_trywait to fail with EAGAIN because the semaphore is not busy.
* avoid blx instruction which does not exist on armv4t or armv4Rich Felker2012-07-141-1/+2
|
* fix several locks that weren't updated right for new futex-based __lockRich Felker2012-07-123-7/+7
| | | | | | these could have caused memory corruption due to invalid accesses to the next field. all should be fixed now; I found the errors with fgrep -r '__lock(&', which is bogus since the argument should be an array.
* fix pthread_kill unlockingRich Felker2012-07-121-1/+1
| | | | it had not been updated for the futex-based locks
* fix potential race condition in detached threadsRich Felker2012-07-112-4/+10
| | | | | | | after the thread unmaps its own stack/thread structure, the kernel, performing child tid clear and futex wake, could clobber a new mapping made at the same location as the just-removed thread's tid field. disable kernel clearing of child tid to prevent this.
* mips clone: don't free stack space used to copy argRich Felker2012-07-111-1/+0
| | | | | | the mips abi reserves stack space equal to the size of the in-register args for the callee to save the args, if desired. this would cause the beginning of the thread structure to be clobbered...
* fix mips clone() on real linux kernelRich Felker2012-07-111-4/+7
| | | | | | the old code worked in qemu app-level emulation, but not on real kernels where the clone syscall does not copy the register values to the new thread. save arguments on the new thread stack instead.
* fix clone() on mips (args were in wrong order)Rich Felker2012-07-111-3/+2
| | | | with this change, threads on mips seem to be working
* first attempt at making threads work on mipsRich Felker2012-07-113-0/+67
|
* generic c version of __set_thread_area for archs where it worksRich Felker2012-07-111-1/+5
| | | | on other archs, like x86[_64], asm version is required
* add pthread_attr_setstack interface (and get)Rich Felker2012-06-093-9/+37
| | | | | | | | | | | | | i originally omitted these (optional, per POSIX) interfaces because i considered them backwards implementation details. however, someone later brought to my attention a fairly legitimate use case: allocating thread stacks in memory that's setup for sharing and/or fast transfer between CPU and GPU so that the thread can move data to a GPU directly from automatic-storage buffers without having to go through additional buffer copies. perhaps there are other situations in which these interfaces are useful too.
* remove implementation-reserved bits when saving signal maskRich Felker2012-06-021-1/+11
| | | | | | | | this fix is necessary because a program could be started with some of the implementation-reserved signals masked (e.g. due to exec having been called from a signal handler, or from a non-musl program) and then could obtain an invalid-to-use-later sigset_t as the old/saved signal mask.
* remove no-longer-needed unblocking of signals in pthread_createRich Felker2012-06-021-1/+0
| | | | | | | this action is now performed in pthread_self initialization; it must be performed there in case the first call to pthread_create is from a signal handler, in which case the old signal mask could be restored on return from the signal.
* ensure pthread-internal signals are unblocked before threads are usedRich Felker2012-05-251-0/+1
| | | | | | | | | | | if the process started with these signals blocked, cancellation could fail or setxid could deadlock. there is no way to globally unblock them after threads have been created. by unblocking them in the pthread_self initialization for the main thread, we ensure that they're unblocked before any other threads are created and also outside of any signal handler context (sigaction initialized pthread_self), which is important so that return from a signal handler won't re-block them.
* remove leftover cp_sp cruft from cancellation code, fix small bugRich Felker2012-05-251-3/+1
| | | | | | | | | | | the bug was that cancellation requests which arrived while a cancellation point was interrupted by a signal handler would not be acted upon when the signal handler returns. this was because cp_sp was never set; it's no longer needed or used. instead, just always re-raise the signal when cancellation was not acted upon. this wastes a tiny amount of time in the rare case where it even matters, but it ensures correctness and simplifies the code.
* fix bad opcode in arm syscall_cp_asmRich Felker2012-05-231-1/+1
|
* fix issue with longjmp out of signal handlers and cancellationRich Felker2012-05-234-52/+35
| | | | | | | | | | | | | | | | | | | stale state information indicating that a thread was possibly blocked at a cancellation point could get left behind if longjmp was used to exit a signal handler that interrupted a cancellation point. to fix the issue, we throw away the state information entirely and simply compare the saved instruction pointer to a range of code addresses in the __syscall_cp_asm function. all the ugly PIC work (which becomes minimal anyway with this approach) is defered to cancellation time instead of happening at every syscall, which should improve performance too. this commit also fixes cancellation on arm, which was mildly broken (race condition, not checking cancellation flag once inside the cancellation point zone). apparently i forgot to implement that. the new arm code is untested, but appears correct; i'll test and fix it later if there are problems.
* simplify cancellation push/pop slightlyRich Felker2012-05-232-12/+9
| | | | | no need to pass unnecessary extra arguments on to the core code in pthread_create.c. this just wastes cycles and code bloat.
* remove everything related to forkallRich Felker2012-05-222-76/+0
| | | | | | | | | | i made a best attempt, but the intended semantics of this function are fundamentally contradictory. there is no consistent way to handle ownership of locks when forking a multi-threaded process. the code could have worked by accident for programs that only used normal mutexes and nothing else (since they don't actually store or care about their owner), but that's about it. broken-by-design interfaces that aren't even in glibc (only solaris) don't belong in musl.
* update license of njk contributed code (x86_64 asm)Rich Felker2012-05-052-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | these changes are based on the following communication via email: "I hereby grant that all of the code I have contributed to musl on or before April 23, 2012 may be licensed under the terms of the following MIT license: Copyright (c) 2011-2012 Nicholas J. Kain Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
* make pthread stacks non-executableRich Felker2012-05-041-1/+1
| | | | | | | | | | | | | this change is necessary or pthread_create will always fail on security-hardened kernels. i considered first trying to make the stack executable and simply retrying without execute permissions when the first try fails, but (1) this would incur a serious performance penalty on hardened systems, and (2) having the stack be executable is just a bad idea from a security standpoint. if there is real-world "GNU C" code that uses nested functions with threads, and it can't be fixed, we'll have to consider other ways of solving the problem, but for now this seems like the best fix.
* overhaul SSP support to use a real canaryRich Felker2012-05-031-0/+1
| | | | | | | | | | | | | pthread structure has been adjusted to match the glibc/GCC abi for where the canary is stored on i386 and x86_64. it will need variants for other archs to provide the added security of the canary's entropy, but even without that it still works as well as the old "minimal" ssp support. eventually such changes will be made anyway, since they are also needed for GCC/C11 thread-local storage support (not yet implemented). care is taken not to attempt initializing the thread pointer unless the program actually uses SSP (by reference to __stack_chk_fail).
* ditch the priority inheritance locks; use malloc's version of lockRich Felker2012-04-242-28/+8
| | | | | | | | | | | | | | | | | | | i did some testing trying to switch malloc to use the new internal lock with priority inheritance, and my malloc contention test got 20-100 times slower. if priority inheritance futexes are this slow, it's simply too high a price to pay for avoiding priority inversion. maybe we can consider them somewhere down the road once the kernel folks get their act together on this (and perferably don't link it to glibc's inefficient lock API)... as such, i've switch __lock to use malloc's implementation of lightweight locks, and updated all the users of the code to use an array with a waiter count for their locks. this should give optimal performance in the vast majority of cases, and it's simple. malloc is still using its own internal copy of the lock code because it seems to yield measurably better performance with -O3 when it's inlined (20% or more difference in the contention stress test).
* internal locks: new owner of contended lock must set waiters flagRich Felker2012-04-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | this bug probably would have gone unnoticed since it's only used in the fallback code for systems where priority-inheritance locking fails. unfortunately this approach results in one spurious wake syscall on the final unlock, when there are no waiters remaining. the alternative (possibly better) would be to use broadcast wakes instead of reflagging the waiter unconditionally, and let each waiter reflag itself; this saves one syscall at the expense of invoking the "thundering herd" effect (worse performance degredation) when there are many waiters. ideally we would be able to update all of our locks to use an array of two ints rather than a single int, and use a separate counter system like proper mutexes use; then we could avoid all spurious wake calls without resorting to broadcasts. however, it's not clear to me that priority inheritance futexes support this usage. the kernel sets the waiters flag for them (just like we're doing now) and i can't tell if it's safe to bypass the kernel when unlocking just because we know (from private data, the waiter count) that there are no waiters. this is something that could be explored in the future.
* new internal locking primitive; drop spinlocksRich Felker2012-04-241-6/+27
| | | | | | we use priority inheritance futexes if possible so that the library cannot hit internal priority inversion deadlocks in the presence of realtime priority scheduling (full support to be added later).
* fix pthread_cleanup_pop(1) crash in non-thread-capable, static-linked programsRich Felker2012-02-282-2/+2
|
* work around "signal loses thread pointer" issue with "approach 2"Rich Felker2012-02-271-2/+3
| | | | | | | | | | | | this was discussed on the mailing list and no consensus on the preferred solution was reached, so in anticipation of a release, i'm just committing a minimally-invasive solution that avoids the problem by ensuring that multi-threaded-capable programs will always have initialized the thread pointer before any signal handler can run. in the long term we may switch to initializing the thread pointer at program start time whenever the program has the potential to access any per-thread data.
* small fix for new pthread cleanup stuffRich Felker2012-02-092-1/+1
| | | | | | | even if pthread_create/exit code is not linked, run flag needs to be checked and cleanup function potentially run on pop. thus, move the code to the module that's always linked when pthread_cleanup_push/pop is used.
* replace bad cancellation cleanup abi with a sane oneRich Felker2012-02-093-62/+23
| | | | | | | | | | | | | | | | | | | | | | the old abi was intended to duplicate glibc's abi at the expense of being ugly and slow, but it turns out glib was not even using that abi except on non-gcc-compatible compilers (which it doesn't even support) and was instead using an exceptions-in-c/unwind-based approach whose abi we could not duplicate anyway without nasty dwarf2/unwind integration. the new abi is copied from a very old glibc abi, which seems to still be supported/present in current glibc. it avoids all unwinding, whether by sjlj or exceptions, and merely maintains a linked list of cleanup functions to be called from the context of pthread_exit. i've made some care to ensure that longjmp out of a cleanup function should work, even though it is not required to. this change breaks abi compatibility with programs which were using pthread cancellation, which is unfortunate, but that's why i'm making the change now rather than later. considering that most pthread features have not been usable until recently anyway, i don't see it as a major issue at this point.
* fix cancellation failure in single-threaded programsRich Felker2012-01-221-1/+1
| | | | | | | even a single-threaded program can be cancellable, e.g. if it's called pthread_cancel(pthread_self()). the correct predicate to check is not whether multiple threads have been invoked, but whether pthread_self has been invoked.
* report sem value overflows in sem_postRich Felker2011-10-261-0/+4
| | | | | this is not required by the standard, but it's nicer than corrupting the state and rather inexpensive.
* fix typo in arm clone() asmRich Felker2011-10-091-1/+1
|
* recovering ownerdead robust mutex must reset recursive lock countRich Felker2011-10-031-0/+1
|
* simplify robust mutex unlock code pathRich Felker2011-10-031-4/+4
| | | | | | | right now it's questionable whether this change is an improvement or not, but if we later want to support priority inheritance mutexes, it will be important to have the code paths unified like this to avoid major code duplication.
* fix crash if pthread_mutex_unlock is called without ever lockingRich Felker2011-10-031-1/+1
| | | | | this is valid for error-checking mutexes; otherwise it invokes UB and would be justified in crashing.
* use count=0 instead of 1 for recursive mutex with only one lock referenceRich Felker2011-10-032-4/+2
| | | | | | | this simplifies the code paths slightly, but perhaps what's nicer is that it makes recursive mutexes fully reentrant, i.e. locking and unlocking from a signal handler works even if the interrupted code was in the middle of locking or unlocking.
* synchronize cond var destruction with exiting waitsRich Felker2011-10-022-0/+11
|
* fix failure-to-wake in rwlock unlockRich Felker2011-10-011-1/+1
| | | | | | | | | a reader unlocking the lock need only wake one waiter (necessarily a writer, but a writer unlocking the lock must wake all waiters (necessarily readers). if it only wakes one, the remainder can remain blocked indefinitely, or at least until the first reader unlocks (in which case the whole lock becomes serialized and behaves as a mutex rather than a read lock).
* fix excessive/insufficient wakes in __vm_unlockRich Felker2011-09-281-3/+3
| | | | | | there is no need to send a wake when the lock count does not hit zero, but when it does, all waiters must be woken (since all with the same sign are eligible to obtain the lock).
* improve pshared barriersRich Felker2011-09-281-11/+13
| | | | | | | | | | | | | | eliminate the sequence number field and instead use the counter as the futex because of the way the lock is held, sequence numbers are completely useless, and this frees up a field in the barrier structure to be used as a waiter count for the count futex, which lets us avoid some syscalls in the best case. as of now, self-synchronized destruction and unmapping should be fully safe. before any thread can return from the barrier, all threads in the barrier have obtained the vm lock, and each holds a shared lock on the barrier. the barrier memory is not inspected after the shared lock count reaches 0, nor after the vm lock is released.
* next step making barrier self-sync'd destruction safeRich Felker2011-09-282-6/+18
| | | | i think this works, but it can be simplified. (next step)
* barrier destroy must also wait for threads in other processes exiting barrierRich Felker2011-09-281-0/+2
| | | | | the vm lock only waits for threads in the same process exiting. actually this fix is not enough, but it's a start...
* correctly handle the degenerate barrier in the pshared caseRich Felker2011-09-271-1/+1
|
* fix crash in pthread_cond_wait mutex-locked checkRich Felker2011-09-271-1/+1
| | | | | | it was assuming the result of the condition it was supposed to be checking for, i.e. that the thread ptr had already been initialized by pthread_mutex_lock. use the slower call to be safe.
* fix crash in pthread_testcancel if pthread_self has not been calledRich Felker2011-09-271-1/+1
|
* improve/debloat mutex unlock error checking in pthread_cond_waitRich Felker2011-09-271-3/+3
| | | | | | | we're not required to check this except for error-checking mutexes, but it doesn't hurt. the new test is actually simpler/lighter, and it also eliminates the need to later check that pthread_mutex_unlock succeeds.
* check mutex owner in pthread_cond_waitRich Felker2011-09-271-0/+3
| | | | | | | | | | when used with error-checking mutexes, pthread_cond_wait is required to fail with EPERM if the mutex is not locked by the caller. previously we relied on pthread_mutex_unlock to generate the error, but this is not valid, since in the case of such invalid usage the internal state of the cond variable has already been potentially corrupted (due to access outside the control of the mutex). thus, we have to check first.
* fix pshared barrier wrong return value.Rich Felker2011-09-271-1/+1
| | | | i set the return value but then never used it... oops!