2003-04-01 03:46:29 +00:00
|
|
|
/*
|
2005-04-02 01:20:00 +00: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 03:46:29 +00:00
|
|
|
* $FreeBSD$
|
2005-04-02 01:20:00 +00:00
|
|
|
*
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-04-04 02:57:49 +00:00
|
|
|
#include "namespace.h"
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <pthread.h>
|
2006-04-04 02:57:49 +00:00
|
|
|
#include "un-namespace.h"
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
#include "thr_private.h"
|
2003-05-15 17:56:18 +00:00
|
|
|
|
2003-04-01 03:46:29 +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);
|
|
|
|
|
2006-11-24 09:57:38 +00:00
|
|
|
static inline void
|
|
|
|
testcancel(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
if (__predict_false(SHOULD_CANCEL(curthread) &&
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
!THR_IN_CRITICAL(curthread)))
|
2006-11-24 09:57:38 +00:00
|
|
|
_pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_testcancel(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
testcancel(curthread);
|
|
|
|
}
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
int
|
|
|
|
_pthread_cancel(pthread_t pthread)
|
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
int ret;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2003-05-25 08:48:11 +00:00
|
|
|
/*
|
2006-11-24 09:57:38 +00:00
|
|
|
* POSIX says _pthread_cancel should be async cancellation safe.
|
2010-09-13 07:03:01 +00:00
|
|
|
* _thr_find_thread and THR_THREAD_UNLOCK will enter and leave critical
|
2006-11-24 09:57:38 +00:00
|
|
|
* region automatically.
|
2003-05-25 08:48:11 +00:00
|
|
|
*/
|
2010-09-13 07:03:01 +00:00
|
|
|
if ((ret = _thr_find_thread(curthread, pthread, 0)) == 0) {
|
2006-11-24 09:57:38 +00:00
|
|
|
if (!pthread->cancel_pending) {
|
|
|
|
pthread->cancel_pending = 1;
|
2010-09-08 02:18:20 +00:00
|
|
|
if (pthread->state != PS_DEAD)
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_send_sig(pthread, SIGCANCEL);
|
|
|
|
}
|
|
|
|
THR_THREAD_UNLOCK(curthread, pthread);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
2006-11-24 09:57:38 +00:00
|
|
|
return (ret);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
int
|
|
|
|
_pthread_setcancelstate(int state, int *oldstate)
|
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2006-11-24 09:57:38 +00:00
|
|
|
int oldval;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2006-11-24 09:57:38 +00:00
|
|
|
oldval = curthread->cancel_enable;
|
2003-04-01 03:46:29 +00:00
|
|
|
switch (state) {
|
|
|
|
case PTHREAD_CANCEL_DISABLE:
|
2006-11-24 09:57:38 +00:00
|
|
|
curthread->cancel_enable = 0;
|
2005-04-02 01:20:00 +00:00
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_ENABLE:
|
2006-11-24 09:57:38 +00:00
|
|
|
curthread->cancel_enable = 1;
|
2013-06-19 04:47:41 +00:00
|
|
|
if (curthread->cancel_async)
|
|
|
|
testcancel(curthread);
|
2003-04-01 03:46:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-11-24 09:57:38 +00:00
|
|
|
return (EINVAL);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-11-24 09:57:38 +00:00
|
|
|
if (oldstate) {
|
|
|
|
*oldstate = oldval ? PTHREAD_CANCEL_ENABLE :
|
|
|
|
PTHREAD_CANCEL_DISABLE;
|
|
|
|
}
|
|
|
|
return (0);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_setcanceltype(int type, int *oldtype)
|
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2006-11-24 09:57:38 +00:00
|
|
|
int oldval;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2006-11-24 09:57:38 +00:00
|
|
|
oldval = curthread->cancel_async;
|
2003-04-01 03:46:29 +00:00
|
|
|
switch (type) {
|
|
|
|
case PTHREAD_CANCEL_ASYNCHRONOUS:
|
2006-11-24 09:57:38 +00:00
|
|
|
curthread->cancel_async = 1;
|
2005-04-02 01:20:00 +00:00
|
|
|
testcancel(curthread);
|
2003-04-01 03:46:29 +00:00
|
|
|
break;
|
|
|
|
case PTHREAD_CANCEL_DEFERRED:
|
2006-11-24 09:57:38 +00:00
|
|
|
curthread->cancel_async = 0;
|
2003-04-01 03:46:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-11-24 09:57:38 +00:00
|
|
|
return (EINVAL);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-11-24 09:57:38 +00:00
|
|
|
if (oldtype) {
|
|
|
|
*oldtype = oldval ? PTHREAD_CANCEL_ASYNCHRONOUS :
|
|
|
|
PTHREAD_CANCEL_DEFERRED;
|
|
|
|
}
|
|
|
|
return (0);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_pthread_testcancel(void)
|
|
|
|
{
|
2006-11-24 09:57:38 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
|
Add signal handler wrapper, the reason to add it becauses there are
some cases we want to improve:
1) if a thread signal got a signal while in cancellation point,
it is possible the TDP_WAKEUP may be eaten by signal handler
if the handler called some interruptibly system calls.
2) In signal handler, we want to disable cancellation.
3) When thread holding some low level locks, it is better to
disable signal, those code need not to worry reentrancy,
sigprocmask system call is avoided because it is a bit expensive.
The signal handler wrapper works in this way:
1) libthr installs its signal handler if user code invokes sigaction
to install its handler, the user handler is recorded in internal
array.
2) when a signal is delivered, libthr's signal handler is invoke,
libthr checks if thread holds some low level lock or is in critical
region, if it is true, the signal is buffered, and all signals are
masked, once the thread leaves critical region, correct signal
mask is restored and buffered signal is processed.
3) before user signal handler is invoked, cancellation is temporarily
disabled, after user signal handler is returned, cancellation state
is restored, and pending cancellation is rescheduled.
2010-09-01 02:18:33 +00:00
|
|
|
testcancel(curthread);
|
2003-05-15 17:56:18 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 09:57:38 +00:00
|
|
|
void
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_cancel_enter(struct pthread *curthread)
|
2003-05-15 17:56:18 +00:00
|
|
|
{
|
Add signal handler wrapper, the reason to add it becauses there are
some cases we want to improve:
1) if a thread signal got a signal while in cancellation point,
it is possible the TDP_WAKEUP may be eaten by signal handler
if the handler called some interruptibly system calls.
2) In signal handler, we want to disable cancellation.
3) When thread holding some low level locks, it is better to
disable signal, those code need not to worry reentrancy,
sigprocmask system call is avoided because it is a bit expensive.
The signal handler wrapper works in this way:
1) libthr installs its signal handler if user code invokes sigaction
to install its handler, the user handler is recorded in internal
array.
2) when a signal is delivered, libthr's signal handler is invoke,
libthr checks if thread holds some low level lock or is in critical
region, if it is true, the signal is buffered, and all signals are
masked, once the thread leaves critical region, correct signal
mask is restored and buffered signal is processed.
3) before user signal handler is invoked, cancellation is temporarily
disabled, after user signal handler is returned, cancellation state
is restored, and pending cancellation is rescheduled.
2010-09-01 02:18:33 +00:00
|
|
|
curthread->cancel_point = 1;
|
|
|
|
testcancel(curthread);
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
Add signal handler wrapper, the reason to add it becauses there are
some cases we want to improve:
1) if a thread signal got a signal while in cancellation point,
it is possible the TDP_WAKEUP may be eaten by signal handler
if the handler called some interruptibly system calls.
2) In signal handler, we want to disable cancellation.
3) When thread holding some low level locks, it is better to
disable signal, those code need not to worry reentrancy,
sigprocmask system call is avoided because it is a bit expensive.
The signal handler wrapper works in this way:
1) libthr installs its signal handler if user code invokes sigaction
to install its handler, the user handler is recorded in internal
array.
2) when a signal is delivered, libthr's signal handler is invoke,
libthr checks if thread holds some low level lock or is in critical
region, if it is true, the signal is buffered, and all signals are
masked, once the thread leaves critical region, correct signal
mask is restored and buffered signal is processed.
3) before user signal handler is invoked, cancellation is temporarily
disabled, after user signal handler is returned, cancellation state
is restored, and pending cancellation is rescheduled.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_cancel_enter2(struct pthread *curthread, int maycancel)
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
{
|
Add signal handler wrapper, the reason to add it becauses there are
some cases we want to improve:
1) if a thread signal got a signal while in cancellation point,
it is possible the TDP_WAKEUP may be eaten by signal handler
if the handler called some interruptibly system calls.
2) In signal handler, we want to disable cancellation.
3) When thread holding some low level locks, it is better to
disable signal, those code need not to worry reentrancy,
sigprocmask system call is avoided because it is a bit expensive.
The signal handler wrapper works in this way:
1) libthr installs its signal handler if user code invokes sigaction
to install its handler, the user handler is recorded in internal
array.
2) when a signal is delivered, libthr's signal handler is invoke,
libthr checks if thread holds some low level lock or is in critical
region, if it is true, the signal is buffered, and all signals are
masked, once the thread leaves critical region, correct signal
mask is restored and buffered signal is processed.
3) before user signal handler is invoked, cancellation is temporarily
disabled, after user signal handler is returned, cancellation state
is restored, and pending cancellation is rescheduled.
2010-09-01 02:18:33 +00:00
|
|
|
curthread->cancel_point = 1;
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
if (__predict_false(SHOULD_CANCEL(curthread) &&
|
|
|
|
!THR_IN_CRITICAL(curthread))) {
|
|
|
|
if (!maycancel)
|
|
|
|
thr_wake(curthread->tid);
|
|
|
|
else
|
|
|
|
_pthread_exit(PTHREAD_CANCELED);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
Add signal handler wrapper, the reason to add it becauses there are
some cases we want to improve:
1) if a thread signal got a signal while in cancellation point,
it is possible the TDP_WAKEUP may be eaten by signal handler
if the handler called some interruptibly system calls.
2) In signal handler, we want to disable cancellation.
3) When thread holding some low level locks, it is better to
disable signal, those code need not to worry reentrancy,
sigprocmask system call is avoided because it is a bit expensive.
The signal handler wrapper works in this way:
1) libthr installs its signal handler if user code invokes sigaction
to install its handler, the user handler is recorded in internal
array.
2) when a signal is delivered, libthr's signal handler is invoke,
libthr checks if thread holds some low level lock or is in critical
region, if it is true, the signal is buffered, and all signals are
masked, once the thread leaves critical region, correct signal
mask is restored and buffered signal is processed.
3) before user signal handler is invoked, cancellation is temporarily
disabled, after user signal handler is returned, cancellation state
is restored, and pending cancellation is rescheduled.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_cancel_leave(struct pthread *curthread, int maycancel)
|
2006-12-04 14:20:41 +00:00
|
|
|
{
|
2010-09-24 07:52:07 +00:00
|
|
|
curthread->cancel_point = 0;
|
2010-09-24 13:01:01 +00:00
|
|
|
if (__predict_false(SHOULD_CANCEL(curthread) &&
|
|
|
|
!THR_IN_CRITICAL(curthread) && maycancel))
|
|
|
|
_pthread_exit(PTHREAD_CANCELED);
|
2006-12-04 14:20:41 +00:00
|
|
|
}
|
2010-09-25 01:57:47 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
_pthread_cancel_enter(int maycancel)
|
|
|
|
{
|
|
|
|
_thr_cancel_enter2(_get_curthread(), maycancel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_pthread_cancel_leave(int maycancel)
|
|
|
|
{
|
|
|
|
_thr_cancel_leave(_get_curthread(), maycancel);
|
|
|
|
}
|