summaryrefslogtreecommitdiff
path: root/lib/libpthread/pthread.c
AgeCommit message (Collapse)Author
2023-03-24Use snprintf_ss in pthread__assertfunc and update comment injoerg
pthread__errorfunc. snprintf can use locks in some code paths and we only care about the restricted subset here.
2022-05-31libpthread(3): Fix a marvellous interaction with rtld.riastradh
Patch from chs@. Comment explaining the story by me. This patch may not be optimal -- maybe it would be better in pthread__init, or better for rtld to call _lwp_unpark after _lwp_park in the contened case -- but we've tested this version and it's annoying to reproduce, so let's take this version and worry about testing improvements later.
2022-02-12libpthread: Move namespacing include to top of .c files.riastradh
Stuff like libc's namespace.h, or atomic_op_namespace.h, which does namespacing tricks like `#define atomic_cas_uint _atomic_cas_uint', has to go at the top of each .c file. If it goes in the middle, it might be too late to affect the declarations, and result in compile errors. I tripped over this by including <sys/atomic.h> in mips <machine/lock.h>. (Maybe we should create a new pthread_namespace.h file for the purpose, but this'll do for now.)
2021-04-13fake-use alloca()'s return value to quieten -Werror=unused-resultmrg
2020-07-22s/reseting/resetting/msaitoh
2020-06-14Don't need to ignore ESRCH from _lwp_park() any more.ad
2020-06-11Drop self->pt_lock before clearing TSD / malloc TSD.ad
2020-06-10- Make pthread_condvar and pthread_mutex work on the stack rather than inad
pthread_t, so there's less chance of bad things happening if someone calls (for example) pthread_cond_broadcast() from a signal handler. - Remove all the deferred waiter handling except for the one case that really matters which is transferring waiters from condvar -> mutex on wakeup, and do that by splicing the condvar's waiters onto the mutex. - Remove the mutex waiters bit as it's another complication that's not strictly needed.
2020-06-04If _malloc_thread_cleanup is implement, call it from libpthread.joerg
Provide the hook from modern jemalloc to avoid using TSD for the thread destruction cleanup as it can result in reentrancy crashes if fork is called from a thread that never called malloc as it will result in a late malloc from the pre-fork synchronisation handler.
2020-06-03Deal with a couple of problems with threads being awoken early due toad
timeouts or cancellation where: - The restarting thread calls _lwp_exit() before another thread gets around to waking it with _lwp_unpark(), leading to ESRCH (observed by joerg@). (I may have removed a similar check mistakenly over the weekend.) - The restarting thread considers itself gone off the sleep queue but at the same time another thread is part way through waking it, and hasn't fully completed that operation yet by setting thread->pt_mutexwait = 0. I think that could have potentially lead to the list of waiters getting messed up given the right circumstances.
2020-06-02Pass down errno when calling pthread__errorfunc after a system call.joerg
Allow format arguments for that reason and use (v)snprintf_ss in pthread_errorfunc to avoid race conditions and the like.
2020-06-01In the interests of reliability simplify waiter handling more and redoad
condvars to manage the list of waiters with atomic ops.
2020-05-16- Try to eliminate a hang in "parked" I've been seeing while stress testing.ad
Centralise wakeup of deferred waiters in pthread__clear_waiters() and use throughout libpthread. Make fewer assumptions. Be more conservative in pthread_mutex when dealing with pending waiters. - Remove the "hint" argument everywhere since the kernel doesn't use it any more.
2020-05-15Lock/unlock/reinit pthread__deadqueue_lock over fork.joerg
2020-04-14Drop most of the logic associated with pthread__started.joerg
The pthread_cond logic is a questionable optimisation at best and the post-fork logic is plainly broken.
2020-02-16Revert "Enhance the pthread(3) + malloc(3) init model"kamil
It is reported to hand on aarch64 with gzip.
2020-02-16Set __isthreaded before bootstrapping malloc(3)kamil
jemalloc depends on the __isthreaded dynamic state logic. Reported by <wiz> for mpv and by <tih> for gzip.
2020-02-15Enhance the pthread(3) + malloc(3) init modelkamil
Separate the pthread_atfork(3) call from pthread_tsd_init() and move it into a distinct function. Call inside pthread__init() late TSD initialization route, just after "pthread_atfork(NULL, NULL, pthread__fork_callback);". Document that malloc(3) initialization is now controlled again and called during the first pthread_atfork(3) call. Remove #if 0 code from pthread_mutex.c as we no longer initialize malloc prematurely.
2020-02-08Change the behavior of pthread_equal()kamil
On error when not aborting, do not return EINVAL as it has a side effect of being interpreted as matching threads. For invalid threads return unmatched. Check pthreads for NULL, before accessing pt_magic field. This avoids faults on comparision with a NULL pointer. This behavior is in the scope of UB, but should be easier to deal with buggy software.
2020-02-05Remove trailing whiteapaces and tabryoon
2020-01-29- pthread_join(): remove temporary hack now kernel returns correct errno.ad
- kill(getpid(), SIGABRT) -> _lwp_kill(_lwp_self(), SIGABRT)
2020-01-29Chack thread->pt_magic with PT_MAGIC promptlykamil
Rearrange some checks to avoid verifying pthread_t after using it.
2020-01-29Revert previouskamil
Two assignments are correct.
2020-01-29Do not set stackbase2 twice for !__MACHINE_STACK_GROWS_UPkamil
2020-01-28pthread_join(): add a temporary hack to make lib/libpthread/t_detach pass.ad
The correct fix is to do this in kernel (I have that change, but it's part of the wider change to index LWPs in a tree).
2020-01-27pthread_detach(), pthread_join(): go back to using _lwp_detach() andad
_lwp_wait(), rather than doing it all in userspace. There's less to go wrong. Doesn't seem to be a performance penalty.
2020-01-25pthread_exit(): it looks there there is at least one path through whichad
a thread can exit with waiters still hanging off it (cancellation when waiting on a condvar) so deal with all/any crappy failure like that and make sure there are never any waiters left before exiting. Maybe of help for: PR: bin/50350: rump/rumpkern/t_sp/stress_{long,short} fail on Core 2
2020-01-25- Fix a race between the kernel and libpthread, where a new thread can startad
life without its self->pt_lid being filled in. - Fix an error path in _lwp_create(). If the new LID can't be copied out, then get rid of the new LWP (i.e. either succeed or fail, not both). - Mark l_dopreempt and l_nopreempt volatile in struct lwp.
2020-01-13Rip out some very ambitious optimisations around pthread_mutex that aread
don't buy much. This stuff is hard enough to get right in the kernel let alone userspace, and I don't trust that it's right.
2019-03-05Transfer all the keys that were created in the libc stub implementationchristos
to the pthread tsd implementation when the main thread is created. This corrects a problem where a process created keys before libpthread was loaded (either from the libc constructor or because libpthread was dlopened later). This fixes a problem with jemalloc which creates keys in the constructor.
2018-08-19Drop a duplicate instruction linekamil
No functional change intended.
2017-12-08Deal with more lwp_park() timestamp unconstingkre
2017-07-11Implement __cxa_thread_atexit and __cxa_thread_atexit_impl. Thisjoerg
functions are used for destructors of thread_local objects. If a pending destructor exists, prevent unloading of shared objects. Introduce __dl_cxa_refcount interface for this purpose. When the last reference is gone and the object has been dlclose'd before, the unloading is finalized. Ideally, __cxa_thread_atexit_impl wouldn't exist, but libstdc++ insists on providing __cxa_thread_atexit as direct wrapper without further patching.
2017-07-02Do not look at environmental variables for suid/guid binaries.joerg
2017-07-02Export the guard size of the main thread via vm.guard_size. Add ajoerg
complementary writable sysctl for the initial guard size of threads created via pthread_create. Let the existing attribut accessors do the right thing. Raise the default guard size for threads to 64KB.
2015-05-29Fix previous: Can't use calloc/malloc before we complete initializationchristos
of the thread library, because malloc uses pthread_foo_specific, and it will end up initializing itself incorrectly.
2015-05-29Make PTHREAD_KEYS_MAX dynamically adjustablemanu
NetBSD's PTHREAD_KEYS_MAX is set to 256, which is low compared to other systems like Linux (1024) or MacOS X (512). As a result some setups tested on Linux will exhibit problems on NetBSD because of pthread_keys usage beyond the limit. This happens for instance on Apache with various module loaded, and in this case no particular developper can be blamed for going beyond the limit, since several modules from different sources contribute to the problem. This patch makes the limit conigurable through the PTHREAD_KEYS_MAX environement variable. If undefined, the default remains unchanged (256). In any case, the value cannot be lowered below POSIX-mandated _POSIX_THREAD_KEYS_MAX (128). While there: - use EXIT_FAILURE instead of 1 when calling err(3) in libpthread. - Reset _POSIX_THREAD_KEYS_MAX to POSIX mandated 128, instead of 256.
2014-12-16Allow for arbitrary MI scheduler implementations.pooka
A concrete result is enabling unpatched libpthread to run on the rumprun stacks (e.g. Xen and bare metal) with a non-NetBSD scheduler. Those schedulers hook into the existing _lwp_frobnitz() NetBSD syscall interfaces (well, "syscall" interfaces in that scenario ;) More specifically about the change itself: 1) instead of calling _lwp_makecontext() followed by _lwp_create() and passing the entry point in ucontext_t (MD) through the calls, roll the calls into pthread__makelwp() and allow alternate implementations for that MI interface. 2) allow compile-time overriding of __lwp_gettcb_fast() or __lwp_getprivate_fast, which are inline and leak MD scheduler/thread details into libpthread Additionally, two small nits: I) define LIB=pthread before including mk.conf so that it's possible to test for LIB==pthread in mk.conf II) make it possible to leave out pthread_cancelstub.c. This is required by the current implementation of rumprun-posix (i.e. rumprun on POSIX hosts) due to symbol collisions. It needs to be fixed properly some day, but for now allows an almost-correct libpthread to run. I am sure @justin will be happy to explain the details ;) no change to NetBSD tested: anita+atf
2014-01-31remove compatibility code for handling CLOCK_MONOTONIC and handle it in thechristos
syscall directly.
2013-03-21- Allow libpthread to be dlopened again, by providing libc stubs to libpthread.christos
- Fail if the dlopened libpthread does pthread_create(). From manu@ - Discussed at length in the mailing lists; approved by core@ - This was chosen as the least intrusive patch that will provide the necessary functionality. XXX: pullup to 6
2013-01-01Don't rely on sys/lwp.h includeing sys/resource.h for us.dsl
2012-11-14add argsusedchristos
2012-11-06Remove duplicate "const" modifier found by clang -Wduplicate-decl-specifierapb
2012-11-03libpthread: replace the use of obsolete sys/tree.h interface with rbtree(9).rmind
2012-11-03cast return of sysconfchristos
2012-08-15return errno if pthread_create hits the system limit, not just -1drochner
(this is not entirely correct because it can return ENOMEM which is not mentioned in the spec, but there are other places in pthread_create whete ENOMEM is returned -- it at all, this should be fixed everywhere)
2012-08-02Prefer sysconf to obtain number of CPUs.joerg
2012-05-04Simplify check for TLS definition to not hide code. Drop it in anotherjoerg
place as it is redundant.
2012-04-26Store allocated lwpctl state in the thread actually forked,enami
which is the only thread lives in the child process. The problem originally reported here: https://bugs.ruby-lang.org/issues/6341
2012-03-22don't reuse a dynamically allocated stack if a fixed one is requesteddrochner