Commit Graph

241 Commits

Author SHA1 Message Date
Mike Makonnen
5dbd7addb0 1. Now that it's a thread's state is changed from within the kernel, where
no userland locks are heald, the dead thread lock can no longer protect
   access to it. Therefore, instead of using an if (!dead)...else clause
   after walking the active threads list test the thread pointer before
   deciding not to walk the dead threads list. If the thread pointer is null
   it means it was not found in the active threads list and the dead threads
   list should be checked.

2. Do not free the stack of a thread that is not marked dead. This is the
   2nd and final part of eliminating the race to free a thread's stack.

MFC after: 3 days
2004-10-13 11:42:20 +00:00
Mike Makonnen
4cdb7f14ed Remove a reference to a non-existent syscall: _thr_exit(). The
actual name is thr_exit(). How this ever worked is beyond me.
2004-10-08 14:48:02 +00:00
Mike Makonnen
401901ac43 Close a race between a thread exiting and the freeing of it's stack.
After some discussion the best option seems to be to signal the thread's
death from within the kernel. This requires that thr_exit() take an
argument.

Discussed with: davidxu, deischen, marcel
MFC after: 3 days
2004-10-06 14:23:00 +00:00
Mike Makonnen
2d79470f8a Remove vestiges of libthr's signal mangling past. This fixes that last
known problem with mysql on libthr: not being able to kill mysqld.
2004-09-22 18:51:16 +00:00
Mike Makonnen
ff9af45a01 The SUSv3 function say that the affected functions MAY FAIL, if the
specified mutex is invalid. In spec parlance 'MAY FAIL' means it's
up to the implementor. So, remove the check for NULL pointers for two
reasons:
	1. A mutex may be invalid without necessarily being NULL.
	2. If the pointer to the mutex is NULL core-dumping in the
	   vicinity of the problem is much much much better than failing
	   in some other part of the code (especially when the application
	   doesn't check the return value of the function that you oh so
	   helpfully set to EINVAL).
2004-09-22 16:53:23 +00:00
Mike Makonnen
737afa3cec Implement cancellation points in libc interfaces, as specified by POSIX. 2004-09-16 13:55:46 +00:00
David Xu
9027ac471c Adjust code to support AMD64, on AMD64, thread needs to set fsbase by
itself before it can execute any other code, so new thread should be
created with all signals are masked until after fsbase is set.
2004-08-19 23:49:04 +00:00
David Xu
f5eb80a9d3 Add AMD64 support code. 2004-08-19 23:41:12 +00:00
Doug Rabson
e6f19330ce Add rtld-elf to the include path for the rtld to pthread TLS interface. 2004-08-15 21:51:18 +00:00
Doug Rabson
adbadf41a9 Add TLS support for libthr on i386. 2004-08-15 16:21:30 +00:00
Mike Makonnen
0feabab576 o Assertions to catch that stuff that shouldn't happen is not happening.
o In the rwlock code: move a duplicated check inside an if..else to after
  the if...else clause.
o When initializing a static rwlock move the initialization check
  inside the lock.
o In thr_setschedparam.c: When breaking out of the trylock...retry if busy
  loop make sure to reset the mtx pointer to null if the mutex is nolonger
  in a queue.
2004-07-30 17:13:00 +00:00
Marcel Moolenaar
7ea419c0cf Define _libthr_debug for use by libthread_db. 2004-07-18 04:23:30 +00:00
Marcel Moolenaar
cc7be8dc05 Record the offset of thr_id in the thread structure. Required for
debugging.
2004-07-04 19:07:07 +00:00
Marcel Moolenaar
cd28f17da2 Change the thread ID (thr_id_t) used for 1:1 threading from being a
pointer to the corresponding struct thread to the thread ID (lwpid_t)
assigned to that thread. The primary reason for this change is that
libthr now internally uses the same ID as the debugger and the kernel
when referencing to a kernel thread. This allows us to implement the
support for debugging without additional translations and/or mappings.

To preserve the ABI, the 1:1 threading syscalls, including the umtx
locking API have not been changed to work on a lwpid_t. Instead the
1:1 threading syscalls operate on long and the umtx locking API has
not been changed except for the contested bit. Previously this was
the least significant bit. Now it's the most significant bit. Since
the contested bit should not be tested by userland, this change is
not expected to be visible. Just to be sure, UMTX_CONTESTED has been
removed from <sys/umtx.h>.

Reviewed by: mtm@
ABI preservation tested on: i386, ia64
2004-07-02 00:40:07 +00:00
Mike Makonnen
105711487e When a thread is created suspended have libthr suspend it explicitly
instead of asking the kernel to do it when we create the thread.
2004-06-30 15:57:36 +00:00
Mike Makonnen
03d74100cf Implement pthread_atfork in libthr. This is mostly from deichen's
work in libpthread.

Submitted by: Dan Nelson <dnelson@allantgroup.com>
2004-06-27 10:01:35 +00:00
Mike Makonnen
356c2d4f58 In the case that the global thread list is being re-initialized after
a fork, make sure that the current thread isn't detached and freed. As
a consequence the thread should be inserted into the head of the
active list only once (in the beginning).
2004-06-27 09:53:06 +00:00
Mike Makonnen
4cd18a22d5 Make libthr async-signal-safe without costly signal masking. The guidlines I
followed are: Only 3 functions (pthread_cancel, pthread_setcancelstate,
pthread_setcanceltype) are required to be async-signal-safe by POSIX. None of
the rest of the pthread api is required to be async-signal-safe. This means
that only the three mentioned functions are safe to use from inside
signal handlers.
However, there are certain system/libc calls that are
cancellation points that a caller may call from within a signal handler,
and since they are cancellation points calls have to be made into libthr
to test for cancellation and exit the thread if necessary. So, the
cancellation test and thread exit code paths must be async-signal-safe
as well. A summary of the changes follows:

