1 /*
2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
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.
14 *
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.
25 *
26 */
27
28 #include "namespace.h"
29 #include <machine/tls.h>
30 #include <pthread.h>
31 #include "un-namespace.h"
32
33 #include "thr_private.h"
34
35 int
_pthread_cancel(pthread_t pthread)36 _pthread_cancel(pthread_t pthread)
37 {
38 pthread_t curthread = tls_get_curthread();
39 int oldval, newval = 0;
40 int oldtype;
41 int ret;
42
43 /*
44 * POSIX says _pthread_cancel should be async cancellation safe,
45 * so we temporarily disable async cancellation.
46 */
47 _pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
48 if ((ret = _thr_ref_add(curthread, pthread, 0)) != 0) {
49 _pthread_setcanceltype(oldtype, NULL);
50 return (ret);
51 }
52
53 do {
54 oldval = pthread->cancelflags;
55 if (oldval & THR_CANCEL_NEEDED)
56 break;
57 newval = oldval | THR_CANCEL_NEEDED;
58 } while (!atomic_cmpset_acq_int(&pthread->cancelflags, oldval, newval));
59
60 if (!(oldval & THR_CANCEL_NEEDED) && SHOULD_ASYNC_CANCEL(newval))
61 _thr_send_sig(pthread, SIGCANCEL);
62
63 _thr_ref_delete(curthread, pthread);
64 _pthread_setcanceltype(oldtype, NULL);
65 return (0);
66 }
67
68 static inline void
testcancel(pthread_t curthread)69 testcancel(pthread_t curthread)
70 {
71 int newval;
72
73 newval = curthread->cancelflags;
74 if (SHOULD_CANCEL(newval))
75 _pthread_exit(PTHREAD_CANCELED);
76 }
77
78 int
_pthread_setcancelstate(int state,int * oldstate)79 _pthread_setcancelstate(int state, int *oldstate)
80 {
81 pthread_t curthread = tls_get_curthread();
82 int oldval;
83
84 oldval = curthread->cancelflags;
85 if (oldstate != NULL)
86 *oldstate = ((oldval & THR_CANCEL_DISABLE) ?
87 PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE);
88 switch (state) {
89 case PTHREAD_CANCEL_DISABLE:
90 atomic_set_int(&curthread->cancelflags, THR_CANCEL_DISABLE);
91 break;
92 case PTHREAD_CANCEL_ENABLE:
93 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_DISABLE);
94 testcancel(curthread);
95 break;
96 default:
97 return (EINVAL);
98 }
99
100 return (0);
101 }
102
103 int
_pthread_setcanceltype(int type,int * oldtype)104 _pthread_setcanceltype(int type, int *oldtype)
105 {
106 pthread_t curthread = tls_get_curthread();
107 int oldval;
108
109 oldval = curthread->cancelflags;
110 if (oldtype != NULL)
111 *oldtype = ((oldval & THR_CANCEL_AT_POINT) ?
112 PTHREAD_CANCEL_ASYNCHRONOUS :
113 PTHREAD_CANCEL_DEFERRED);
114 switch (type) {
115 case PTHREAD_CANCEL_ASYNCHRONOUS:
116 atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
117 testcancel(curthread);
118 break;
119 case PTHREAD_CANCEL_DEFERRED:
120 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
121 break;
122 default:
123 return (EINVAL);
124 }
125
126 return (0);
127 }
128
129 void
_pthread_testcancel(void)130 _pthread_testcancel(void)
131 {
132 testcancel(tls_get_curthread());
133 }
134
135 int
_thr_cancel_enter(pthread_t curthread)136 _thr_cancel_enter(pthread_t curthread)
137 {
138 int oldval;
139
140 oldval = curthread->cancelflags;
141 if (!(oldval & THR_CANCEL_AT_POINT)) {
142 atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
143 testcancel(curthread);
144 }
145 return (oldval);
146 }
147
148 void
_thr_cancel_leave(pthread_t curthread,int previous)149 _thr_cancel_leave(pthread_t curthread, int previous)
150 {
151 if (!(previous & THR_CANCEL_AT_POINT))
152 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
153 }
154
155 __strong_reference(_pthread_cancel, pthread_cancel);
156 __strong_reference(_pthread_setcancelstate, pthread_setcancelstate);
157 __strong_reference(_pthread_setcanceltype, pthread_setcanceltype);
158 __strong_reference(_pthread_testcancel, pthread_testcancel);
159