Commit Graph

54 Commits

Author SHA1 Message Date
drochner 1982972a07 catch up with the __RENAME of nanosleep(2) a while ago, otherwise we
get the compatibility function which assumes a different struct timespec
2010-03-23 20:35:44 +00:00
lukem a06595c27b fix -Wsign-compare issues 2009-01-18 12:14:16 +00:00
yamt 639a0868fd remove ; after __weak_alias() 2008-10-25 14:14:11 +00:00
matt c0038aadef Change some type to eliminate some lint warnings. 2008-08-02 16:02:26 +00:00
pooka 6ebb8696c9 add pthread_cond_has_waiters_np() 2008-07-18 16:17:11 +00:00
ad 27e50f7c64 Avoid spurious assertion failure. 2008-06-28 10:37:20 +00:00
ad 10ba267505 Split cond_signal/cond_broadcast into inline and non-inline parts, like
the kernel.
2008-06-23 11:01:19 +00:00
ad 49b506e72f PR lib/38948: libpthread, java: thread awakening itself 2008-06-21 11:27:41 +00:00
ad bd2852ae8c Add a comment describing some limitiations of this implementation. 2008-05-26 02:06:21 +00:00
ad 6458d64f4c - Eliminate one test+branch.
- Fix a comment.
- Fix a lock leak.
2008-05-26 00:16:35 +00:00
ad dec5fb2e52 pthread_cond_timedwait: don't leak EINTR or EALREADY to the caller. 2008-05-25 23:51:31 +00:00
ad 49ec182c8c Fix error in previous. 2008-05-25 17:11:13 +00:00
ad 2bcb8bf1c4 PR lib/38741 priority inversion in libpthread breaks apps that use
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-05-25 17:05:28 +00:00
martin ce099b4099 Remove clause 3 and 4 from TNF licenses 2008-04-28 20:22:51 +00:00
ad 377f098ab0 Adjust mutex/rwlock definitions to match reality now that there is only
one implementation of each. PR lib/38030.
2008-02-14 21:40:51 +00:00
ad 622bbc505a - Use pthread__cancelled() in more places.
- pthread_join(): assert that pthread_cond_wait() returns zero.
2007-12-24 16:04:20 +00:00
ad 8077340e63 Remove the debuglog stuff. ktrace is more useful now. 2007-11-19 15:14:11 +00:00
ad 15e9cec117 For PR bin/37347:
- 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-11-13 15:57:10 +00:00
ad 20e3392edc Add a per-mutex deferred wakeup flag so that threads doing something like
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-13 23:51:47 +00:00
ad f4fd6b797e - Get rid of self->pt_mutexhint and use pthread__mutex_owned() instead.
- 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-08 22:49:50 +00:00
ad 8ccc6e060d - Don't take the mutex's spinlock (ptr_interlock) in pthread_cond_wait().
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-09-07 14:09:27 +00:00
ad d9adedd764 Trim fat off libpthread internal spinlock operations. Makes a mesurable
improvement across the board.
2007-08-16 13:54:16 +00:00
ad c3f8e2ee55 Change the signature of _lwp_park() to accept an lwpid_t and second
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-07 19:04:21 +00:00
ad 50fa8db4e4 Some significant performance improvements, and a fix for a race with pthread
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-08-04 13:37:48 +00:00
ad b5a5e72af1 Mirror a fix made to the kernel's condvars:
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-04-12 21:36:06 +00:00
ad a5070151ae - Test+branch is usually cheaper than making an indirect function call,
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-24 18:51:59 +00:00
ad 1dd83d7758 Move PTHREADD_ADD(PTHREADD_COND_WOKEUP) back to the correct spot. 2007-03-21 19:08:18 +00:00
ad d68cf1be1a - When signalling waiters, try not to awaken them immediatley. If we hold
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().
2007-03-20 23:49:58 +00:00
ad bfa3094e84 Cosmetic changes. 2007-03-14 23:34:48 +00:00
ad fe47a5c777 - Update to match changed pthread__park() interface.
- Fix a bug where a thread could be "reentered" onto the waiters queue.
2007-03-05 23:55:54 +00:00
ad f1c2a5c056 Undo previous, it matches IEEE 1003.1. 2007-03-05 22:25:27 +00:00
ad d6d036f667 Drop the interlock if cancelled. 2007-03-05 22:11:40 +00:00
ad 44e5b56619 Fix a sleep/wakeup race with condvars. 2007-03-02 19:56:47 +00:00
ad de2138164c Remove the PTHREAD_SA option. If M:N threads is reimplemented it's
better off done with a seperate library.
2007-03-02 18:53:51 +00:00
ad dba14ef0a0 Don't grab the state lock to check for cancellation around condition
variables, as _lwp_wakeup/lwp_park provide the necessary barrier.
2007-03-02 17:47:40 +00:00
ad ded2602507 Fix bugs with and improve upon previous. 2006-12-24 18:39:45 +00:00
ad 1ac6a89b79 Conditionalised support for 1:1 threads. Needs associated kernel changes
and more work to be useful.
2006-12-23 05:14:46 +00:00
mycroft 8fa85b4a65 gettimeofday();TIMEVAL_TO_TIMESPEC(); is exactly equivalent to
clock_gettime(CLOCK_REALTIME), except the latter may have more preicison
some day.  So, use that.
2005-01-06 17:33:36 +00:00
nathanw cb9ebec1e2 Correctly return ETIMEDOUT when the absolute time is in the past, in
the nonthreaded case. Fixes, and fix from, PR lib/25961.
2004-12-10 17:11:53 +00:00
nathanw 8bf7374bcf In cond_wait() and cond_timedwait(), do the ERRORCHECK testing of the
waiters list in all cases, not just on cancellation; there are other
sources of spurious wakeups, such as single-stepping in the debugger.

