Commit Graph

432 Commits

Author SHA1 Message Date
David Xu
a772047bc6 o Use a daemon thread to monitor signal events in kernel, if pending
signals were changed in kernel, it will retrieve the pending set and
  try to find a thread to dispatch the signal. The dispatching process
  can be rolled back if the signal is no longer in kernel.

o Create two functions _thr_signal_init() and _thr_signal_deinit(),
  all signal action settings are retrieved from kernel when threading
  mode is turned on, after a fork(), child process will reset them to
  user settings by calling _thr_signal_deinit(). when threading mode
  is not turned on, all signal operations are direct past to kernel.

o When a thread generated a synchoronous signals and its context returned
  from completed list, UTS will retrieve the signal from its mailbox and try
  to deliver the signal to thread.

o Context signal mask is now only used when delivering signals, thread's
  current signal mask is always the one in pthread structure.

o Remove have_signals field in pthread structure, replace it with
  psf_valid in pthread_signal_frame. when psf_valid is true, in context
  switch time, thread will backout itself from some mutex/condition
  internal queues, then begin to process signals. when a thread is not
  at blocked state and running, check_pending indicates there are signals
  for the thread, after preempted and then resumed time, UTS will try to
  deliver signals to the thread.

o At signal delivering time, not only pending signals in thread will be
  scanned, process's pending signals will be scanned too.

o Change sigwait code a bit, remove field sigwait in pthread_wait_data,
  replace it with oldsigmask in pthread structure, when a thread calls
  sigwait(), its current signal mask is backuped to oldsigmask, and waitset
  is copied to its signal mask and when the thread gets a signal in the
  waitset range, its current signal mask is restored from oldsigmask,
  these are done in atomic fashion.

o Two additional POSIX APIs are implemented, sigwaitinfo() and sigtimedwait().

o Signal code locking is better than previous, there is fewer race conditions.

o Temporary disable most of code in _kse_single_thread as it is not safe
  after fork().
2003-06-28 09:55:02 +00:00
David Xu
d15cbd7dc0 Use mmap retuned value.
Reviewed by: deischen
2003-06-28 09:48:05 +00:00
David Xu
8d5f23a1f9 Temporary disable rwlock based code, replace it with low level KSE locking
code until rtld-elf and libkse can cooperate better, those code can be
restored.

Reviewed by: deischen
2003-06-28 09:47:22 +00:00
David Xu
52d9c77df4 Write new thread pointer back only when success.
Reviewed by: deischen
2003-06-28 09:41:59 +00:00
David Xu
a56b526b51 After thread was interrupted by signal, it should relock mutex.
Reviewed by: deischen
2003-06-28 09:40:57 +00:00
David Xu
a07576d63c if thread is exiting, just returns. kse_thr_interrupt interface
was changed, it needs signal parameter, pass -1 to it, it indicates to
interrupt syscall.

Reviewed by: deischen
2003-06-28 09:39:35 +00:00
Marcel Moolenaar
fd62f5ca46 Implement _ia64_save_context() and _ia64_restore_context(). Both
functions are derived from the swapctx() and restorectx() (resp)
from sys/ia64/ia64/context.s. The code is expected to be 99%
correct, but has not yet been tested.

Note that with these functions operating on mcontext_t, we also
created the foundation upon which we can implement getcontext(2)
and setcontext(2) replacements. It's not guaranteed that the use
of these syscalls and _ia64_{save|restore}_context() on the same
uicontext_t is actually going to work. Replacing the syscalls is
now trivially achieved.

This commit completes the ia64 port of libpthread itself (modulo
testing and bugfixes).
2003-06-27 06:15:13 +00:00
Marcel Moolenaar
b51f305ec1 Implement _ia64_enter_uts(). The purpose of this function is to switch
the register stack and memory stack and call the function given to it.