o Almost all of the code paths that masked signals, as well as locking the
  pthread structure now lock only the pthread structure.
o Signals are masked (and left that way) as soon as a thread enters
  pthread_exit().
o The active and dead threads locks now explicitly require that signals
  are masked.
o Access to the isdead field of the pthread structure is protected by both
  the active and dead list locks for writing. Either one is sufficient for
  reading.
o The thread state and type fields have been combined into one three-state
  switch to make it easier to read without requiring a lock. It doesn't need
  a lock for writing (and therefore for reading either) because only the
  current thread can write to it and it is an integer value.
o The thread state field of the pthread structure has been eliminated. It
  was an unnecessary field that mostly duplicated the flags field, but
  required additional locking that would make a lot more code paths require
  signal masking. Any truly unique values (such as PS_DEAD) have been
  reborn as separate members of the pthread structure.
o Since the mutex and condvar pthread functions are not async-signal-safe
  there is no need to muck about with the wait queues when handling
  a signal ...
o ... which also removes the need for wrapping signal handlers and sigaction(2).
o The condvar and mutex async-cancellation code had to be revised as a result
  of some of these changes, which resulted in semi-unrelated changes which
  would have been difficult to work on as a separate commit, so they are
  included as well.

The only part of the changes I am worried about is related to locking for
the pthread joining fields. But, I will take a closer look at them once this
mega-patch is committed.
2004-05-20 12:06:16 +00:00
Mike Makonnen
7295f69667 q§ 2004-05-20 11:55:04 +00:00
Mike Makonnen
afa647196b Unconditionaly initialize any spin lock passed to pthread_spin_init(). While
makeing sure the spinlock isn't already in use might be a nice feature to
have in theory, it's hard to implement in practice since the passed in
pointer may not be NULL, but still be an invalid value (i.e. 1..2..3.. etc).
2004-04-24 09:38:41 +00:00
Mike Makonnen
572d95e285 o Also check that the mutex type is not less than the minimum allowable value.
o Don't check attribute for NULL. It's the callers responsibility.
2004-03-29 13:57:55 +00:00
Mike Makonnen
0ad70ba98e Make the minimum implementation of pthread_kill conform to the
functionality spelled out in SUSv3.
	o Signal of 0 means do everything except send the signal
	o Check that the signal is not invalid
	o Check that the target thread is not dead/invalid
2004-03-29 13:56:04 +00:00
Mike Makonnen
b321aa9888 o Don't explicitly check the thread for NULL. That is the caller's
responsibility.
o If a thread is not joinable, the correct return value is EINVAL.
2004-03-29 13:53:43 +00:00
Mike Makonnen
8c223652fb o If a thread is marked as detached AND on the dead threads list
the correct return value is ESRCH.
o Don't check the attribute for NULL. It's the caller's responsibility.
o Make the bitwise comparison explicit.
2004-03-29 13:51:51 +00:00
Mike Makonnen
61bf8f4731 If a condition variable is statically initialized don't return
an error. Return successfully without doing anything.
2004-03-29 11:24:02 +00:00
Mike Makonnen
0c3a942692 The thread suspend function now returns ETIMEDOUT, not EAGAIN. 2004-03-29 09:35:07 +00:00
Mike Makonnen
0465e53d8c o Remove more references to SIGTHR
o Remove clock resolution information left over from libc_r
2004-03-29 05:45:01 +00:00
Mike Makonnen
1c6f63018d Remove the garbage collector thread. All resources are freed
in-line. If the exiting thread cannot release a resource, then
the next thread to exit will release it.
2004-03-28 14:05:28 +00:00
Mike Makonnen
8bd3b0415b o Since we're not using signals for thread synchronization anymore,
sigprocmask no longer needs to be wrapped.
o raise(3) is applied to the calling thread in a threaded program.
o In the sigaction wrapper reference the correct structure.
o Don't treat SIGTHR especially anymore (infact it won't exist in
  a little while).
2004-03-27 15:05:28 +00:00
Mike Makonnen
7c8aa41383 Stop using signals for synchronizing threads. The performance penalty
was too much.
2004-03-27 14:39:21 +00:00
Mike Makonnen
81fda5bdd5 o The mutex locking functions aren't normally cancellation points. But,
we still have to DTRT when an asynchronously cancellable thread is
  cancelled while waiting for a mutex.
o While dequeueing a waiting mutex don't skip a thread if it has
  a cancel pending. Only skip it if it is also async cancellable.
2004-03-26 14:47:54 +00:00
Mike Makonnen
8733f60328 o Initialize a local variable before referencing it. This was not
the cause of any bugs because it is *always* indirectly set
  in the for...loop, but better to be explicit about it.
o Check the magic number of the passed in thread only after it has
  been found in the active thread list. Otherwise, if the check is done
  at the very beginning we may end up pointing to garbage if the
  thread was once a valid thread, but has now been destroyed.
