Commit Graph

185 Commits

Author SHA1 Message Date
Daniel Eischen
b4145b0bfa Enable _PTHREADS_INVARIANTS until the recent libc_r changes are
shaken out.
2000-10-13 22:19:50 +00:00
Daniel Eischen
fbeb36e4bf Implement zero system call thread switching. Performance of
thread switches should be on par with that under scheduler
activations.

  o Timing is achieved through the use of a fixed interval
    timer (ITIMER_PROF) to count scheduling ticks instead
    of retrieving the time-of-day upon every thread switch
    and calculating elapsed real time.

  o Polling for I/O readiness is performed once for each
    scheduling tick instead of every thread switch.

  o The non-signal saving/restoring versions of setjmp/longjmp
    are used to save and restore thread contexts.  This may
    allow the removal of _THREAD_SAFE macros from setjmp()
    and longjmp() - needs more investigation.

Change signal handling so that signals are handled in the
context of the thread that is receiving the signal.  When
signals are dispatched to a thread, a special signal handling
frame is created on top of the target threads stack.  The
frame contains the threads saved state information and a new
context in which the thread can run.  The applications signal
handler is invoked through a wrapper routine that knows how
to restore the threads saved state and unwind to previous
frames.

Fix interruption of threads due to signals.  Some states
were being improperly interrupted while other states were
not being interrupted.  This should fix several PRs.

Signal handlers, which are invoked as a result of a process
signal (not by pthread_kill()), are now called with the
code (or siginfo_t if SA_SIGINFO was set in sa_flags) and
sigcontext_t as received from the process signal handler.

Modify the search for a thread to which a signal is delivered.
The search algorithm is now:

  o First thread found in sigwait() with signal in wait mask.
  o First thread found sigsuspend()'d on the signal.
  o Current thread if signal is unmasked.
  o First thread found with signal unmasked.

Collapse machine dependent support into macros defined in
pthread_private.h.  These should probably eventually be moved
into separate MD files.

Change the range of settable priorities to be compliant with
POSIX (0-31).  The threads library uses higher priorities
internally for real-time threads (not yet implemented) and
threads executing signal handlers.  Real-time threads and
threads running signal handlers add 64 and 32, respectively,
to a threads base priority.

Some other small changes and cleanups.

PR:		17757 18559 21943
Reviewed by:	jasone
2000-10-13 22:12:32 +00:00
Maxim Sobolev
24a1dab36f Add thread-safe wrapper for fpathconf(2) syscall.
Reviewed by:	jlemon
2000-09-19 18:01:03 +00:00
Jason Evans
f3ee83c3bf The second call to _thread_kern_sig_defer() in sem_post() should be a call
to _thread_kern_sig_undefer().
2000-08-23 07:59:50 +00:00
Alfred Perlstein
022daa34f2 Fix an off-by-one error in the recursive mutex handling that made it
prematurely release recursive mutexes.

Test case provided by: Bradley T. Hughes <bhughes@trolltech.com>
Reviewed by: deischen
2000-08-13 01:30:36 +00:00
Jonathan Lemon
532c92a865 Add wrapper for kevent() syscall
Noted as missing by: nicolas.leonard@animaths.com
2000-08-07 16:51:56 +00:00
Jason Evans
b167c9a5c1 Make sem_post() safe to call from within a signal handler, as required by
POSIX/SUSv2.
2000-08-01 21:19:09 +00:00
Doug Rabson
678ef1b49e Call _thread_init() from pthread_once() if it has not already been called.
This fixes a segfault in some C++ programs which use exceptions before
main() has been called (i.e. from global constructors).

Reviewed by: deischen
2000-07-21 09:31:13 +00:00
Jason Evans
8e234adf86 Change my email address in the copyright notices for the sake of consistency
(jasone@canonware.com --> jasone@freebsd.org).
2000-07-18 01:38:19 +00:00
Jason Evans
390a1cd5eb Deal correctly with statically initialized condition variables in
pthread_cond_signal(), pthread_cond_broadcast(), and pthread_cond_timedwait().

Do not dump core in pthread_cond_timedwait() (due to a NULL pointer
dereference) if attempting to wait on an uninitialized condition variable.

PR:	bin/18099
2000-07-17 22:55:05 +00:00
Jason Evans
e21fa6847e Remove DEBUG_FLAGS=-g3, which never should have been committed. 2000-07-05 16:21:42 +00:00
Jason Evans
8d107d1210 If multiple threads are blocked in sigwait() for the same signal that does
not have a user-supplied signal handler, when a signal is delivered, one
thread will receive the signal, and then the code reverts to having no
signal handler for the signal.  This can leave the other sigwait()ing
threads stranded permanently if the signal is later ignored, or can result
in process termination when the process should have delivered the signal to
one of the threads in sigwait().

