summaryrefslogtreecommitdiff
path: root/lib/libpthread/pthread_cond.c
AgeCommit message (Collapse)Author
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.)
2020-06-14Another bug. The CAS loop in pthread_cond_signal() could race against thead
thread it is trying to awake. The thread could exit the condvar and then reinsert itself at the head of the list with a new waiter behind it. It's likely possible to fix this in a way that's wait-free but for now just fix the bug.
2020-06-13Nix trailing whitespace.riastradh
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-06Adjust previous. In the condvar case the wakeup might already have beenad
eaten.
2020-06-04Nix trailing whitespace. NFCI.riastradh
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-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-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-01-29Use pthread_condattr_t and pthread_cond_t magic fieldskamil
Validate _PT_CONDATTR_MAGIC and _PT_COND_MAGIC respectively.
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.
2017-12-08unconst the timestampchristos
2016-07-03GSoC 2016 Charles Cui: Implement thread priority protection based on workchristos
by Andy Doran. Also document the get/set pshared thread calls as not implemented, and add a skeleton implementation that is disabled. XXX: document _sched_protect(2).
2014-01-31remove compatibility code for handling CLOCK_MONOTONIC and handle it in thechristos
syscall directly.
2014-01-31PR/44756: Sad Clouds: Prevent leakage of errno = ESRCH from _lwp_park. Thischristos
has two parts: - in pthread_cond_timedwait() if the thread we are trying to unpark exited, retry the the _lwp_park call without it. - pthread_mutex() was affecting errno since it is calling _lwp_park() from pthread_mutex_lock_slow(). preserve the original errno. Note that the example problem still causes an occassional deadlock on machines with many CPUs and it is the same deadlock we observe with named.
2013-04-01for safety, declare mono on the outermost block it is used.christos
2013-03-28PR/47703: Yasushi Oshima: pthread_cond_timedwait() does not waitchristos
after call pthread_condattr_setclock(CLOCK_MONOTONIC) _lwp_park(2) expects a realtime clock, and it gets passed a monotonic one. Since monotonic < real, it never sleeps. This patch adjusts the monotonic clock to be a real one before it passes is to _lwp_park(2). This is the minimal hacky fix and it will be fixed properly in _lwp_park(2) in the future. XXX: pullup to 6.
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
2012-11-03add pthread_condattr_setclock(3)christos
2012-06-15Do not mark pthread_cond_timedwait explicitly as inlineable, since itjoerg
calls pthread__self, which is static.
2010-11-02Spell immediately correctly.skrll
2010-03-23catch up with the __RENAME of nanosleep(2) a while ago, otherwise wedrochner
get the compatibility function which assumes a different struct timespec
2009-01-18fix -Wsign-compare issueslukem
2008-10-25remove ; after __weak_alias()yamt
2008-08-02Change some type to eliminate some lint warnings.matt
2008-07-18add pthread_cond_has_waiters_np()pooka
2008-06-28Avoid spurious assertion failure.ad
2008-06-23Split cond_signal/cond_broadcast into inline and non-inline parts, likead
the kernel.
2008-06-21PR lib/38948: libpthread, java: thread awakening itselfad
2008-05-26Add a comment describing some limitiations of this implementation.ad
2008-05-26- Eliminate one test+branch.ad
- Fix a comment. - Fix a lock leak.
2008-05-25pthread_cond_timedwait: don't leak EINTR or EALREADY to the caller.ad
2008-05-25Fix error in previous.ad
2008-05-25PR lib/38741 priority inversion in libpthread breaks apps that usead
SCHED_FIFO threads - Change condvar sync so that we never take the condvar's spinlock without first holding the caller-provided mutex. Previously, the spinlock was only taken without the mutex in an error path, but it was enough to trigger the problem described in the PR. - Even with this change, applications calling pthread_cond_signal/broadcast without holding the interlocking mutex are still subject to the problem described in the PR. POSIX discourages this saying that it leads to undefined scheduling behaviour, which seems good enough for the time being. - Elsewhere, use a hash of mutexes instead of per-object spinlocks to synchronize entry/exit from sleep queues. - Simplify how sleep queues are maintained.
2008-04-28Remove clause 3 and 4 from TNF licensesmartin
2008-02-14Adjust mutex/rwlock definitions to match reality now that there is onlyad
one implementation of each. PR lib/38030.
2007-12-24- Use pthread__cancelled() in more places.ad
- pthread_join(): assert that pthread_cond_wait() returns zero.
2007-11-19Remove the debuglog stuff. ktrace is more useful now.ad
2007-11-13For PR bin/37347:ad
- Override __libc_thr_init() instead of using our own constructor. - Add pthread__getenv() and use instead of getenv(). This is used before we are up and running and unfortunatley getenv() takes locks. Other changes: - Cache the spinlock vectors in pthread__st. Internal spinlock operations now take 1 function call instead of 3 (i386). - Use pthread__self() internally, not pthread_self(). - Use __attribute__ ((visibility("hidden"))) in some places. - Kill PTHREAD_MAIN_DEBUG.
2007-09-13Add a per-mutex deferred wakeup flag so that threads doing something likead
the following do not wake other threads early: pthread_mutex_lock(&mutex); pthread_cond_broadcast(&cond); foo = malloc(100); /* takes libc mutexes */ pthread_mutex_unlock(&mutex);
2007-09-08- Get rid of self->pt_mutexhint and use pthread__mutex_owned() instead.ad
- Update some comments and fix minor bugs. Minor cosmetic changes. - Replace some spinlocks with mutexes and rwlocks. - Change the process private semaphores to use mutexes and condition variables instead of doing the synchronization directly. Spinlocks are no longer used by the semaphore code.
2007-09-07- Don't take the mutex's spinlock (ptr_interlock) in pthread_cond_wait().ad
Instead, make the deferred wakeup list a per-thread array and pass down the lwpid_t's that way. - In pthread_cond_wait(), take the mutex before dealing with early wakeup. In this way there should never be contention on the CV's spinlock if the app follows POSIX rules (there should only be contention on the user-provided mutex). - Add a port of the kernel's rwlocks. The rwlock's spinlock is only taken if there is contention. This is enabled where atomic ops are available. Right now that is only i386 and amd64 because I don't have other hardware to test with. It's trivial to add stubs for other architectures as long as they have compare-and-swap. When we have proper atomic ops the old rwlock code can be removed. - Add a new mutex implementation that's similar to the kernel's mutexes, but uses compare-and-swap to maintain the waiters list, so no spinlocks are involved. Same caveats apply as for the rwlocks.
2007-08-16Trim fat off libpthread internal spinlock operations. Makes a mesurablead
improvement across the board.
2007-08-07Change the signature of _lwp_park() to accept an lwpid_t and secondad
hint pointer, but do so in a way that remains compatible with older pthread libraries. This can be used to wake another thread before the calling thread goes asleep, saving at least one syscall + involuntary context switch. This turns out to be a fairly large win on the condvar benchmarks that I have tried.
2007-08-04Some significant performance improvements, and a fix for a race with pthreadad
detach/join. - Make mutex acquire spin for a short time, as done with spinlocks. - Make the number of spins controllable with the env var PTHREAD_NSPINS. - Reduce the amount of time that libpthread internal spinlocks are held. - Rely more on the barrier effects of park/unpark to avoid taking spinlocks. - Simplify the locking around pthreads and the global queues. - Align per-thread sync data on a 128 byte boundary. - Offset thread stacks by a small amount to try and reduce cache thrash.
2007-04-12Mirror a fix made to the kernel's condvars:ad
After resuming execution, the thread must check to see if it has been restarted as a result of pthread_cond_signal(). If it has, but cannot take the wakeup (because of eg a pending Unix signal or timeout) then try to ensure that another thread sees it. This is necessary because there may be multiple waiters, and at least one should take the wakeup if possible.
2007-03-24- Test+branch is usually cheaper than making an indirect function call,ad
so avoid making them. - When parking an LWP on a condition variable, point the hint argument at the mutex's waiters queue. Chances are we will be awoken from that later.
2007-03-21Move PTHREADD_ADD(PTHREADD_COND_WOKEUP) back to the correct spot.ad
2007-03-20- When signalling waiters, try not to awaken them immediatley. If we holdad
the mutex that the waiters are using to synchronise, then transfer them to the mutex's waiters list so that the wakeup is deferred until release of the mutex. Improves the timings for CV sleep/wakeup by between 30-100% in tests conducted locally on a UP system. There can be a penalty for MP systems when only one thread is being awoken, but in practice I think it won't be be an issue. - pthread_signal: search for a thread that does not have a pending wakeup. Threads can have a pending wakeup and still be on the waiters list if we clash with an earlier pthread_cond_broadcast().