2004-03-26 14:45:35 +00:00
Mark Murray
16fc3635f7 Make NULL a (void*)0 whereever possible, and fix the warnings(-Werror)
that this provokes. "Wherever possible" means "In the kernel OR NOT
C++" (implying C).

There are places where (void *) pointers are not valid, such as for
function pointers, but in the special case of (void *)0, agreement
settles on it being OK.

Most of the fixes were NULL where an integer zero was needed; many
of the fixes were NULL where ascii <nul> ('\0') was needed, and a
few were just "other".

Tested on: i386 sparc64
2004-03-05 08:10:19 +00:00
Peter Grehan
a95a647dcc libthr powerpc support.
Submitted by:  Suleiman Souhlal <refugee@segfaulted.com>
Tested with: most libpthread tests, Apache 'worker' MDM
2004-03-02 06:25:20 +00:00
Mike Makonnen
d4d7df5c0c Implement PThreads barriers and barrier attributes. 2004-02-19 13:51:52 +00:00
Mike Makonnen
8c18819a81 Don't wake up the thread after the signal handler
has been executed. On return from the signal handler
the call will either be restarted or EINTR will be returned,
but it will not go back to its previous state. So, it is
sufficient to simply change the state to 'running' without
actually trying to wake up the thread.
2004-02-19 13:47:12 +00:00
Mike Makonnen
097c5e8da4 Remove thr_getschedparam.c since it's contents have been moved into
thr_setschedparam.c
2004-02-18 15:59:54 +00:00
Mike Makonnen
32eaa7dddd There are consumers of rwlocks, inluding our own libc, that depend on
a PTHREAD_RWLOCK_INITIALIZER to do for rwlocks what
a similarly named symbol does for statically initialized mutexes.
This symbol was dropped in The Open Group Base Specifications Issue 6
and does not exist in IEEE Std 1003.1, 2003, but it should still be
supported for backwards compatibility.

Pointy hat: mtm
2004-02-18 15:30:10 +00:00
Mike Makonnen
b3d73b9b1e o Catch up with the mutex priority protocol fixes.
o Move pthread_getschedparam() into the same file with it's
  pthread_set* counterpart. Copyright on both files is identical.
2004-02-18 15:26:00 +00:00
Mike Makonnen
b325a92087 o Style
o Instead of checking both the passed in pointer and its value
  for NULL, only check the latter. Any caller that passes in
  a NULL pointer is obviously wrong.
2004-02-18 15:22:52 +00:00
Mike Makonnen
a561651c34 o Refactor and, among other things, get rid of insane nesting levels.
o Fix mutex priority protocols. Keep separate counts of priority
  inheritance and protection mutexes to make things easier.
  This will not have much affect since this is only the
  userland side, and the rest involves kernel scheduling.
2004-02-18 15:16:31 +00:00
Mike Makonnen
06e5becb69 Move the initialization of thread priority to a common function. 2004-02-18 15:05:56 +00:00
Mike Makonnen
0d48409fe2 Move the weak references to the top of the file to conform
to the format of other similar files in libthr.
2004-02-18 14:56:20 +00:00
Colin Percival
d623b765cf style cleanup: Remove duplicate $FreeBSD$ tags.
These files had tags after the copyright notice,
inside the comment block (incorrect, removed),
and outside the comment block (correct).