regress/lib/libpthread/conddestroy1 now passes.
2004-07-27 21:44:48 +00:00
kleink 5a6f0c93e7 In the nothread version, don't return EINTR if select() was interrupted
since this value is explicitly ruled out by POSIX.  Instead, return 0,
like a spurious wakeup.  From YAMAMOTO Takashi in PR standards/24240.
2004-05-03 09:13:34 +00:00
cl 774b4b225b Update mutex/rwlock/sem code to match recent change in cond code. 2003-11-24 23:54:13 +00:00
nathanw 250e7bbf8e Fix a race between timed wait callbacks and manual signal/broadcasting
accidentally introduced in rev. 1.5.

Noted by Christian Limpach.
2003-11-24 22:54:31 +00:00
nathanw 4e755ddba8 Prevent ptc_mutex from remaining set if a CV sleep is woken by
cancellation:

 * Arrange to not set ptc_mutex until after the pre-sleep cancellation
   test.

 * In the post-sleep cancellation test, check if there are no more
   sleepers and clear ptc_mutex if so.

While here, sprinkle some __predict_false() around the cancellation
tests.
2003-11-21 22:08:00 +00:00
nathanw 3f6de8d84a Use pthread__error() instead of pthread__abort(). 2003-04-23 19:36:12 +00:00
nathanw b7ac97b866 More agressive assertions. Change a number of detected conditions, which
officially have undefined behavior, from returning an error code to raising
an assertion failure.
Also, don't bother to explicitly test for (illegal) null pointers and return
an error; they'll bomb out soon enough.
2003-04-18 21:36:38 +00:00
nathanw 008033bb65 When a thread sleeps on a mutex or cv, have it put itself on the
front of the sleep queue rather than the back. This is more
cache-friendly behavior and within the (lack of) constraints on wakeup
ordering imposed on equal-priority threads.
2003-04-16 18:30:43 +00:00
lukem f043c0fb57 add __RCSID() 2003-03-08 08:03:34 +00:00
nathanw de9d8cde2a Remove debug toggle and add debug counter for signals and broadcasts
that actually wake another thread up.
2003-02-15 04:38:33 +00:00
nathanw 947e41dfa9 Arrange to pause for a while if pthread_cond_wait() or
pthread_cond_timedwait() is called before any threads have been
created and the SA infrastructure is up and running.

Addresses PR lib/20139.

XXX probably need to do this for all of the pthread_*_timedlock()
functions, too.
2003-02-01 00:57:31 +00:00