To fix this problem, maintain a count of sigwait()ers for each signal that
has no default signal handler.  Use the count to correctly install/uninstall
dummy signal handlers.

Reviewed by:	deischen
2000-06-27 21:30:16 +00:00
Jason Evans
314be1347b pthread_mutex_lock(), pthread_cond_trywait(), and pthread_cond_wait() are
not allowed to return EINTR, but use of pthread_suspend_np() could cause
EINTR to be returned.  To fix this, restructure pthread_suspend_np() so that
it does not interrupt a thread that is waiting on a mutex or condition, and
keep enough state around that pthread_resume_np() can fix things up
afterwards.

Reviewed by:	deischen
2000-06-14 17:17:41 +00:00
Jake Burkholder
e39756439c Back out the previous change to the queue(3) interface.
It was not discussed and should probably not happen.

Requested by:		msmith and others
2000-05-26 02:09:24 +00:00
Jake Burkholder
740a1973a6 Change the way that the queue(3) structures are declared; don't assume that
the type argument to *_HEAD and *_ENTRY is a struct.

Suggested by:	phk
Reviewed by:	phk
Approved by:	mdodd
2000-05-23 20:41:01 +00:00
Jason Evans
ccb3a748f4 Fix a memory leak. pthread_set_name_np() allocates space for a name, but
was not deallocating space for the previous name, if any.

PR:	misc/18504
2000-05-16 22:08:14 +00:00
Bruce Evans
80a07932e7 Fixed missing consts for function parameters, so that the code matches
the man page and POSIX.1.  Fixed nearby misformatting.  Fixed a missing
prototype.
2000-05-11 15:57:17 +00:00
Jason Evans
4c089f4dff Add missing man pages. Fix various compliance bugs, mostly having to do with
error return values.  Implement pthread_mutexattr_gettype().

PR:		docs/16537, docs/17538
2000-05-02 06:51:40 +00:00
Jason Evans
98a1f447bb Add a wrapper for the sendfile() system call.
PR:		bin/17366
2000-04-27 00:59:44 +00:00
Jason Evans
e419521997 Add test to detect propagation of cancellation points within libc_r. 2000-04-26 23:25:58 +00:00
Jason Evans
be1d533999 Automated regression test harness for libc_r. Existing tests are integrated,
a new test for POSIX semaphores was added, and examples of harness usage are
included.
2000-04-24 21:07:45 +00:00
Jason Evans
7e5e179982 Explicitly include sys/cdefs.h to get the definition of __strong_reference(),
rather than getting lucky due to header dependencies.
2000-03-18 22:36:46 +00:00
Daniel Eischen
1d013a86ed Fix pthread_suspend_np/pthread_resume_np. For the record, suspending a
thread waiting on an event (I/O, condvar, etc) will, when resumed using
pthread_resume_np, return with EINTR.  For example, suspending and resuming
a thread blocked on read() will not requeue the thread for the read, but
will return -1 with errno = EINTR.  If the suspended thread is in a critical
region, the thread is suspended as soon as it leaves the critical region.

Fix a bogon in pthread_kill() where a signal was being delivered twice
to threads waiting in sigwait().

Reported by (suspend/resume bug):	jdp
Reviewed by:	jasone
2000-03-15 13:59:27 +00:00
Jason Evans
8a668b9049 For errors, return -1 and set errno to indicate the error type, rather than
returning the error directly.

For sem_post(), make sure that the correct thread is woken up.  This has
unfortunate performance implications, but is necessary for POSIX compliance.

Approved by:	jkh
2000-02-16 19:34:53 +00:00
Jason Evans
d77639f25f Use __strong_reference() instead of __weak_reference() to assure that the
weak symbols of the same name are not used.
2000-01-29 22:53:55 +00:00
Jason Evans
4ec6f67d10 Use _fcntl() (not fcntl()) inside of fcntl().
Reported by:	green
2000-01-28 22:10:27 +00:00
Jason Evans
9233c4d942 Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo().  In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate.  In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().

Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().

Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().

Make thread cancellation fully POSIX-compliant.