Approved by:	rwatson (mentor)
2004-02-10 20:42:33 +00:00
Daniel Eischen
518ae0c8fd Remove the band-aid (#include <time.h>). 2004-02-03 22:30:01 +00:00
Daniel Eischen
fc9579997c Add <time.h> -- bandaid to unbreak world in <semaphore.h>. 2004-02-03 15:55:30 +00:00
Mike Makonnen
4e3c587d23 Bump up the maximum number concurrent threads on x86. 2004-02-01 15:33:01 +00:00
Mike Makonnen
1baa64733c I update the rwlock code in libthr to be more standards compliant and
what do I get for my troubles? libc breaks offcourse!

Reimplement a hack (in libthr) that allows libc to use
rwlocks without initializing them first. The hack was reimplemented
so that only a private libc version of the rwlock locking functions
initializes an uninitialized rwlock. The application version will
correctly fail.
2004-01-29 12:03:17 +00:00
Mike Makonnen
98a11db62d When suspending a thread if the timeout was very short or
the system call got interrupted and the absolute timeout is
converted to a relative timeout, it may happen that we get a
negative number. In such a case, simply set the timeout to
zero so that if the event that the thread wants to wait for has
happened it can still return successfully, but if it hasn't
happened then the thread doesn't suspend indefinitely. This should
fix certain applications (including mozilla) that seem to hang
indefinitely sometimes.

Noticed and debugged by: Morten Johansen <root@morten-johansen.net>
2004-01-29 09:44:36 +00:00
Mike Makonnen
dec04f43d9 o Implement the pthread_spin_* functions in libthr.
o Man pages
2004-01-22 15:31:56 +00:00
Mike Makonnen
1c6841ae78 Refactor _pthread_mutex_init
o Simplify the logic by removing a lot of unnecesary nesting
	o Reduce the amount of local variables
	o Zero-out the allocated structure and get rid of
	  all the unnecessary setting to 0 and NULL;

Refactor _pthread_mutex_destroy
	o Simplify the logic by removing a lot of unnecesary nesting
	o No need to check pointer that the mutex attributes points
	  to. Checking passed in pointer is enough.
2004-01-19 15:00:57 +00:00
Mike Makonnen
c40bafac85 Implement reference counting of read-write locks. This uses
a list in the thread structure to keep track of the locks and
how many times they have been locked. This list is checked
on every lock and unlock. The traversal through the list is
O(n). Most applications don't hold so many locks at once that
this will become a problem. However, if it does become a problem
it might be a good idea to review this once libthr is
off probation and in the optimization cycle.
This fixes:
	o deadlock when a thread tries to recursively acquire a
	  read lock when a writer is waiting on the lock.
	o a thread could previously successfully unlock a lock it did not own
	o deadlock when a thread tries to acquire a write lock on
	  a lock it already owns for reading or writing [ this is admittedly
	  not required by POSIX, but is nice to have ]
2004-01-19 14:51:45 +00:00
Mike Makonnen
104ff764e5 Add an implementation of pthread_rwlock_timed{rd,wr}lock() to libthr with
attendant documentation.
2004-01-16 10:52:10 +00:00
Mike Makonnen
14f8ddcd08 o We are not required to initialize an invalid rwlock. So axe all that
code and simply return EINVAL (which is allowed by the standard) in
  all those pthread functions that previously initialized it.

o Refactor the pthread_rwlock_[try]rdlock() and pthread_rwlock_[try]wrlock()
  functions. They are now completeley condensed into rwlock_rdlock_common()
  and rwlock_wrlock_common(), respectively.

o If the application tries to destroy an rwlock that is currently
  held by a thread return EBUSY where it previously went ahead and
  freed all resources associated with the lock.

o Refactor _pthread_rwlock_init() to make it look (relatively) sane.

o When obtaining a read lock on an rwlock the check for whether it
  would exceed the maximum allowed read locks should happen *before*
  we obtain the lock.

o The pthread_rwlock_* functions shall *never* return EINTR, so make
  sure to requeue/resuspend the thread if it encounters such an error.

o Make a note that pthread_rwlock_unlock() needs to ensure it holds a
  lock on an rwlock it tries to unlock. It will be implemented in a
  separate commit because it requires some additional rwlock infrastructure.
2004-01-16 07:10:30 +00:00
Ruslan Ermilov
2ed59d22bd Return ENOTSUP instead of -1. 2004-01-15 16:09:58 +00:00
Mike Makonnen
2aa9de1f77 o Implement pthread_mutex_timedlock(), which does not block indefinitely on
a mutex locked by another thread.
o document it: pthread_mutex_timedlock(3)
2003-12-30 08:44:55 +00:00
Mike Makonnen
2b33fc6470 Make it possible for the library to specify a timeout value when
waiting on a locked mutex. This involves passing a struct timespec
from the pthread mutex locking interfaces all the way down to the
function that suspends the thread until the mutex is released.
The timeout is assumed to be an absolute time (i.e. not relative to
the current time).

Also, in _thread_suspend() make the passed in timespec const.
2003-12-30 08:34:57 +00:00
Doug Rabson
795a502646 Don't block SIGTRAP - it makes it hard to debug programs with gdb.
Reviewed by: mtm
2003-12-26 12:11:16 +00:00
Mike Makonnen
f2c3dd08ec Preparations to make libthr work in multi-threaded fork()ing applications.
o Remove some code duplication between _thread_init(), which is run once
  to initialize libthr and the intitial thread, and pthread_create(), which
  initializes newly created threads, into a new function called from both
  places: init_td_common()
o Move initialization of certain parts of libthr into a separate
  function. These include:
	- Active threads list and it's lock
	- Dead threads list and it's lock & condition variable
	- Naming and insertion of the initial thread into the
	  active threads list.
2003-12-26 08:16:17 +00:00
Mike Makonnen
8657fd166c Remove _giant_mutex and its associated macros. 2003-12-15 12:38:06 +00:00
Mike Makonnen
2543fd4700 Comment out most of pthread_setschedparam. Pthread priorities didn't
work before anyways, and I didn't want to fix broken code I had no
way of testing. It was necessary however, in order to get rid of GIANT_LOCK.
Pthread priorities will have to wait a little longer to get fixed.
2003-12-15 12:31:46 +00:00
Mike Makonnen
c830473999 When creating a pthread in the suspended state their were two
problems: (1) The wrong flag was being checked for in the attribute
	  (2) The pthread's state was not being set to indicate it was
	      suspended.

Noticed by: Igor Sysoev <is@rambler-co.ru>
2003-12-15 09:35:02 +00:00
Mike Makonnen
099fe19901 Doh! Lock the thread passed in by the caller, not the current thread. 2003-12-12 09:51:39 +00:00
Mike Makonnen
f318a5206c Remove uses of GIANT_LOCK and replace with appropriate thread
and thread list locks.
2003-12-11 08:34:07 +00:00
Mike Makonnen
d214f02991 Take a stab at fixing some of the macro-nightmare.
PTHREAD_NEW_STATE should work as expected now: a thread
marked PS_RUNNING will get sent a SIGTHR.
Still more cleanups necessary.
2003-12-09 11:20:01 +00:00
Mike Makonnen
8955220107 Fix the wrapper function around signals so that a signal handling
thread on one of the mutex or condition variable queues is removed
from those queues before the real signal handler is called.
2003-12-09 11:12:11 +00:00
Mike Makonnen
6fedbb4e37 Ugghh, cvs add the functions necessary to lock the global signal action
table.
2003-12-09 11:06:55 +00:00
Mike Makonnen
4a7709c540 o Add a wrapper around sigaction(2), so we can insert our own wrapper
around signals.
o Lock the process global signal action table.
2003-12-09 11:04:36 +00:00
Mike Makonnen
d63466e954 Enable cancellation points around some syscalls. 2003-12-09 11:01:09 +00:00
Mike Makonnen
a4be5b10a2 Use dynamic instead of static LDT allocation.
Approved by: re (scottl)
2003-12-02 16:00:26 +00:00
Marcel Moolenaar
ed74e02776 Relink libc_r.a, libc_r.so and libc_r_p.so from libthr to libkse.
On ia64, where there's no libc_r at all, libkse is now the default
thread library by virtue of these links.

The reasons for this change are:
1. libkse is slated to become the default thread library anyway,
2. active development and maintenance is only present for libkse,
3. GNOME and KDE, both in the process of being supported on ia64,
   work better with KSE; even on ia64.
2003-09-27 23:27:19 +00:00
Marcel Moolenaar
c24297c0f2 Implement _get_curthread and _set_curthread. We use GCCs builtin
function this, which expands to PAL calls (rduniq and wruniq).
This needs adjustment when TLS is implemented.
2003-07-24 07:51:49 +00:00
Mike Makonnen
9e222aaf7a The MD framework for libthr on alpha 2003-07-19 15:57:52 +00:00
Mike Makonnen
393441d43b When _PTHREADSINVARIANTS is defined SIGABRT is not included
in the set of signals to block.
Also, make the PANIC macro call abort() instead of simply
exiting.
2003-07-08 09:58:23 +00:00
Mike Makonnen
659045ffbf Change all instances of THR_LOCK/UNLOCK, etc to UMTX_*.
It is a more acurate description of the locks they
operate on.
2003-07-06 10:18:48 +00:00
Mike Makonnen
9644071977 There's no need for _umtxtrylock to be a separate function.
Roll it into the pre-existing macro that's used to call it.
2003-07-06 10:10:32 +00:00
Mike Makonnen
e921a3c976 _pthread_mutex_trylock() is another internal libc function that must block
signals.
2003-07-03 13:28:53 +00:00
Mike Makonnen
f493d09ae7 Begin making libthr async signal safe.
Create a private, single underscore, version of pthread_mutex_unlock for libc.
pthread_mutex_lock already has one. These versions are different from the
ones that applications will link against because they block all signals
from the time a call to lock the mutex is made until it is successfully
unlocked.
2003-07-02 02:05:23 +00:00
Mike Makonnen
745a4a9ef8 Do not attempt to reque a thread on a mutex queue. It may be that
a thread receives a spurious wakeup from sigtimedwait(), so make sure
that the call to the queueing code is called only once before entering
the loop (not in the loop). This should fix some fatal errors people
are seeing with messages stating the thread is already on the mutex queue.
These errors may still be triggered from signal handlers; however, since
that part of the code is not locked down yet.
2003-07-01 15:52:09 +00:00
Ruslan Ermilov
0b3cbc5c38 Axe AINC.
Submitted by:	bde
2003-07-01 15:07:01 +00:00
Mike Makonnen
fadd82e367 Catchup with _thread_suspend() changes. 2003-06-30 12:35:31 +00:00
Mike Makonnen
dbc6f4c07d Sweep through pthread locking and use the new locking primitives for
libthr.
2003-06-29 23:51:04 +00:00
Mike Makonnen
2234d5bea2 Locking primitives and operations in libthr should use struct umtx,
not spinlock_t. Spinlock_t and the associated functions and macros may
require blocking signals in order for async-safe libc functions to behave
appropriately in libthr. This is undesriable for libthr internal locking.
So, this is the first step in completely separating libthr from libc's
locking primitives.

Three new macros should be used for internal libthr locking from now on:
THR_LOCK, THR_TRYLOCK, THR_UNLOCK.
2003-06-29 23:49:41 +00:00
Mike Makonnen
c36507007f In a critical section, separate the aquisition of the thread lock
and the disabling of signals. What we are really interested in is
keeping track of recursive disabling of signals. We should not
be recursively acquiring thread locks. Any such situations should
be reorganized to not require a recursive lock.

Separating the two out also allows us to block signals independent of
acquiring thread locks. This will be needed in libthr in the near future when
we put the pieces together to protect libc functions that use pthread mutexes
and low level locks.
2003-06-29 21:21:52 +00:00
John Polstra
7c916264aa Make _thread_suspend work with both the old broken sigtimedwait
implementation and the new improved one.  We now precompute the
signal set passed to sigtimedwait, using an inverted set when
necessary for compatibility with older kernels.
2003-06-29 15:55:44 +00:00
Mike Makonnen
7e2160688c The move to _retire() a thread in the GC instead of in the thread's
exit function has invalidated the need for _spin[un]lock_pthread().
The _spin[un]lock() functions can now dereference curthread without
the danger that the ldtentry containing the pointer to the thread
has been cleared out from under them.
2003-06-29 00:12:40 +00:00
Marcel Moolenaar
6128a735ea Create compatibility links for libc_r on ia64 to prevent build-time
breakages. Note that runtime compatibility is not guaranteed. Future
changes to setjmp/longjmp in libc will break threaded applications
linked against libc_r.so.5 on ia64. We pull our "tier 2" card once
more...

Reviewed by: ru
2003-06-27 18:07:47 +00:00
Mike Makonnen
05e948d996 _thread_printf() is only used for debugging or in cases where something's
screwed beyond all help, so it can just skip the pthreads wrapper
for write(2) and call directly into it.
2003-06-09 17:58:15 +00:00
Mike Makonnen
4dee39fea0 Make C applications statically compiled with libthr work. Previously,
an application compiled -static with libthr would dump core in
malloc(3) because the stub thread initialization routine in libc would
be used instead of the libthr supplied one.
2003-06-04 08:23:05 +00:00
Mike Makonnen
b9662ddd18 Teach recent changes in the umtx structure in the kernel to the libthr
initialiazer.

Found by:	tinderbox
2003-06-03 09:31:33 +00:00
Mike Makonnen
cff6c3cab1 Unwind the _giant_mutex from pthread_detach(). When detaching a joiner thread
it's important the correct lock order is observed: lock first the joined and
then the joiner.
2003-06-02 11:01:00 +00:00
Mike Makonnen
4384412030 Consolidate static_init() and static_init_private into one function.
The behaviour of this function is controlled by the argument: private.
2003-06-02 10:04:18 +00:00
David E. O'Brien
a23e5f4d43 .S comments must be C comments, not ASM ones. 2003-06-02 02:32:56 +00:00
Mike Makonnen
6e1aa51e9e I botched one of my committs in the last round. Fix it. 2003-05-31 14:38:22 +00:00
Mike Makonnen
1b2a19ce0e Make the mutex static initializers look more like the one for
condition variables. Cosmetic.

Explicitly compare against PTHREAD_MUTEX_INITIALIZER. We shouldn't
encourage calls to the mutex functions with null pointers to mutexes.

Approved by:	re/jhb
2003-05-29 20:58:31 +00:00
Mike Makonnen
41f2bd859f Use a static lock to ake sure pthread_cond_* functions called
from multiple threads don't initialze the same condition variable
more than once.

Explicitly compare cond pointers with PTHREAD_COND_INITIALIZER instead
of NULL. Just because it happens to be defined as NULL is no reason
to encourage the idea that people can call those functions with
NULL pointers to a condition variable.

Approved by:	re/jhb
2003-05-29 20:54:00 +00:00
Mike Makonnen
0e335eaeb5 Missing unlock.
Approved by:	re/jhb
2003-05-29 20:49:17 +00:00
Mike Makonnen
b3cdf7ae2e Don't hold the active thread list lock when signaling the gc thread.
The dead list thread is sufficient for synchronization.

Retire the arch_id (ldt array slot) in the gc thread instead of the
doing it in the thread itself.

Approved by:	re/jhb
2003-05-29 20:46:53 +00:00
Mike Makonnen
981f4968f0 It's unnecessary to lock the thread during creation. Simply extend
the scope of the active thread list lock.

Approved by:	re/jhb
2003-05-29 20:40:50 +00:00
Mike Makonnen
a09d02f780 Minimize the potential for deadlocks between an exiting thread and it's
joiner by making sure all locks and unlocks occur in the same order. For
the record the lock order is: DEAD_LIST, THREAD_LIST, exiting thread, joiner
thread.

Approved by: re/rwatson
2003-05-27 21:48:42 +00:00
Mike Makonnen
ee98e9be9f Revert part of the last commit. I don't know what I was smoking.
Approved by: re/rwatson
2003-05-27 21:43:49 +00:00
Mike Makonnen
ca1c469cc7 Decouple the thread stack [de]allocating functions from the 'dead threads list'
lock. It's not really necessary and we don't need the added complexity
or potential for deadlocks.

Approved by:	re/blanket libthr
2003-05-26 00:37:07 +00:00
Mike Makonnen
2387af9962 Revise the unlock order in _pthread_join(). Also, if the joined
thread is not dead, the join loop is guaranteed to execute at least
once, so there is no need to pick up the thread list lock after
we return from suspenstion only to release it after the loop.

Approved by:	re/blanket libthr
2003-05-26 00:28:49 +00:00
Mike Makonnen
12c407a424 Return gracefully, rather than aborting, when the maximum concurrent
threads per process has been reached. Return EAGAIN, as per spec.

Approved by:	re/blanket libthr
2003-05-25 22:40:57 +00:00
Mike Makonnen
d39d651258 _pthread_cancel() breaks the normal lock order of first locking the
joined and then the joiner thread. There isn't an easy (sane?) way
to make it use the correct order without introducing races involving
the target thread and finding which (active or dead) list it is on. So,
after locking the canceled thread it will try to lock the joined thread
and if it fails release the first lock and try again from the top.

Introduce a new function, _spintrylock, which is simply a wrapper arround
umtx_trylock(), to help accomplish this.

Approved by: re/blanket libthr
2003-05-25 08:48:11 +00:00
Mike Makonnen
4393f2c4ec Part of the last patch.
Modify the thread creation and thread searching routine
to lock the thread lists with the new locks instead of GIANT_LOCK.

Approved by:	re/blanket libthr
2003-05-25 08:35:37 +00:00
Mike Makonnen
71d09bc86a Start locking up the active and dead threads lists. The active threads
list is protected by a spinlock_t, but the dead list uses a pthread_mutex
because it is necessary to synchronize other threads with the garbage
collector thread. Lock/Unlock macros are used so it's easier to make
changes to the locks in the future.

The 'dead thread list' lock is intended to replace the gc mutex.
This doesn't have any practical ramifications. It simply makes it
clearer what the purpose of the lock is. The gc will use this lock,
instead of the gc mutex, to synchronize access to the dead list with
other threads.

Modify _pthread_exit() to use these two new locks instead of GIANT_LOCK,
and also to properly lock and protect thread state changes,
especially with respect to a joining thread.

The gc thread was also re-arranged to be more organized and less nested.

_pthread_join() was also modified to use the thread list locks. However,
locking and unlocking here needs special care because a thread could find
itself in a position where it's joining an exiting thread that is
waiting on the dead list lock, which this thread (joiner) holds. If the
joiner doesn't take care to lock *and* unlock in the same order they
(the joiner and the joinee) could deadlock against each other.

Approved by:	re/blanket libthr
2003-05-25 08:31:33 +00:00
Mike Makonnen
6a1899ed5c The libthr code makes use of higher-level primitives (pthread_mutex_t and
pthread_cond_t) internaly in addition to the low-level spinlock_t. The
garbage collector mutex and condition variable are two such examples. This
might lead to critical sections nested within critical sections. Implement
a reference counting mechanism so that signals are masked only on the first
entry and unmasked on the last exit.

I'm not sure I like the idea of nested critical sections, but if
the library is going to use the pthread primitives it might be necessary.

Approved by:	re/blanket libthr
2003-05-25 07:58:22 +00:00
Marcel Moolenaar
9e14b94e43 The struct mcontext has changed. It's using the register sets. Bring
this in line.
2003-05-25 06:49:19 +00:00
Mike Makonnen
a224a3919d Lock the cond queue (condition variables):
Access to the thread's flags and state is protected by
_thread_critical_enter/exit(). When a thread is signaled with a condition
its state must be protected by locking it and disabling
signals before it is taken of the waiters' queue.

Move the implementation of pthread_cond_signal() and pthread_cond_broadcast()
into one function, cond_signal(). Its behaviour is determined by the
last argument, int broadcast. If this is set to 1 it will remove all
waiters, otherwise it will wake up only the first waiter thread.

Remove an extraneous call to pthread_testcancel().

Approved by:	re/blanket libthr
2003-05-24 01:02:16 +00:00
Mike Makonnen
59a47b31d0 Add two functions: _spinlock_pthread() and _spinunlock_pthread()
that take the address of a struct pthread as their first argument.
_spin[un]lock() just become wrappers arround these two functions.
These new functions are for use in situations where curthread can't be
used. One example is _thread_retire(), where we invalidate the array index
curthread uses to get its pointer..

Approved by:	re/blanket libthr
2003-05-23 23:39:31 +00:00
Mike Makonnen
b32a99e5f4 EDOOFUS
Prevent one thread from messing up another thread's saved signal
mask by saving it in struct pthread instead of leaving it as a
global variable. D'oh!

Approved by:	re/blanket libthr
2003-05-23 10:28:13 +00:00
Mike Makonnen
7d9d7ca2ed Make WARNS2 clean. The fixes mostly included:
o removed unused variables
	o explicit inclusion of header files
	o prototypes for externally defined functions

Approved by:    re/blanket libthr
2003-05-23 09:48:20 +00:00
Mike Makonnen
4e3f7b6ede note to self: do not confuse void* with int.
Approved by:	re/blanket libthr
2003-05-23 08:13:24 +00:00
Mike Makonnen
509d72c4b9 o Make the defenition of _set_curthread() match its declaration
in thr_private.h

o Lock down the ldt_entries array and ldt_free, which points to
  the next free slot. As noted in the comments, it's necessary
  to special case the initial_thread because %gs is not setup
  for it yet. This is ok because that early in the program there
  won't be any reentrancy issues anyways.

Approved by:	re/blanket libthr
2003-05-21 08:21:24 +00:00
Mike Makonnen
6439d4c286 Insert a debugging aid:
When in either the mutex or cond queue we notice that the thread
 is already on one of the queues, don't just simply abort(). Print
 out the thread's identifiers and what queue it was on.

Approved by: markm/mentor, re/blanket libthr
2003-05-21 03:41:07 +00:00
Mike Makonnen
5ccf23715a Re-enable the garbage collector thread in anticipation of further
locking work. I can't see anything obviously wrong with it (other than
the need to update the locking).