While here, provide empty, non-working, stubs for the context functions
(_ia64_save_context() and _ia64_restore_context()) so that anyone can at
least compile libkse from CVS sources. Real implementations will follow
soon.
2003-06-26 05:40:15 +00:00
Marcel Moolenaar
6351f43f14 Implement _thr_enter_uts() and _thr_switch() as inline functions to
minimize the amount and complexity of assembly code that needs to be
written. This way the core functionality is spread over 3 elementary
functions that don't have to do anything that can more easily and
more safely be done in C. As such, assembly code will only have to
know about the definition of mcontext_t.
The runtime cost of not having these functions being inlined is less
important than the cleanliness and maintainability of the code at
this stage of the implementation.
2003-06-26 03:55:58 +00:00
Marcel Moolenaar
5858b0cea8 Explicitly widen int types before casting to pointer types. On 64-bit
platforms the compiler warns about incompatible integer/pointer casts
and on ia64 this generally is bad news. We know that what we're doing
here is valid/correct, so suppress the warning. No functional change.

Sleeps better: marcel
2003-06-24 00:37:26 +00:00
Marcel Moolenaar
82468d1f27 Untangle the inter-dependency of kse types and ksd types/functions
by moving the definition of struct ksd to pthread_md.h and removing
the inclusion of ksd.h from thr_private.h (which has the definition
of struct kse and kse_critical_t). This allows ksd.h to have inline
functions that use struct kse and kse_critical_t and generally
yields a cleaner implementation at the cost of not having all ksd
related types/definitions in one header.

Implement the ksd functionality on ia64 by using inline functions
and permanently remove ksd.c from the ia64 specific makefile.

This change does not clean up the i386 specific version of ksd.h.

NOTE: The ksd code on ia64 abuses the tp register in the same way
as it is abused in libthr in that it is incompatible with the
runtime specification. This will be address when support for TLS
hits the tree.
2003-06-23 23:15:06 +00:00
Marcel Moolenaar
46559d7101 Change the definition of _ksd_curkse, _ksd_curthread and
_ksd_readandclear_tmbx to be function-like. That way we
can define them as inline functions or create prototypes
for them.

This change allows the ksd interface on ia64 to be fully
inlined.
2003-06-23 09:49:16 +00:00
Marcel Moolenaar
ca4b6c293b Define THR_{G|S}ETCONTEXT to expand to {g|s}etcontext(2).
Define THR_ALIGN to align at 16-byte boundaries.
2003-06-23 04:52:09 +00:00
Marcel Moolenaar
97caaa6522 Implement atomic_swap_{int|long|ptr}. Define atomic_swap_ptr as a
macro that expands to atomic_swap_long() to avoid compiler warnings
caused by incompatible pointer passing.
2003-06-23 04:44:43 +00:00
Marcel Moolenaar
c2dac9a63d Remove _atomic_lock. It's not used. 2003-06-23 04:33:32 +00:00
Marcel Moolenaar
842728619a Move the machine specific files from sys/Makefile.inc and put them
in a machine specific makefile. While here, sort the sub-directories
in Makefile and remove _atomic_lock.S from all makefiles.
2003-06-23 04:28:31 +00:00
David Xu
eb90369fa6 Don't lock scheduler lock twice. 2003-06-18 06:08:03 +00:00
Daniel Eischen
690f13f3c3 After selecting a thread to handle a signal and taking
its scheduling lock, make sure that the thread still has
the signal unmasked.

Make a debug statement conditional on debugging being
enabled.
2003-06-08 17:37:21 +00:00
Daniel Eischen
f91de797ce Insert threads at the end of the free thread list so that
the chance of getting the same thread id when allocating a
new thread is reduced.  This won't work if the application
creates a new thread for every time a thread exits, but
we're still within the allowances of POSIX.
2003-06-08 17:35:11 +00:00
Daniel Eischen
4d6f145a3b Provide a reference to __sys_write. The implementation uses this when
debugging is enabled so the symbol needs to be resolved before rtld
locking is enabled.  I may not really know what I'm talking about,
but it works.

Submitted by:	kan
2003-06-08 17:29:32 +00:00
Philippe Charnier
19677e1070 The .Fn function 2003-06-08 10:40:44 +00:00
Warner Losh
cedfd4f63f Don't force -L/usr/lib. This is incorrect because we should not be
looking at the host environment for anything.  This breaks building
-CURRENT on 4.x as well.