Suggested by:	deischen
2000-01-27 23:07:25 +00:00
Jason Evans
0c5d1a3361 Fix millisecond to nanosecond conversion.
PR:		misc/16245
2000-01-22 09:15:15 +00:00
Jason Evans
beab1ec9b5 Minor *jmp() cleanups. 2000-01-20 21:53:59 +00:00
Jason Evans
120bfc9ded Add sem_*() functions. Named semaphores and process-shared semaphores
are not supported by this implementation, and the error return values
from sem_init(), sem_open(), sem_close(), and sem_unlink() reflect this.

Approved by:	jkh
2000-01-20 07:54:49 +00:00
Jason Evans
adbd6ee028 Do signal deferral for pthread_kill() as it was done in the old days.
Submitted by:	deischen
2000-01-20 04:46:52 +00:00
Jason Evans
0a3fa43c7e Implement continuations to correctly handle [sig|_]longjmp() inside of a
signal handler.  Explicitly check for jumps to anywhere other than the
current stack, since such jumps are undefined according to POSIX.

While we're at it, convert thread cancellation to use continuations, since
it's cleaner than the original cancellation code.

Avoid delivering a signal to a thread twice.  This was a pre-existing bug,
but was likely unexposed until these other changes were made.

Defer signals generated by pthread_kill() so that they can be delivered on
the appropriate stack.  deischen claims that this is unnecessary, which is
likely true, but without this change, pthread_kill() can cause undefined
priority queue states and/or PANICs in [sig|_]longjmp(), so I'm leaving
this in for now.  To compile this code out and exercise the bug, define
the _NO_UNDISPATCH cpp macro.  Defining _PTHREADS_INVARIANTS as well will
cause earlier crashes.

PR:			kern/14685
Collaboration with:	deischen
2000-01-19 07:04:50 +00:00
Daniel Eischen
7e416d8e49 Properly initialize the last active time of the initial thread. This fixes
the case that a CPU hungry main thread is prevented from being preempted
due to a negative calculation of its time slice.

Reported by:	Alexander Litvin <archer@lucky.net>
2000-01-18 11:35:25 +00:00
Jason Evans
f560c4e709 Track libc's three-tier symbol naming. libc_r must currently implement
the _libc_*() entry points and add *() weak aliases.  This will all
change for the better when libc_r becomes libpthread.
2000-01-12 09:28:58 +00:00
Daniel Eischen
3eadb69d1f Make sched_param parameter a const to comply with POSIX and SUSv2 specs.
This doesn't need to be applied to stable, because somehow -stable seems
to have gotten it right.

Reviewed by:	jasone
2000-01-10 04:14:08 +00:00
Jason Evans
91e92a2d3d Don't explicitly mmap() red zones at the bottom of thread stacks (except
the initial thread).  Instead, just leave an unmapped gap between thread
stacks and make sure that the thread stacks won't grow into these gaps,
simply by limiting the size of the stacks with the 'len' argument to
mmap().  This (if I understand correctly) reduces VM overhead
considerably.

Reviewed by:	deischen
1999-12-29 15:44:59 +00:00
Daniel Eischen
1752e66d49 -Wall and minor style(9) cleanups. 1999-12-28 18:13:04 +00:00
Daniel Eischen
69186ed701 Change stack allocation algorithm to make better use of memory
(it was leaving an unused block).  Also protect the global stack
pointer from context changes while fiddling with it.
1999-12-28 18:12:07 +00:00
Daniel Eischen
8d048bba15 Don't wakeup threads when there is a process signal and no installed
handler.  Thread-to-thread signals (pthread_signal) are treated differently
than process signals; a pthread_signal can wakeup a blocked thread if
a signal handler is not installed for that signal.

Found by:	ACE tests
1999-12-28 18:08:09 +00:00
Jason Evans
386812d440 Fix some minor POSIX/SUSv2 compliance nits.
PR:		kern/11982
1999-12-18 01:00:42 +00:00
Daniel Eischen
fc8f3f5bfe Fix problems with cancellation while in critical regions.
o Cancellation flags were not getting properly set/cleared.
  o Loops waiting for internal locks were not being exited
    correctly by a cancelled thread.
  o Minor spelling (cancelation -> cancellation) and formatting
    corrections (missing tab).

Found by:	tg
Reviewed by:	jasone
1999-12-17 00:57:54 +00:00
Daniel Eischen
3dc268f4e7 Fixes for signal handling:
o Don't call signal handlers with the signal handler access lock
    held.
  o Remove pending signals before calling signal handlers.  If
    pending signals were not removed prior to handling them,
    invocation of the handler could cause the handler to be
    called more than once for the same signal.  Found by: JB
  o When SIGCHLD arrives, wake up all threads in PS_WAIT_WAIT
    (wait4).