Approved by: markm/mentor, re/blanket libthr
2003-05-21 03:34:54 +00:00
Mike Makonnen
f97591bf25 When a thread exits it does not return from the kernel unless it
is the *only* remaining thread in the application, in which case we
should not core dump, and instead exit gracefully.

Approved by: markm/mentor, re/blanket libthr
2003-05-21 03:29:18 +00:00
Mike Makonnen
3f07b4bcbd The thread id was being set *before* zeroing out the thread. Reverse
the order.

Approved by: markm/mentor, re/blanket libthr
2003-05-21 03:22:36 +00:00
Mike Makonnen
9dc6e7848f Move a misplaced comment.
Approved by:	markm/mentor (implicit), re/blanket libthr
2003-05-20 18:48:41 +00:00
Mike Makonnen
dd3b229e2c Do some cleanup with respect to condition variables. The implementation
of pthread_cond_timedwait() is moved into cond_wait_common().
Pthread_cond_wait() and pthread_cond_timedwait() are now wrappers around
this function. Previously, the former called the latter with the abstime
pointing to 0 time. This violated Posix semantics should an application
have reason to call it with that argument because instead or returning
immediately it would have waited indefinitely for the cv to be signaled.

Approved by:	markm/mentor, re/blanket libthr
Reviewed by:	jeff
2003-05-15 18:17:13 +00:00
Mike Makonnen
6da7f4937e o Make the setting/checking of cancel state atomic with
respect to other threads and signal handlers by moving to
  the _thread_critical_enter/exit functions.

