freebsd-dev/lib/libthr/thread/thr_cancel.c
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

144 lines
3.2 KiB
C

/*
* David Leonard <d@openbsd.org>, 1999. Public domain.
* $FreeBSD$
*/
#include <sys/errno.h>
#include <pthread.h>
#include <stdlib.h>
#include "thr_private.h"
/*
* Static prototypes
*/
static void testcancel(void);
__weak_reference(_pthread_cancel, pthread_cancel);
__weak_reference(_pthread_setcancelstate, pthread_setcancelstate);
__weak_reference(_pthread_setcanceltype, pthread_setcanceltype);
__weak_reference(_pthread_testcancel, pthread_testcancel);
/*
* Posix requires this function to be async-cancel-safe, so it
* may not aquire any type of resource or call any functions
* that might do so.
*/
int
_pthread_cancel(pthread_t pthread)
{
/* Don't continue if cancellation has already been set. */
if (atomic_cmpset_int(&pthread->cancellation, (int)CS_NULL,
(int)CS_PENDING) != 1)
return (0);
/*
* Only wakeup threads that are in cancellation points or
* have set async cancel.
* XXX - access to pthread->flags is not safe. We should just
* unconditionally wake the thread and make sure that
* the the library correctly handles spurious wakeups.
*/
if ((pthread->cancellationpoint || pthread->cancelmode == M_ASYNC) &&
(pthread->flags & PTHREAD_FLAGS_NOT_RUNNING) != 0)
PTHREAD_WAKE(pthread);
return (0);
}
/*
* Posix requires this function to be async-cancel-safe, so it
* may not aquire any type of resource or call any functions
* that might do so.
*/
int
_pthread_setcancelstate(int state, int *oldstate)
{
int ostate;
ostate = (curthread->cancelmode == M_OFF) ? PTHREAD_CANCEL_DISABLE :
PTHREAD_CANCEL_ENABLE;
switch (state) {
case PTHREAD_CANCEL_ENABLE:
curthread->cancelmode = curthread->cancelstate;
break;
case PTHREAD_CANCEL_DISABLE:
if (curthread->cancelmode != M_OFF) {
curthread->cancelstate = curthread->cancelmode;
curthread->cancelmode = M_OFF;
}
break;
default:
return (EINVAL);
}
if (oldstate != NULL)
*oldstate = ostate;
return (0);
}
/*
* Posix requires this function to be async-cancel-safe, so it
* may not aquire any type of resource or call any functions that
* might do so.
*/
int
_pthread_setcanceltype(int type, int *oldtype)
{
enum cancel_mode omode;
omode = curthread->cancelstate;
switch (type) {
case PTHREAD_CANCEL_ASYNCHRONOUS:
if (curthread->cancelmode != M_OFF)
curthread->cancelmode = M_ASYNC;
curthread->cancelstate = M_ASYNC;
break;
case PTHREAD_CANCEL_DEFERRED:
if (curthread->cancelmode != M_OFF)
curthread->cancelmode = M_DEFERRED;
curthread->cancelstate = M_DEFERRED;
break;
default:
return (EINVAL);
}
if (oldtype != NULL) {
if (omode == M_DEFERRED)
*oldtype = PTHREAD_CANCEL_DEFERRED;
else if (omode == M_ASYNC)
*oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
}
return (0);
}
void
_pthread_testcancel(void)
{
testcancel();
}
static void
testcancel()
{
if (curthread->cancelmode != M_OFF) {
/* Cleanup a canceled thread only once. */
if (atomic_cmpset_int(&curthread->cancellation,
(int)CS_PENDING, (int)CS_SET) == 1) {
_thread_exit_cleanup();
pthread_exit(PTHREAD_CANCELED);
PANIC("cancel");
}
}
}
void
_thread_enter_cancellation_point(void)
{
testcancel();
curthread->cancellationpoint = 1;
}
void
_thread_leave_cancellation_point(void)
{
curthread->cancellationpoint = 0;
testcancel();
}