1999-11-28 05:38:13 +00:00
|
|
|
/*
|
|
|
|
* David Leonard <d@openbsd.org>, 1999. Public domain.
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <pthread.h>
|
2002-09-16 08:45:36 +00:00
|
|
|
#include "thr_private.h"
|
1999-11-28 05:38:13 +00:00
|
|
|
|
2006-03-13 00:59:51 +00:00
|
|
|
LT10_COMPAT_PRIVATE(_pthread_cancel);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_cancel);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_setcancelstate);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_setcancelstate);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_setcanceltype);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_setcanceltype);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_testcancel);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_testcancel);
|
|
|
|
|
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
|
|
|
|
2003-12-09 02:20:56 +00:00
|
|
|
static inline int
|
|
|
|
checkcancel(struct pthread *curthread)
|
|
|
|
{
|
2004-12-18 18:07:37 +00:00
|
|
|
if ((curthread->cancelflags & THR_CANCELLING) != 0) {
|
2003-12-09 02:20:56 +00:00
|
|
|
/*
|
|
|
|
* It is possible for this thread to be swapped out
|
|
|
|
* while performing cancellation; do not allow it
|
|
|
|
* to be cancelled again.
|
|
|
|
*/
|
2004-12-18 18:07:37 +00:00
|
|
|
if ((curthread->flags & THR_FLAGS_EXITING) != 0) {
|
|
|
|
/*
|
|
|
|
* This may happen once, but after this, it
|
|
|
|
* shouldn't happen again.
|
|
|
|
*/
|
|
|
|
curthread->cancelflags &= ~THR_CANCELLING;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if ((curthread->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) {
|
|
|
|
curthread->cancelflags &= ~THR_CANCELLING;
|
|
|
|
return (1);
|
|
|
|
}
|
2003-12-09 02:20:56 +00:00
|
|
|
}
|
2004-12-18 18:07:37 +00:00
|
|
|
return (0);
|
2003-12-09 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
testcancel(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
if (checkcancel(curthread) != 0) {
|
|
|
|
/* Unlock before exiting: */
|
|
|
|
THR_THREAD_UNLOCK(curthread, curthread);
|
|
|
|
|
|
|
|
_thr_exit_cleanup();
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
PANIC("cancel");
|
|
|
|
}
|
|
|
|
}
|
2003-04-18 05:04:16 +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
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2003-05-24 02:29:25 +00:00
|
|
|
struct pthread *joinee = NULL;
|
2003-07-23 02:11:07 +00:00
|
|
|
struct kse_mailbox *kmbx = NULL;
|
1999-11-28 05:38:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0)) == 0) {
|
|
|
|
/*
|
2003-10-08 00:30:38 +00:00
|
|
|
* Take the thread's lock while we change the cancel flags.
|
2003-04-18 05:04:16 +00:00
|
|
|
*/
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_LOCK(curthread, pthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SCHED_LOCK(curthread, pthread);
|
2003-06-28 09:39:35 +00:00
|
|
|
if (pthread->flags & THR_FLAGS_EXITING) {
|
|
|
|
THR_SCHED_UNLOCK(curthread, pthread);
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(curthread, pthread);
|
2003-06-28 09:39:35 +00:00
|
|
|
_thr_ref_delete(curthread, pthread);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
1999-12-17 00:57:54 +00:00
|
|
|
if (((pthread->cancelflags & PTHREAD_CANCEL_DISABLE) != 0) ||
|
2003-04-18 05:04:16 +00:00
|
|
|
(((pthread->cancelflags & THR_AT_CANCEL_POINT) == 0) &&
|
|
|
|
((pthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) == 0)))
|
1999-12-17 00:57:54 +00:00
|
|
|
/* Just mark it for cancellation: */
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->cancelflags |= THR_CANCELLING;
|
1999-12-17 00:57:54 +00:00
|
|
|
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: */
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->cancelflags |= THR_CANCELLING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_LOCKWAIT:
|
|
|
|
/*
|
|
|
|
* These can't be removed from the queue.
|
|
|
|
* Just mark it as cancelling and tell it
|
|
|
|
* to yield once it leaves the critical
|
|
|
|
* region.
|
|
|
|
*/
|
|
|
|
pthread->cancelflags |= THR_CANCELLING;
|
|
|
|
pthread->critical_yield = 1;
|
1999-11-28 05:38:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_SLEEP_WAIT:
|
2003-02-17 10:05:18 +00:00
|
|
|
case PS_SIGSUSPEND:
|
|
|
|
case PS_SIGWAIT:
|
1999-11-28 05:38:13 +00:00
|
|
|
/* Interrupt and resume: */
|
|
|
|
pthread->interrupted = 1;
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->cancelflags |= THR_CANCELLING;
|
2003-07-23 02:11:07 +00:00
|
|
|
kmbx = _thr_setrunnable_unlocked(pthread);
|
1999-11-28 05:38:13 +00:00
|
|
|
break;
|
|
|
|
|
2001-05-20 23:08:33 +00:00
|
|
|
case PS_JOIN:
|
2003-05-24 02:29:25 +00:00
|
|
|
/* Disconnect the thread from the joinee: */
|
|
|
|
joinee = pthread->join_status.thread;
|
|
|
|
pthread->join_status.thread = NULL;
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->cancelflags |= THR_CANCELLING;
|
2003-07-23 02:11:07 +00:00
|
|
|
kmbx = _thr_setrunnable_unlocked(pthread);
|
2003-05-24 02:29:25 +00:00
|
|
|
if ((joinee != NULL) &&
|
2003-07-02 13:23:03 +00:00
|
|
|
(pthread->kseg == joinee->kseg)) {
|
2003-05-24 02:29:25 +00:00
|
|
|
/* Remove the joiner from the joinee. */
|
|
|
|
joinee->joiner = NULL;
|
|
|
|
joinee = NULL;
|
|
|
|
}
|
2001-08-16 06:31:32 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-14 17:17:41 +00:00
|
|
|
case PS_SUSPENDED:
|
1999-11-28 05:38:13 +00:00
|
|
|
case PS_MUTEX_WAIT:
|
|
|
|
case PS_COND_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;
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->cancelflags |= THR_CANCEL_NEEDED;
|
2003-07-23 02:11:07 +00:00
|
|
|
kmbx = _thr_setrunnable_unlocked(pthread);
|
2003-10-08 00:17:13 +00:00
|
|
|
pthread->continuation =
|
|
|
|
_thr_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
|
|
|
}
|
2003-07-17 23:02:30 +00:00
|
|
|
if ((pthread->cancelflags & THR_AT_CANCEL_POINT) &&
|
|
|
|
(pthread->blocked != 0 ||
|
|
|
|
pthread->attr.flags & PTHREAD_SCOPE_SYSTEM))
|
2003-08-05 22:46:00 +00:00
|
|
|
kse_thr_interrupt(&pthread->tcb->tcb_tmbx,
|
2003-07-17 23:02:30 +00:00
|
|
|
KSE_INTR_INTERRUPT, 0);
|
1999-11-28 05:38:13 +00:00
|
|
|
}
|
1999-12-17 00:57:54 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
2003-10-08 00:30:38 +00:00
|
|
|
* Release the thread's lock and remove the
|
2003-04-18 05:04:16 +00:00
|
|
|
* reference:
|
|
|
|
*/
|
|
|
|
THR_SCHED_UNLOCK(curthread, pthread);
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(curthread, pthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
_thr_ref_delete(curthread, pthread);
|
2003-07-23 02:11:07 +00:00
|
|
|
if (kmbx != NULL)
|
|
|
|
kse_wakeup(kmbx);
|
2003-05-24 02:29:25 +00:00
|
|
|
|
|
|
|
if ((joinee != NULL) &&
|
|
|
|
(_thr_ref_add(curthread, joinee, /* include dead */1) == 0)) {
|
|
|
|
/* Remove the joiner from the joinee. */
|
|
|
|
THR_SCHED_LOCK(curthread, joinee);
|
|
|
|
joinee->joiner = NULL;
|
|
|
|
THR_SCHED_UNLOCK(curthread, joinee);
|
|
|
|
_thr_ref_delete(curthread, joinee);
|
|
|
|
}
|
1999-11-28 05:38:13 +00:00
|
|
|
}
|
|
|
|
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;
|
2003-04-18 05:04:16 +00:00
|
|
|
int need_exit = 0;
|
|
|
|
|
2003-10-08 00:30:38 +00:00
|
|
|
/* Take the thread's lock while fiddling with the state: */
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_LOCK(curthread, curthread);
|
1999-11-28 05:38:13 +00:00
|
|
|
|
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:
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags &= ~PTHREAD_CANCEL_DISABLE;
|
|
|
|
if ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)
|
2003-04-18 05:04:16 +00:00
|
|
|
need_exit = checkcancel(curthread);
|
1999-11-28 05:38:13 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_DISABLE:
|
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;
|
|
|
|
}
|
|
|
|
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
if (need_exit != 0) {
|
|
|
|
_thr_exit_cleanup();
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
PANIC("cancel");
|
|
|
|
}
|
2003-07-07 04:28:23 +00:00
|
|
|
if (ret == 0 && oldstate != NULL)
|
|
|
|
*oldstate = ostate;
|
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
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;
|
2003-04-18 05:04:16 +00:00
|
|
|
int need_exit = 0;
|
|
|
|
|
2003-10-08 00:30:38 +00:00
|
|
|
/* Take the thread's lock while fiddling with the state: */
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_LOCK(curthread, curthread);
|
1999-11-28 05:38:13 +00:00
|
|
|
|
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:
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS;
|
2003-04-18 05:04:16 +00:00
|
|
|
need_exit = checkcancel(curthread);
|
1999-11-28 05:38:13 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_DEFERRED:
|
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;
|
|
|
|
}
|
|
|
|
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
if (need_exit != 0) {
|
|
|
|
_thr_exit_cleanup();
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
PANIC("cancel");
|
|
|
|
}
|
2003-07-07 04:28:23 +00:00
|
|
|
if (ret == 0 && oldtype != NULL)
|
|
|
|
*oldtype = otype;
|
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-04-18 05:04:16 +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();
|
|
|
|
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_LOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
testcancel(curthread);
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(curthread, curthread);
|
1999-11-28 05:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-12-09 02:20:56 +00:00
|
|
|
_thr_cancel_enter(struct pthread *thread)
|
1999-11-28 05:38:13 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Look for a cancellation before we block: */
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_LOCK(thread, thread);
|
2003-04-18 05:04:16 +00:00
|
|
|
testcancel(thread);
|
|
|
|
thread->cancelflags |= THR_AT_CANCEL_POINT;
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(thread, thread);
|
2003-04-18 05:04:16 +00:00
|
|
|
}
|
2001-01-24 13:03:38 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
void
|
2003-12-09 02:20:56 +00:00
|
|
|
_thr_cancel_leave(struct pthread *thread, int check)
|
2003-04-18 05:04:16 +00:00
|
|
|
{
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_LOCK(thread, thread);
|
2003-04-18 05:04:16 +00:00
|
|
|
thread->cancelflags &= ~THR_AT_CANCEL_POINT;
|
1999-11-28 05:38:13 +00:00
|
|
|
/* Look for a cancellation after we unblock: */
|
2003-12-09 02:20:56 +00:00
|
|
|
if (check)
|
|
|
|
testcancel(thread);
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(thread, thread);
|
1999-11-28 05:38:13 +00:00
|
|
|
}
|
2000-01-19 07:04:50 +00:00
|
|
|
|
2003-10-08 00:17:13 +00:00
|
|
|
void
|
|
|
|
_thr_finish_cancellation(void *arg)
|
2000-01-19 07:04:50 +00:00
|
|
|
{
|
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
|
|
|
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_LOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((curthread->cancelflags & THR_CANCEL_NEEDED) != 0) {
|
|
|
|
curthread->cancelflags &= ~THR_CANCEL_NEEDED;
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
_thr_exit_cleanup();
|
2000-01-19 07:04:50 +00:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
2003-10-08 00:17:13 +00:00
|
|
|
THR_THREAD_UNLOCK(curthread, curthread);
|
2000-01-19 07:04:50 +00:00
|
|
|
}
|