o Introduce an static function, testcancel(), that is used by
  the other functions in this module. This allows it to make
  locking assumptions that the top-level functions can't.

o Rework the code flow a bit to reduce indentation levels.

Approved by:	markm/mentor, re/blanket libthr
Reviewed by:	jeff
2003-05-15 17:56:18 +00:00
Mike Makonnen
479778b07f msg2 2003-05-12 10:40:53 +00:00
Mike Makonnen
c984b5a72a msg1 2003-05-12 10:34:01 +00:00
Mike Makonnen
a260623c5f Fix a null dereference leading to a core dump when
the number of threads exceeds the number of open slots
in ldt_entries[].

Approved by:	markm (mentor)(implicit)
Reviewed by:	jeff
2003-05-06 02:33:49 +00:00
Mike Makonnen
ae0df91f34 o Correct a debug message that refered to the wrong function
o Remove an unncecesary if clause

Approved by:	markm (mentor)(implicit)
Reviewd by:	jeff
2003-05-06 02:30:52 +00:00
Marcel Moolenaar
1d3e7116dc Add support for ia64.
Note that the tp register (r13) is reserved as the TLS pointer in
the same way that that gp register (r1) is reserved as the global
pointer. This implementation uses the tp register to point to the
thread structure used by the threads implementation. This is not
in violation with the runtime specification provided the TLS is
a fixed distance from the thread structure. This is only an issue
when code used the __thread keyword to create TLS. This is not
supported at the moment.
2003-04-20 03:06:42 +00:00
Marcel Moolenaar
09dd61fd99 Use STDERR_FILENO as the file descriptor passed to _thread_printf()
instead of 0 (ie stdin). Writing to stdin may not be possible.
2003-04-20 02:58:30 +00:00
Marcel Moolenaar
e7c9b7517b Fix build breakage (on ia64) caused by a missing file descriptor to
_thread_printf(). Use STDERR_FILENO as the file descriptor.
2003-04-20 02:56:12 +00:00
John Polstra
8c0d4b5f92 Add stub implementations of pthread_[gs]etconcurrency to libc_r and
libthr.  No changes were made to libpthread by request of deischen,
who will soon commit a real implementation for that library.