Submitted by: kan@
2003-06-08 03:58:20 +00:00
David Xu
a05fa0abea Only init _thread_sigact once, needn't init it again after a fork().
Obtained from: deischen
2003-06-04 12:40:21 +00:00
David Xu
cd0a0c267b Despite whether threaded mode is turned on, always save thread's
signal mask.
2003-06-04 12:38:21 +00:00
David Xu
e84a8d0d65 KMF_DONE is now in /sys/sys/kse.h, no longer need to define it here. 2003-06-04 03:22:59 +00:00
David Xu
a4c69f224b Free memory of internal low level lock when mutex and condition variable
are destroyed.

Submitted by: tegge
2003-06-03 02:21:01 +00:00
David Xu
9abece6475 Save THR_FLAGS_IN_TDLIST in signal frame, otherwise if a thread received
a signal will can not be removed from thread list after it exited.

Reviewed by: deischen
Approved by: re (jhb)
2003-05-30 14:50:16 +00:00
Alexander Kabaev
84d55c7fad Attempt to eliminate PLT relocations from rwlock aquire/release
path, making them suitable for direct use by the dynamic loader.

Register libpthread-specific locking API with rtld on startup.

This still has some rough edges with signals which should be
addresses later.

Approved by:	re (scottl)
2003-05-30 00:21:52 +00:00
Daniel Eischen
43dd76d242 Call the __sys_sigprocmask(the system call) when sigprocmask()
is called and the application is not threaded.  This works around
a problem when an application that hasn't yet become threaded
tries to jump out of a signal handler.

Reported by:	mbr
Approved by:	re@ (rwatson)
2003-05-30 00:09:22 +00:00
Daniel Eischen
28362a5c80 Don't really spin on a spinlock; silently convert it to the same
low-level lock used by the libpthread implementation.  In the
future, we'll eliminate spinlocks from libc but that will wait
until after 5.1-release.

Don't call an application signal handler if the handler is
the same as the library-installed handler.  This seems to
be possible after a fork and is the cause of konsole hangs.

Approved by:	re@ (jhb)
2003-05-29 17:10:45 +00:00
Ruslan Ermilov
734ac3b543 mdoc(7) fixes.
Approved by:	re (blanket)
2003-05-24 19:53:08 +00:00
Daniel Eischen
1cb570c531 Change low-level locking a bit so that we can tell if
a lock is being waitied on.

Fix a races in join and cancellation.

When trying to wait on a CV and the library is not yet
threaded, make it threaded so that waiting actually works.

When trying to nanosleep() and we're not threaded, just
call the system call nanosleep instead of adding the thread
to the wait queue.

Clean up adding/removing new threads to the "all threads queue",
assigning them unique ids, and tracking how many active threads
there are.  Do it all when the thread is added to the scheduling
queue instead of making pthread_create() know how to do it.

Fix a race where a thread could be marked for signal delivery
but it could be exited before we actually add the signal to it.

Other minor cleanups and bug fixes.

Submitted by:	davidxu
Approved by:	re@ (blanket for libpthread)
2003-05-24 02:29:25 +00:00
Daniel Eischen
28f318b941 Eek, staticize a couple of functions that shouldn't
be external (initialize()!).

Remove cancellation points from _pthread_cond_wait and
_pthread_cond_timedwait (single underscore versions are
libc private functions).  Point the weak reference(!) for
these functions to the versions with cancellation points.

Approved by:	re@(blanket till 5/19)
Pointed out by:	kan (cancellation point bug)
2003-05-19 23:04:50 +00:00
Daniel Eischen
fd626336fd Add a method of yielding the current thread with the scheduler
lock held (_thr_sched_switch_unlocked()) and use this to avoid
dropping the scheduler lock and having the scheduler retake the
same lock again.

Add a better way of detecting if a low-level lock is in use.

When switching out a thread due to blocking in the UTS, don't
switch to the KSE's scheduler stack only to switch back to
another thread.  If possible switch to the new thread directly
from the old thread and avoid the overhead of the extra
context switch.

Check for pending signals on a thread when entering the scheduler
and add them to the threads signal frame.  This includes some
other minor signal fixes.

Most of this was a joint effor between davidxu and myself.

Reviewed by:	davidxu
Approved by:	re@ (blanket for libpthread)
2003-05-16 19:58:30 +00:00
Daniel Eischen
07e6b1c7a3 Make pthread_join() async-cancel-safe. David was going to commit
this, but I think he's asleep and want to be sure it gets in before
the freeze.

