2 * Copyright (c) 2005, David Xu<davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $DragonFly: src/lib/libthread_xu/thread/thr_cancel.c,v 1.1 2005/02/01 12:38:27 davidxu Exp $
30 #include "thr_private.h"
32 __weak_reference(_pthread_cancel, pthread_cancel);
33 __weak_reference(_pthread_setcancelstate, pthread_setcancelstate);
34 __weak_reference(_pthread_setcanceltype, pthread_setcanceltype);
35 __weak_reference(_pthread_testcancel, pthread_testcancel);
37 int _pthread_setcanceltype(int type, int *oldtype);
40 _pthread_cancel(pthread_t pthread)
42 struct pthread *curthread = _get_curthread();
43 int oldval, newval = 0;
48 * POSIX says _pthread_cancel should be async cancellation safe,
49 * so we temporarily disable async cancellation.
51 _pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
52 if ((ret = _thr_ref_add(curthread, pthread, 0)) != 0) {
53 _pthread_setcanceltype(oldtype, NULL);
58 oldval = pthread->cancelflags;
59 if (oldval & THR_CANCEL_NEEDED)
61 newval = oldval | THR_CANCEL_NEEDED;
62 } while (!atomic_cmpset_acq_int(&pthread->cancelflags, oldval, newval));
64 if (!(oldval & THR_CANCEL_NEEDED) && SHOULD_ASYNC_CANCEL(newval))
65 _thr_send_sig(pthread, SIGCANCEL);
67 _thr_ref_delete(curthread, pthread);
68 _pthread_setcanceltype(oldtype, NULL);
73 testcancel(struct pthread *curthread)
77 newval = curthread->cancelflags;
78 if (SHOULD_CANCEL(newval))
79 pthread_exit(PTHREAD_CANCELED);
83 _pthread_setcancelstate(int state, int *oldstate)
85 struct pthread *curthread = _get_curthread();
88 oldval = curthread->cancelflags;
90 *oldstate = ((oldval & THR_CANCEL_DISABLE) ?
91 PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE);
93 case PTHREAD_CANCEL_DISABLE:
94 atomic_set_int(&curthread->cancelflags, THR_CANCEL_DISABLE);
97 case PTHREAD_CANCEL_ENABLE:
98 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_DISABLE);
99 testcancel(curthread);
110 _pthread_setcanceltype(int type, int *oldtype)
112 struct pthread *curthread = _get_curthread();
115 oldval = curthread->cancelflags;
117 *oldtype = ((oldval & THR_CANCEL_AT_POINT) ?
118 PTHREAD_CANCEL_ASYNCHRONOUS :
119 PTHREAD_CANCEL_DEFERRED);
121 case PTHREAD_CANCEL_ASYNCHRONOUS:
122 atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
123 testcancel(curthread);
126 case PTHREAD_CANCEL_DEFERRED:
127 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
138 _pthread_testcancel(void)
140 testcancel(_get_curthread());
144 _thr_cancel_enter(struct pthread *curthread)
148 oldval = curthread->cancelflags;
149 if (!(oldval & THR_CANCEL_AT_POINT)) {
150 atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
151 testcancel(curthread);
157 _thr_cancel_leave(struct pthread *curthread, int previous)
159 if (!(previous & THR_CANCEL_AT_POINT))
160 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);