PR:		standards/50848
Submitted by:	Sergey A. Osokin <osa@freebsd.org.ru>
MFC after:	1 week
2003-04-20 01:53:13 +00:00
Jake Burkholder
55ad402a8f - Pass a ucontext_t to _set_curthread. If non-NULL the new thread is set
as curthread in the new context, so that it will be set automatically when
  the thread is switched to.  This fixes a race where we'd run for a little
  while with curthread unset in _thread_start.

Reviewed by:	jeff
2003-04-03 03:34:50 +00:00
Jeff Roberson
996a395d37 - Don't overrun the ldt buffer.
Submitted by:	gordan@freebsd.org
2003-04-02 22:53:52 +00:00
Jake Burkholder
68c724b96f Implement _get_curthread and _set_curthread. This is especially easy. 2003-04-02 08:15:07 +00:00
Jeff Roberson
26f52e2f8b - Define curthread as _get_curthread() and remove all direct calls to
_get_curthread().  This is similar to the kernel's curthread.  Doing
   this saves stack overhead and is more convenient to the programmer.
 - Pass the pointer to the newly created thread to _thread_init().
 - Remove _get_curthread_slow().
2003-04-02 03:05:39 +00:00
Jake Burkholder
dd3dd8724d Add MD makefile. 2003-04-01 23:52:24 +00:00
Jeff Roberson
00c301540e - Don't drop and reacquire giant in thread_suspend(). Change callers to do
this manually.  This will facilitate the unrolling of giant.
 - Don't allow giant to recurse anymore.  This should never happen.
2003-04-01 22:41:41 +00:00
Jeff Roberson
901d86ae8a - Reenable setschedparam and the prioceiling code now that the mutex code
has been restored.
2003-04-01 22:40:29 +00:00
Jeff Roberson
360a519459 - Restore old mutex code from libc_r. It is more standards compliant.
This was changed because originally we were blocking on the umtx and
   allowing the kernel to do the queueing.  It was decided that the
   lib should queue and start the threads in the order it decides and the
   umtx code would just be used like spinlocks.
2003-04-01 22:39:31 +00:00
Jeff Roberson
7a57e9abdd - Adjust the makefiles so we have a per architecture makefile. 2003-04-01 07:07:38 +00:00
Jeff Roberson
70d5093a8b - Commit the forgotten libthr/sys bits. 2003-04-01 03:51:08 +00:00
Jeff Roberson
bb535300dd - Add libthr but don't hook it up to the regular build yet. This is an
adaptation of libc_r for the thr system call interface.  This is beta
   quality code.
2003-04-01 03:46:29 +00:00