1999-11-28 05:38:13 +00:00
|
|
|
/*
|
|
|
|
* David Leonard <d@openbsd.org>, 1999. Public domain.
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "pthread_private.h"
|
|
|
|
|
2000-01-19 07:04:50 +00:00
|
|
|
static void finish_cancellation(void *arg);
|
|
|
|
|
2001-04-10 04:19:21 +00:00
|
|
|
__weak_reference(_pthread_cancel, pthread_cancel);
|
|
|
|
__weak_reference(_pthread_setcancelstate, pthread_setcancelstate);
|
|
|
|
__weak_reference(_pthread_setcanceltype, pthread_setcanceltype);
|
|
|
|
__weak_reference(_pthread_testcancel, pthread_testcancel);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_cancel(pthread_t pthread)
|
1999-11-28 05:38:13 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = _find_thread(pthread)) != 0) {
|
|
|
|
/* NOTHING */
|
|
|
|
} else if (pthread->state == PS_DEAD || pthread->state == PS_DEADLOCK) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
/* Protect the scheduling queues: */
|
|
|
|
_thread_kern_sig_defer();
|
|
|
|
|
1999-12-17 00:57:54 +00:00
|
|
|
if (((pthread->cancelflags & PTHREAD_CANCEL_DISABLE) != 0) ||
|
|
|
|
(((pthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) == 0) &&
|
|
|
|
((pthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0)))
|
|
|
|
/* Just mark it for cancellation: */
|
|
|
|
pthread->cancelflags |= PTHREAD_CANCELLING;
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Check if we need to kick it back into the
|
|
|
|
* run queue:
|
|
|
|
*/
|
1999-11-28 05:38:13 +00:00
|
|
|
switch (pthread->state) {
|
|
|
|
case PS_RUNNING:
|
|
|
|
/* No need to resume: */
|
|
|
|
pthread->cancelflags |= PTHREAD_CANCELLING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_SPINBLOCK:
|
|
|
|
case PS_FDR_WAIT:
|
|
|
|
case PS_FDW_WAIT:
|
|
|
|
case PS_POLL_WAIT:
|
|
|
|
case PS_SELECT_WAIT:
|
1999-12-17 00:57:54 +00:00
|
|
|
/* Remove these threads from the work queue: */
|
1999-11-28 05:38:13 +00:00
|
|
|
if ((pthread->flags & PTHREAD_FLAGS_IN_WORKQ)
|
|
|
|
!= 0)
|
|
|
|
PTHREAD_WORKQ_REMOVE(pthread);
|
|
|
|
/* Fall through: */
|
|
|
|
case PS_SIGTHREAD:
|
|
|
|
case PS_SLEEP_WAIT:
|
|
|
|
case PS_WAIT_WAIT:
|
|
|
|
case PS_SIGSUSPEND:
|
|
|
|
case PS_SIGWAIT:
|
|
|
|
/* Interrupt and resume: */
|
|
|
|
pthread->interrupted = 1;
|
|
|
|
pthread->cancelflags |= PTHREAD_CANCELLING;
|
|
|
|
PTHREAD_NEW_STATE(pthread,PS_RUNNING);
|
|
|
|
break;
|
|
|
|
|
2001-05-20 23:08:33 +00:00
|
|
|
case PS_JOIN:
|
2000-06-14 17:17:41 +00:00
|
|
|
case PS_SUSPENDED:
|
|
|
|
if (pthread->suspended == SUSP_NO ||
|
|
|
|
pthread->suspended == SUSP_YES ||
|
|
|
|
pthread->suspended == SUSP_NOWAIT) {
|
|
|
|
/*
|
|
|
|
* This thread isn't in any scheduling
|
|
|
|
* queues; just change it's state:
|
|
|
|
*/
|
|
|
|
pthread->cancelflags |=
|
|
|
|
PTHREAD_CANCELLING;
|
|
|
|
PTHREAD_SET_STATE(pthread, PS_RUNNING);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
1999-11-28 05:38:13 +00:00
|
|
|
case PS_MUTEX_WAIT:
|
|
|
|
case PS_COND_WAIT:
|
|
|
|
case PS_FDLR_WAIT:
|
|
|
|
case PS_FDLW_WAIT:
|
|
|
|
case PS_FILE_WAIT:
|
|
|
|
/*
|
|
|
|
* Threads in these states may be in queues.
|
|
|
|
* In order to preserve queue integrity, the
|
|
|
|
* cancelled thread must remove itself from the
|
|
|
|
* queue. Mark the thread as interrupted and
|
|
|
|
* needing cancellation, and set the state to
|
|
|
|
* running. When the thread resumes, it will
|
2000-01-19 07:04:50 +00:00
|
|
|
* remove itself from the queue and call the
|
|
|
|
* cancellation completion routine.
|
1999-11-28 05:38:13 +00:00
|
|
|
*/
|
|
|
|
pthread->interrupted = 1;
|
|
|
|
pthread->cancelflags |= PTHREAD_CANCEL_NEEDED;
|
|
|
|
PTHREAD_NEW_STATE(pthread,PS_RUNNING);
|
2000-01-19 07:04:50 +00:00
|
|
|
pthread->continuation = finish_cancellation;
|
1999-11-28 05:38:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_DEAD:
|
|
|
|
case PS_DEADLOCK:
|
|
|
|
case PS_STATE_MAX:
|
|
|
|
/* Ignore - only here to silence -Wall: */
|
|
|
|
break;
|
1999-12-17 00:57:54 +00:00
|
|
|
}
|
1999-11-28 05:38:13 +00:00
|
|
|
}
|
1999-12-17 00:57:54 +00:00
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
/* Unprotect the scheduling queues: */
|
|
|
|
_thread_kern_sig_undefer();
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_setcancelstate(int state, int *oldstate)
|
1999-11-28 05:38:13 +00:00
|
|
|
{
|
2001-01-24 13:03:38 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
1999-11-28 05:38:13 +00:00
|
|
|
int ostate;
|
|
|
|
int ret;
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
ostate = curthread->cancelflags & PTHREAD_CANCEL_DISABLE;
|
1999-11-28 05:38:13 +00:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case PTHREAD_CANCEL_ENABLE:
|
|
|
|
if (oldstate != NULL)
|
|
|
|
*oldstate = ostate;
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags &= ~PTHREAD_CANCEL_DISABLE;
|
|
|
|
if ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)
|
1999-11-28 05:38:13 +00:00
|
|
|
pthread_testcancel();
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_DISABLE:
|
|
|
|
if (oldstate != NULL)
|
|
|
|
*oldstate = ostate;
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags |= PTHREAD_CANCEL_DISABLE;
|
1999-11-28 05:38:13 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_setcanceltype(int type, int *oldtype)
|
1999-11-28 05:38:13 +00:00
|
|
|
{
|
2001-01-24 13:03:38 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
1999-11-28 05:38:13 +00:00
|
|
|
int otype;
|
|
|
|
int ret;
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
otype = curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS;
|
1999-11-28 05:38:13 +00:00
|
|
|
switch (type) {
|
|
|
|
case PTHREAD_CANCEL_ASYNCHRONOUS:
|
|
|
|
if (oldtype != NULL)
|
|
|
|
*oldtype = otype;
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS;
|
1999-11-28 05:38:13 +00:00
|
|
|
pthread_testcancel();
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_DEFERRED:
|
|
|
|
if (oldtype != NULL)
|
|
|
|
*oldtype = otype;
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS;
|
1999-11-28 05:38:13 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_testcancel(void)
|
1999-11-28 05:38:13 +00:00
|
|
|
{
|
2001-01-24 13:03:38 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
|
|
|
|
if (((curthread->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) &&
|
|
|
|
((curthread->cancelflags & PTHREAD_CANCELLING) != 0)) {
|
1999-11-28 05:38:13 +00:00
|
|
|
/*
|
|
|
|
* It is possible for this thread to be swapped out
|
|
|
|
* while performing cancellation; do not allow it
|
|
|
|
* to be cancelled again.
|
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags &= ~PTHREAD_CANCELLING;
|
1999-11-28 05:38:13 +00:00
|
|
|
_thread_exit_cleanup();
|
2001-05-20 23:08:33 +00:00
|
|
|
pthread_detach((pthread_t)curthread);
|
1999-11-28 05:38:13 +00:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
PANIC("cancel");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thread_enter_cancellation_point(void)
|
|
|
|
{
|
2001-01-24 13:03:38 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
/* Look for a cancellation before we block: */
|
|
|
|
pthread_testcancel();
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags |= PTHREAD_AT_CANCEL_POINT;
|
1999-11-28 05:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thread_leave_cancellation_point(void)
|
|
|
|
{
|
2001-01-24 13:03:38 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
|
|
|
|
curthread->cancelflags &= ~PTHREAD_AT_CANCEL_POINT;
|
1999-11-28 05:38:13 +00:00
|
|
|
/* Look for a cancellation after we unblock: */
|
|
|
|
pthread_testcancel();
|
|
|
|
}
|
2000-01-19 07:04:50 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
finish_cancellation(void *arg)
|
|
|
|
{
|
2001-01-24 13:03:38 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
|
|
|
|
curthread->continuation = NULL;
|
|
|
|
curthread->interrupted = 0;
|
2000-01-19 07:04:50 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
if ((curthread->cancelflags & PTHREAD_CANCEL_NEEDED) != 0) {
|
|
|
|
curthread->cancelflags &= ~PTHREAD_CANCEL_NEEDED;
|
2000-01-19 07:04:50 +00:00
|
|
|
_thread_exit_cleanup();
|
2001-05-20 23:08:33 +00:00
|
|
|
pthread_detach((pthread_t)curthread);
|
2000-01-19 07:04:50 +00:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
}
|