PR:		bin/15328
Reviewed by:	jasone
1999-12-17 00:56:36 +00:00
Jason Evans
e6a5e33c6b Avoid an infinite loop if the last element of the iov array passed to
writev() has an iov_len of 0.

PR:		bin/8281
1999-12-16 22:35:40 +00:00
Daniel Eischen
4fc937ef8f Change signal handling to conform to POSIX specified semantics.
Before this change, a signal was delivered to each thread that
didn't have the signal masked.  Signals also improperly woke up
threads waiting on I/O.  With this change, signals are now
handled in the following way:

  o If a thread is waiting in a sigwait for the signal,
    then the thread is woken up.

  o If no threads are sigwait'ing on the signal and a
    thread is in a sigsuspend waiting for the signal,
    then the thread is woken up.

  o In the case that no threads are waiting or suspended
    on the signal, then the signal is delivered to the
    first thread we find that has the signal unmasked.

  o If no threads are waiting or suspended on the signal,
    and no threads have the signal unmasked, then the signal
    is added to the process wide pending signal set.  The
    signal will be delivered to the first thread that unmasks
    the signal.

If there is an installed signal handler, it is only invoked
if the chosen thread was not in a sigwait.

In the case that multiple threads are waiting or suspended
on a signal, or multiple threads have the signal unmasked,
we wake up/deliver the signal to the first thread we find.
The above rules still apply.

Reported by:	Scott Hess <scott@avantgo.com>
Reviewed by:	jb, jasone
1999-12-04 22:55:59 +00:00
Doug Rabson
3c085f72bb * Fix the stack allocation code so that it works for alpha. Change it
to use mmap(..., MAP_STACK, ...) on alpha too since that should work
  now.
* Add hooks to allow GDB to access the internals of pthreads without
  having to know the exact layout of struct pthread.

Reviewed by: deischen
1999-11-28 19:47:43 +00:00
Alfred Perlstein
7285bccf1a add pthread_cancel, obtained from OpenBSD.
eischen (Daniel Eischen) added wrappers to protect against cancled
threads orphaning internal resources.

the cancelability code is still a bit fuzzy but works for test
programs of my own, OpenBSD's and some examples from ORA's books.

add readdir_r to both libc and libc_r

add some 'const' attributes to function parameters

Reviewed by: eischen, jasone
1999-11-28 05:38:13 +00:00
Marcel Moolenaar
616842b791 Add sigsuspend.o and sigpending.o to HIDDEN_SYSCALLS as well.
Pointed out by: jdp
1999-10-14 10:08:53 +00:00
Marcel Moolenaar
a8ce772846 Add sigprocmask to HIDDEN_SYSCALLS. This renames the syscall to
_thread_sys_sisprocmask in libc_r. This solves the undefined symbol...

Reported by: Kenneth Wayne Culver
1999-09-30 15:07:43 +00:00
Marcel Moolenaar
74562f1a2a Add the proper headers so that the SIGNOTEMPTY and SIGSETAND
macros are defined.
1999-09-30 14:51:31 +00:00
Marcel Moolenaar
3cf3c5d9dd sigset_t change (part 5 of 5)
-----------------------------

Most of the userland changes are in libc. For both the alpha
and the i386 setjmp has been changed to accomodate for the
new sigset_t. Internally, libc is mostly rewritten to use the
new syscalls. The exception is in compat-43/sigcompat.c

The POSIX thread library has also been rewritten to use the
new sigset_t. Except, that it currently only handles NSIG
signals instead of the maximum _SIG_MAXSIG. This should not
be a problem because current applications don't use any
signals higher than NSIG.

There are version bumps for the following libraries:
  libdialog
  libreadline
  libc
  libc_r
  libedit
  libftpio
  libss

These libraries either a) have one of the modified structures
visible in the interface, or b) use sigset_t internally and
may cause breakage if new binaries are used against libraries
that don't have the sigset_t change. This not an immediate
issue, but will be as soon as applications start using the
new range to its fullest.

NOTE: libncurses already had an version bump and has not been
      given one now.

NOTE: doscmd is a real casualty and has been disconnected for
      the moment. Reconnection will eventually happen after
      doscmd has been fixed. I'm aware that being the last one
      to touch it, I'm automaticly promoted to being maintainer.
      According to good taste this means that I will receive a
      badge which either will be glued or mechanically stapled,
      drilled or otherwise violently forced onto me :-)

NOTE: pcvt/vttest cannot be compiled with -traditional. The
      change cause sys/types to be included along the way which
      contains the const and volatile modifiers. I don't consider
      this a solution, but more a workaround.
1999-09-29 15:18:46 +00:00