Submitted by:	davidxu
2003-05-06 00:02:54 +00:00
David Xu
f508d26091 call dump_queues() only when DEBUG_THREAD_KERN is defined, save some
cpu cycles.
2003-05-05 05:01:19 +00:00
Daniel Eischen
c72cd7c9e2 Protect against a race between granting a lock and accessing
other parts of the lock.

Submitted by:	davidxu
2003-05-04 22:29:09 +00:00
Daniel Eischen
40791d9d15 Fix suspend and resume.
Submitted (in part) by:	Kazuaki Oda <kaakun@highway.ne.jp>
2003-05-04 16:17:01 +00:00
David Xu
99c883294c Handle thread canceled case, it is same as signal caused backout,
but will break out of loop.
2003-05-02 11:39:00 +00:00
Daniel Eischen
d143dde438 Move the mailbox to the beginning of the thread and align the
thread so that the context (SSE FPU state) is also aligned.
2003-04-30 15:05:17 +00:00
David Xu
d1021be03f Call kse_wakeup_mutli() after remove current thread from RUNQ to avoid
doing unnecessary idle kse wakeup.
2003-04-30 01:15:21 +00:00
David Xu
30a2952c90 Call kse_wakeup_multi() to wakeup idle KSEs when there are threads ready
to run.
2003-04-30 01:03:58 +00:00
Daniel Eischen
a8b5a9cd12 Jump to the correct label upon detecting an error. 2003-04-29 21:05:17 +00:00
Daniel Eischen
6cc13fa9ad Create the thread signal lock as a KSE lock (as opposed to
a thread lock).

Better protect access to thread state while searching for
threads to handle a signal.

Better protect access to process pending signals while processing
a thread in sigwait().

Submitted by:	davidxu
2003-04-29 21:03:33 +00:00
Daniel Eischen
55613576f5 o Don't add a scope system thread's KSE to the list of available
KSEs when it's thread exits; allow the GC handler to do that.

o Make spinlock/spinlock critical regions.

The following were submitted by davidxu

  o Alow thr_switch() to take a null mailbox argument.

  o Better protect cancellation checks.

  o Don't set KSE specific data when creating new KSEs; rely on the
    first upcall of the KSE to set it.

  o Add the ability to set the maximum concurrency level and do this
    automatically.  We should have a way to enable/disable this with
    some sort of tunable because some applications may not want this
    to be the default.

  o Hold the scheduling lock across thread switch calls.

  o If scheduling of a thread fails, make sure to remove it from the list
    of active threads.

  o Better protect accesses to a joining threads when the target thread is
    exited and detached.

  o Remove some macro definitions that are now provided by <sys/kse.h>.

  o Don't leave the library in threaded mode if creation of the initial
    KSE fails.

  o Wakeup idle KSEs when there are threads ready to run.

  o Maintain the number of threads active in the priority queue.
2003-04-28 23:56:12 +00:00
Daniel Eischen
76f344139c Use the correct link entry for walking the list of threads.
While I'm here, use the TAILQ_FOREACH macro instead of a more
manual method which was inherited from libc_r (so we could
remove elements from the list which isn't needed for libpthread).

Submitted by:	Kazuaki Oda <kaakun@highway.ne.jp>
2003-04-28 21:35:06 +00:00
Daniel Eischen
fd47bf962d Remove the %gs restoring hack (already commented out).
Don't install man pages.

Temporarily (again) rename the library to libkse.  It will be put back
to libpthread after more wide-spread testing.
2003-04-25 01:31:56 +00:00
Daniel Eischen
10629b6587 Add a macro to get the current thread mailbox pointer. 2003-04-23 21:49:34 +00:00
Daniel Eischen
c159269082 Remove the i386-specific hack (well, we only run on i386 anyways)
to always set %gs when resuming a thread.

Install this library as libpthread instead of libkse.
2003-04-23 21:48:29 +00:00
Daniel Eischen
f1c8192fd4 Protect thread errno from being changed while operating
on behalf of the KSE.

Add a kse_reinit function to reinitialize a reused KSE.

Submitted by:	davidxu
2003-04-23 21:46:50 +00:00