Commit Graph

50 Commits

Author SHA1 Message Date
matt 2b0c4dcffd Change pthread_mutex_t to use the amount of space as and be congruent to
the version used in the SA version of pthreads.  This preserves binary
compatibility between both versions of the library.
2008-08-02 19:46:30 +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
ad d5afa113a1 Add some general comments about the mutex implementation. 2008-05-25 12:29:59 +00:00
martin ce099b4099 Remove clause 3 and 4 from TNF licenses 2008-04-28 20:22:51 +00:00
ad 8548e33886 pthread__mutex_lock_slow:
- don't stop spinning if there are already waiters on the mutex, there is
  no point.
- cosmetic change.
2008-03-07 22:23:57 +00:00
ad 6869216239 Fix pthread_mutex_trylock() for recursive mutexes. PR lib/38087.
Based on a patch provided by rafal@.
2008-02-23 15:15:57 +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 a67e1e3475 - Remove libpthread's atomic ops.
- Remove the old spinlock-based mutex and rwlock implementations.
- Use the atomic ops from libc.
2008-02-10 18:50:54 +00:00
rafal 2e86943de9 Make this compile after Andrew's fix (remove now-unused variables) 2008-01-25 02:12:10 +00:00
ad bba572530b pthread_mutex_lock_slow: remove "deliberate deadlock" code that's not
needed with 1:1. PR lib/37524.
2008-01-25 01:09:18 +00:00
christos c6409540ef add missing static decls. 2008-01-08 20:56:08 +00:00
ad b43749fde1 machine/lock.h, not sys/lock.h 2008-01-05 01:37:35 +00:00
ad 989565f81d - Fix pthread_rwlock_trywrlock() which was broken.
- Add new functions: pthread_mutex_held_np, mutex_owner_np, rwlock_held_np,
  rwlock_wrheld_np, rwlock_rdheld_np. These match the kernel's locking
  primitives and can be used when porting kernel code to userspace.

- Always create LWPs detached. Do join/exit sync mostly in userland. When
  looped on a dual core box this seems ~30% quicker than using lwp_wait().
  Reduce number of lock acquire/release ops during thread exit.
2007-12-24 14:46:28 +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 baebee83d5 Fix inverted test after merge of nick-csl-alignment. 2007-09-11 10:27:44 +00:00
skrll 9fdaf800d9 Merge nick-csl-alignment. 2007-09-10 11:34:05 +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 2639b90e3b One more micro-optimization.. 2007-08-16 14:53:45 +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 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 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 b0427b61fb - Maintain a per-thread pointer to the last mutex acquired by the app, to
be used only as as a hint. Clear the pointer when releasing the mutex.
- When releasing a mutex, wake all waiters. Makes it possible to tranfer
  waiters from another object to a mutex.
2007-03-20 23:33:10 +00:00
ad c79299e2ec Update to match changed pthread__park() interface. 2007-03-05 23:56:17 +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 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
wrstuden 25b0eaef3c Close a window in which we can not notice a recently-slept-on-our
mutex thread, thus leaving a thread sleeping on an unlocked mutex.

Reviewed by myself and Christos.

Problem reported by Arne H. Juul, arnej at pvv dot ntnu dot no,
in PR 26208. This fix represents option 1 presented in the PR.
2006-08-22 21:46:09 +00:00
chs 0e67554241 starting the pthread library (ie. calling pthread__start()) before
any threads are created turned out to be not such a good idea.
there are stronger requirements on what has to work in a forked child
while a process is still single-threaded.  so take all that stuff
back out and fix the problems with single-threaded programs that
are linked with libpthread differently, by checking if the library
has been started and doing completely different stuff if it hasn't been:
 - for pthread_rwlock_timedrdlock(), just fail with EDEADLK immediately.
 - for sem_wait(), the only thing that can unlock the semaphore is a
   signal handler, so use sigsuspend() to wait for a signal.
 - for pthread_mutex_lock_slow(), just go into an infinite loop
   waiting for signals.

I also noticed that there's a "sem2" test that has never worked in its
single-threaded form.  the problem there is that a signal handler tries
to take a sem_t interlock which is already held when the signal is received.
fix this too, by adding a single-threaded case for sig_trywait() that
blocks signals instead of using the userland interlock.
2005-10-19 02:15:03 +00:00
chs 2415c56ed0 in pthread_mutex_lock_slow(), pthread_rwlock_timedrdlock() and sem_wait(),
call pthread__start() if it hasn't already been called.  this avoids
an internal assertion from the library if these routines are used
before any threads are created and they need to sleep.
fixes PR 20256, PR 24241, PR 25722, PR 26096.
2005-10-16 00:07:24 +00:00
nathanw 0774992407 Add cancellation protection to pthread_once() as per the standard; if
the once routine is cancelled, the effect on once_control is as if
pthread_once() was never called.

Bug report and fix from PR lib/30734.
2005-07-16 23:14:53 +00:00
cl f2f106648c add libpthread part of concurrency support for SA on MP systems
- enable concurrency according to environment variable PTHREAD_CONCURRENCY
- add idle VP wakeup if there are additional jobs and idle VPs
- make reidlequeue per VP
- enable spinning for locks
- fix race condition in alarm processing
- fix race condition in mutex locking
- make debugging output line buffered and add VP prefix to debug lines
2004-03-14 01:19:41 +00:00
cl 774b4b225b Update mutex/rwlock/sem code to match recent change in cond code. 2003-11-24 23:54:13 +00:00
christos 4e29fa8276 add missing notreached lint comment. 2003-05-27 15:22:56 +00:00
nathanw baa073280b Separately report unlocking an unlocked mutex and unlocking a mutex
owned by another thread.
2003-05-16 21:28:26 +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 a74799949a pthread_mutex_trylock(): It's not an error to call trylock() on a
mutex already locked by the calling thread, even for non-recursive
mutexes.
2003-04-16 18:59:12 +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 515d60d00c Remove unnecessary inclusion of <assert.h>. 2003-02-15 00:52:18 +00:00
nathanw 6cca91fc86 Mutex tuneup.
* Use a double-checked locking technique to avoid taking
   the interlock in pthread_mutex_unlock().

 * In pthread_mutex_lock() and pthread_mutex_trylock(), only store the
   stack pointer, not the thread ID, in ptm_owner. Do the translation
   to a thread ID in the slow-lock, errorcheck, and recursive mutex
   cases rather than in the common path.

 * Juggle where pthread__self() is called, to move it out of the fast path.

Overall, this means that neither pthread_self() nor
pthread_spin[un]lock() are used in the course of locking and unlocking
an uncontested mutex. Speeds up the fast path by 40-50%, and
eliminates about 98% of spinlocks used by a couple of large threaded
applications.

(Still a GET_MUTEX_PRIVATE() in the fast path... perhaps the type
should be in the main body of the mutex).
2003-01-31 02:55:00 +00:00
nathanw 6505a5330c Add debug counters for mutex and condvar operations. 2003-01-27 21:01:00 +00:00
scw 5f7bdeae84 Need to include <string.h> for memcmp() prototype. 2003-01-22 13:49:14 +00:00
thorpej 096005bf70 Fix typo. 2003-01-19 21:42:09 +00:00
thorpej 592409d356 Add mutexattr_init(), mutexattr_destroy(), and mutexattr_settype() (recursive
mutexes are used by the X libraries).
2003-01-19 20:46:11 +00:00
christos 9631ace572 de-lint 2003-01-18 18:40:52 +00:00
thorpej c62a74e6d5 Merge the nathanw_sa branch. 2003-01-18 10:32:11 +00:00