summaryrefslogtreecommitdiff
path: root/src/thread
AgeCommit message (Collapse)AuthorLines
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.
2013-02-01if pthread_create fails, it must not attempt mmap if there is no mappingRich Felker-1/+1
this bug was introduced when support for application-provided stacks was originally added.
2013-02-01pthread stack treatment overhaul for application-provided stacks, etc.Rich Felker-23/+33
the main goal of these changes is to address the case where an application provides a stack of size N, but TLS has size M that's a significant portion of the size N (or even larger than N), thus giving the application less stack space than it expected or no stack at all! the new strategy pthread_create now uses is to only put TLS on the application-provided stack if TLS is smaller than 1/8 of the stack size or 2k, whichever is smaller. this ensures that the application always has "close enough" to what it requested, and the threshold is chosen heuristically to make sure "sane" amounts of TLS still end up in the application-provided stack. if TLS does not fit the above criteria, pthread_create uses mmap to obtain space for TLS, but still uses the application-provided stack for actual call frame stack. this is to avoid wasting memory, and for the sake of supporting ugly hacks like garbage collection based on assumptions that the implementation will use the provided stack range. in order for the above heuristics to ever succeed, the amount of TLS space wasted on POSIX TSD (pthread_key_create based) needed to be reduced. otherwise, these changes would preclude any use of pthread_create without mmap, which would have serious memory usage and performance costs for applications trying to create huge numbers of threads using pre-allocated stack space. the new value of PTHREAD_KEYS_MAX is the minimum allowed by POSIX, 128. this should still be plenty more than real-world applications need, especially now that C11/gcc-style TLS is now supported in musl, and most apps and libraries choose to use that instead of POSIX TSD when available. at the same time, PTHREAD_STACK_MIN has been decreased. it was originally set to PAGE_SIZE back when there was no support for TLS or application-provided stacks, and requests smaller than a whole page did not make sense. now, there are two good reasons to support requests smaller than a page: (1) applications could provide pre-allocated stacks smaller than a page, and (2) with smaller stack sizes, stack+TLS+TSD can all fit in one page, making it possible for applications which need huge numbers of threads with minimal stack needs to allocate exactly one page per thread. the new value of PTHREAD_STACK_MIN, 2k, is aligned with the minimum size for sigaltstack.
2013-02-01replace __wake function with macro that performs direct syscallRich Felker-9/+0
this should generate faster and smaller code, especially with inline syscalls. the conditional with cnt is ugly, but thankfully cnt is always a constant anyway so it gets evaluated at compile time. it may be preferable to make separate __wake and __wakeall macros without a count argument. priv flag is not used yet; private futex support still needs to be done at some point in the future.
2012-11-27fix some restrict-qualifier mismatches in newly added interfacesRich Felker-2/+2
these should have little/no practical impact but they're needed for strict conformance.
2012-11-19powerpc: handle syscall error in clone.rofl0r-25/+9
sigsetjmp: store temporaries in jmp_buf rather than on stack.
2012-11-18fix powerpc asm not to store data in volatile space below stack pointerRich Felker-4/+4
it's essential to decrement the stack pointer before writing to new stack space, rather than afterwards. otherwise there is a race condition during which asynchronous code (signals) could clobber the data being stored. it may be possible to optimize the code further using stwu, but I wanted to avoid making any changes to the actual stack layout in this commit. further improvements can be made separately if desired.
2012-11-17add stub versions of some missing optional pthread interfacesRich Felker-0/+30
priority inheritance is not yet supported, and priority protection probably will not be supported ever unless there's serious demand for it (it's a fairly heavy-weight feature). per-thread cpu clocks would be nice to have, but to my knowledge linux is still not capable of supporting them. glibc fakes them by using the _process_ cpu-time clock and subtracting the thread creation time, which gives seriously incorrect semantics (worse than not supporting the feature at all), so until there's a way to do it right, it will remain as a stub that always fails.
2012-11-14fix indention with spaces in powerpc asmRich Felker-8/+8
2012-11-14Merge remote-tracking branch 'ppc-port/ppc-squashed'Rich Felker-0/+154
2012-11-13PPC port cleaned up, static linking works well now.rofl0r-4/+147
2012-11-13import preliminary ppc work by rdp.Richard Pennington-0/+11
2012-11-11debloat src/thread tree but putting lots of junk in one fileRich Felker-108/+93
POSIX includes mostly-useless attribute-get functions for each attribute-set function, presumably out of some object-oriented dogmatism. the get functions are not useful with the simple idiomatic usage of attributes. there are of course possible valid uses of them (like writing wrappers for pthread init functions that perform special actions on the presence of certain attributes), but considering how tiny these functions are anyway, little is lost by putting them all in one file, and some build-time cost and archive-file-size benefits are achieved.
2012-11-11add support for thread scheduling (POSIX TPS option)Rich Felker-4/+106
linux's sched_* syscalls actually implement the TPS (thread scheduling) functionality, not the PS (process scheduling) functionality which the sched_* functions are supposed to have. omitting support for the PS option (and having the sched_* interfaces fail with ENOSYS rather than omitting them, since some broken software assumes they exist) seems to be the only conforming way to do this on linux.
2012-11-08clean up sloppy nested inclusion from pthread_impl.hRich Felker-8/+13
this mirrors the stdio_impl.h cleanup. one header which is not strictly needed, errno.h, is left in pthread_impl.h, because since pthread functions return their error codes rather than using errno, nearly every single pthread function needs the errno constants. in a few places, rather than bringing in string.h to use memset, the memset was replaced by direct assignment. this seems to generate much better code anyway, and makes many functions which were previously non-leaf functions into leaf functions (possibly eliminating a great deal of bloat on some platforms where non-leaf functions require ugly prologue and/or epilogue).
2012-10-19fix order of syscall args for microblaze clone syscallRich Felker-3/+2
with this commit, based on testing with patches to qemu which are not yet upstream,
2012-10-18ensure microblaze __set_thread_area returns successRich Felker-1/+2
since it did not set the return-value register, the caller could wrongly interpret this as failure.
2012-10-17fix microblaze asm relocations for shared libcRich Felker-1/+1
only @PLT relocations are considered functions for purposes of -Bsymbolic-functions, so always use @PLT. it should not hurt in the static-linked case.
2012-10-15add support for TLS variant I, presently needed for arm and mipsRich Felker-2/+6
despite documentation that makes it sound a lot different, the only ABI-constraint difference between TLS variants II and I seems to be that variant II stores the initial TLS segment immediately below the thread pointer (i.e. the thread pointer points to the end of it) and variant I stores the initial TLS segment above the thread pointer, requiring the thread descriptor to be stored below. the actual value stored in the thread pointer register also tends to have per-arch random offsets applied to it for silly micro-optimization purposes. with these changes applied, TLS should be basically working on all supported archs except microblaze. I'm still working on getting the necessary information and a working toolchain that can build TLS binaries for microblaze, but in theory, static-linked programs with TLS and dynamic-linked programs where only the main executable uses TLS should already work on microblaze. alignment constraints have not yet been heavily tested, so it's possible that this code does not always align TLS segments correctly on archs that need TLS variant I.
2012-10-14fix overlap of thread stacks with thread tls segmentsRich Felker-2/+1
2012-10-07clean up and refactor program initializationRich Felker-1/+1
the code in __libc_start_main is now responsible for parsing auxv, rather than duplicating the parsing all over the place. this should shave off a few cycles and some code size. __init_libc is left as an external-linkage function despite the fact that it could be static, to prevent it from being inlined and permanently wasting stack space when main is called. a few other minor changes are included, like eliminating per-thread ssp canaries (they were likely broken when combined with certain dlopen usages, and completely unnecessary) and some other unnecessary checks. since this code gets linked into every program, it should be as small and simple as possible.
2012-10-05support for TLS in dynamic-loaded (dlopen) modulesRich Felker-23/+28
unlike other implementations, this one reserves memory for new TLS in all pre-existing threads at dlopen-time, and dlopen will fail with no resources consumed and no new libraries loaded if memory is not available. memory is not immediately distributed to running threads; that would be too complex and too costly. instead, assurances are made that threads needing the new TLS can obtain it in an async-signal-safe way from a buffer belonging to the dynamic linker/new module (via atomic fetch-and-add based allocator). I've re-appropriated the lock that was previously used for __synccall (synchronizing set*id() syscalls between threads) as a general pthread_create lock. it's a "backwards" rwlock where the "read" operation is safe atomic modification of the live thread count, which multiple threads can perform at the same time, and the "write" operation is making sure the count does not increase during an operation that depends on it remaining bounded (__synccall or dlopen). in static-linked programs that don't use __synccall, this lock is a no-op and has no cost.
2012-10-04beginnings of full TLS support in shared librariesRich Felker-0/+8
this code will not work yet because the necessary relocations are not supported, and cannot be supported without some internal changes to how relocation processing works (coming soon).
2012-10-04TLS (GNU/C11 thread-local storage) support for static-linked programsRich Felker-14/+33
the design for TLS in dynamic-linked programs is mostly complete too, but I have not yet implemented it. cost is nonzero but still low for programs which do not use TLS and/or do not use threads (a few hundred bytes of new code, plus dependency on memcpy). i believe it can be made smaller at some point by merging __init_tls and __init_security into __libc_start_main and avoiding duplicate auxv-parsing code. at the same time, I've also slightly changed the logic pthread_create uses to allocate guard pages to ensure that guard pages are not counted towards commit charge.
2012-09-30protect sem_open against cancellationRich Felker-13/+19
also fix one minor bug: failure to free the early-reserved slot when the semaphore later found to already be mapped.
2012-09-30overhaul sem_openRich Felker-105/+96
this function was overly complicated and not even obviously correct. avoid using openat/linkat just like in shm_open, and instead expand pathname using code shared with shm_open. remove bogus (and dangerous, with priorities) use of spinlocks. this commit also heavily streamlines the code and ensures there are no failure cases that can happen after a new semaphore has been created in the filesystem, since that case is unreportable.
2012-09-29sem_open should make process-shared semaphoresRich Felker-1/+1
this did not matter because we don't yet treat process-shared special. when private futex support is added, however, it will matter.
2012-09-29use O_CLOEXEC to open semaphore files in sem_openRich Felker-2/+2
2012-09-29fix some indention-with-spaces that crept inRich Felker-4/+4
2012-09-29microblaze portRich Felker-0/+65
based on initial work by rdp, with heavy modifications. some features including threads are untested because qemu app-level emulation seems to be broken and I do not have a proper system image for testing.
2012-09-27fix arm clone syscall bug (no effect unless app uses clone)Rich Felker-2/+1
the code to exit the new thread/process after the start function returns was mixed up in its syscall convention.