2003-04-01 05:46:29 +02:00
|
|
|
/*
|
2005-04-02 03:20:00 +02:00
|
|
|
* Copyright (c) 2005, David Xu <davidxu@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2003-04-01 05:46:29 +02:00
|
|
|
* $FreeBSD$
|
2005-04-02 03:20:00 +02:00
|
|
|
*
|
2003-04-01 05:46:29 +02:00
|
|
|
*/
|
2005-04-02 03:20:00 +02:00
|
|
|
|
2006-04-04 04:57:49 +02:00
|
|
|
#include "namespace.h"
|
2003-04-01 05:46:29 +02:00
|
|
|
#include <pthread.h>
|
2006-04-04 04:57:49 +02:00
|
|
|
#include "un-namespace.h"
|
2003-04-01 05:46:29 +02:00
|
|
|
|
2005-04-02 03:20:00 +02:00
|
|
|
#include "thr_private.h"
|
2003-05-15 19:56:18 +02:00
|
|
|
|
2003-04-01 05:46:29 +02: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);
|
|
|
|
|
2006-11-24 10:57:38 +01:00
|
|
|
static inline void
|
|
|
|
testcancel(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
if (__predict_false(SHOULD_CANCEL(curthread) &&
|
2006-12-04 15:20:41 +01:00
|
|
|
!THR_IN_CRITICAL(curthread) && curthread->cancel_defer == 0))
|
2006-11-24 10:57:38 +01:00
|
|
|
_pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_testcancel(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
testcancel(curthread);
|
|
|
|
}
|
|
|
|
|
2003-04-01 05:46:29 +02:00
|
|
|
int
|
|
|
|
_pthread_cancel(pthread_t pthread)
|
|
|
|
{
|
2005-04-02 03:20:00 +02:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
int ret;
|
2003-04-01 05:46:29 +02:00
|
|
|
|
2003-05-25 10:48:11 +02:00
|
|
|
/*
|
2006-11-24 10:57:38 +01:00
|
|
|
* POSIX says _pthread_cancel should be async cancellation safe.
|
|
|
|
* _thr_ref_add and _thr_ref_delete will enter and leave critical
|
|
|
|
* region automatically.
|
2003-05-25 10:48:11 +02:00
|
|
|
*/
|
2006-11-24 10:57:38 +01:00
|
|
|
if ((ret = _thr_ref_add(curthread, pthread, 0)) == 0) {
|
|
|
|
THR_THREAD_LOCK(curthread, pthread);
|
|
|
|
if (!pthread->cancel_pending) {
|
|
|
|
pthread->cancel_pending = 1;
|
|
|
|
if (pthread->cancel_enable)
|
|
|
|
_thr_send_sig(pthread, SIGCANCEL);
|
|
|
|
}
|
|
|
|
THR_THREAD_UNLOCK(curthread, pthread);
|
|
|
|
_thr_ref_delete(curthread, pthread);
|
2005-04-02 03:20:00 +02:00
|
|
|
}
|
2006-11-24 10:57:38 +01:00
|
|
|
return (ret);
|
2005-04-02 03:20:00 +02:00
|
|
|
}
|
|
|
|
|
2003-04-01 05:46:29 +02:00
|
|
|
int
|
|
|
|
_pthread_setcancelstate(int state, int *oldstate)
|
|
|
|
{
|
2005-04-02 03:20:00 +02:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2006-11-24 10:57:38 +01:00
|
|
|
int oldval;
|
2003-04-01 05:46:29 +02:00
|
|
|
|
2006-11-24 10:57:38 +01:00
|
|
|
oldval = curthread->cancel_enable;
|
2003-04-01 05:46:29 +02:00
|
|
|
switch (state) {
|
|
|
|
case PTHREAD_CANCEL_DISABLE:
|
2006-11-24 10:57:38 +01:00
|
|
|
THR_LOCK(curthread);
|
|
|
|
curthread->cancel_enable = 0;
|
|
|
|
THR_UNLOCK(curthread);
|
2005-04-02 03:20:00 +02:00
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_ENABLE:
|
2006-11-24 10:57:38 +01:00
|
|
|
THR_LOCK(curthread);
|
|
|
|
curthread->cancel_enable = 1;
|
|
|
|
THR_UNLOCK(curthread);
|
2003-04-01 05:46:29 +02:00
|
|
|
break;
|
|
|
|
default:
|
2006-11-24 10:57:38 +01:00
|
|
|
return (EINVAL);
|
2003-04-01 05:46:29 +02:00
|
|
|
}
|
2005-04-02 03:20:00 +02:00
|
|
|
|
2006-11-24 10:57:38 +01:00
|
|
|
if (oldstate) {
|
|
|
|
*oldstate = oldval ? PTHREAD_CANCEL_ENABLE :
|
|
|
|
PTHREAD_CANCEL_DISABLE;
|
|
|
|
}
|
|
|
|
return (0);
|
2003-04-01 05:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_setcanceltype(int type, int *oldtype)
|
|
|
|
{
|
2005-04-02 03:20:00 +02:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2006-11-24 10:57:38 +01:00
|
|
|
int oldval;
|
2003-04-01 05:46:29 +02:00
|
|
|
|
2006-11-24 10:57:38 +01:00
|
|
|
oldval = curthread->cancel_async;
|
2003-04-01 05:46:29 +02:00
|
|
|
switch (type) {
|
|
|
|
case PTHREAD_CANCEL_ASYNCHRONOUS:
|
2006-11-24 10:57:38 +01:00
|
|
|
curthread->cancel_async = 1;
|
2005-04-02 03:20:00 +02:00
|
|
|
testcancel(curthread);
|
2003-04-01 05:46:29 +02:00
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_DEFERRED:
|
2006-11-24 10:57:38 +01:00
|
|
|
curthread->cancel_async = 0;
|
2003-04-01 05:46:29 +02:00
|
|
|
break;
|
|
|
|
default:
|
2006-11-24 10:57:38 +01:00
|
|
|
return (EINVAL);
|
2003-04-01 05:46:29 +02:00
|
|
|
}
|
2005-04-02 03:20:00 +02:00
|
|
|
|
2006-11-24 10:57:38 +01:00
|
|
|
if (oldtype) {
|
|
|
|
*oldtype = oldval ? PTHREAD_CANCEL_ASYNCHRONOUS :
|
|
|
|
PTHREAD_CANCEL_DEFERRED;
|
|
|
|
}
|
|
|
|
return (0);
|
2003-04-01 05:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_pthread_testcancel(void)
|
|
|
|
{
|
2006-11-24 10:57:38 +01:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
|
|
|
|
_thr_cancel_enter(curthread);
|
|
|
|
_thr_cancel_leave(curthread);
|
2003-05-15 19:56:18 +02:00
|
|
|
}
|
|
|
|
|
2006-11-24 10:57:38 +01:00
|
|
|
void
|
2005-04-02 03:20:00 +02:00
|
|
|
_thr_cancel_enter(struct pthread *curthread)
|
2003-05-15 19:56:18 +02:00
|
|
|
{
|
2006-11-24 10:57:38 +01:00
|
|
|
if (curthread->cancel_enable) {
|
|
|
|
curthread->cancel_point++;
|
2005-04-02 03:20:00 +02:00
|
|
|
testcancel(curthread);
|
|
|
|
}
|
2003-04-01 05:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-11-24 10:57:38 +01:00
|
|
|
_thr_cancel_leave(struct pthread *curthread)
|
2003-04-01 05:46:29 +02:00
|
|
|
{
|
2006-11-24 10:57:38 +01:00
|
|
|
if (curthread->cancel_enable)
|
|
|
|
curthread->cancel_point--;
|
2003-04-01 05:46:29 +02:00
|
|
|
}
|
2006-12-04 15:20:41 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
_thr_cancel_enter_defer(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
if (curthread->cancel_enable) {
|
|
|
|
curthread->cancel_point++;
|
|
|
|
testcancel(curthread);
|
|
|
|
curthread->cancel_defer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_cancel_leave_defer(struct pthread *curthread, int check)
|
|
|
|
{
|
|
|
|
if (curthread->cancel_enable) {
|
2006-12-05 06:01:57 +01:00
|
|
|
if (!check) {
|
|
|
|
curthread->cancel_point--;
|
|
|
|
curthread->cancel_defer--;
|
|
|
|
} else {
|
|
|
|
curthread->cancel_defer--;
|
2006-12-04 15:20:41 +01:00
|
|
|
testcancel(curthread);
|
2006-12-05 06:01:57 +01:00
|
|
|
curthread->cancel_point--;
|
|
|
|
}
|
2006-12-04 15:20:41 +01:00
|
|
|
}
|
|
|
|
}
|