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  * $DragonFly: src/lib/libthread_xu/thread/thr_cancel.c,v 1.2 2005/03/29 19:26:20 joerg Exp $
27  */
28 
29 #include <machine/tls.h>
30 
31 #include <pthread.h>
32 #include "thr_private.h"
33 
34 __weak_reference(_pthread_cancel, pthread_cancel);
35 __weak_reference(_pthread_setcancelstate, pthread_setcancelstate);
36 __weak_reference(_pthread_setcanceltype, pthread_setcanceltype);
37 __weak_reference(_pthread_testcancel, pthread_testcancel);
38 
39 int _pthread_setcanceltype(int type, int *oldtype);
40 
41 int
42 _pthread_cancel(pthread_t pthread)
43 {
44 	struct pthread *curthread = tls_get_curthread();
45 	int oldval, newval = 0;
46 	int oldtype;
47 	int ret;
48 
49 	/*
50 	 * POSIX says _pthread_cancel should be async cancellation safe,
51 	 * so we temporarily disable async cancellation.
52 	 */
53 	_pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
54 	if ((ret = _thr_ref_add(curthread, pthread, 0)) != 0) {
55 		_pthread_setcanceltype(oldtype, NULL);
56 		return (ret);
57 	}
58 
59 	do {
60 		oldval = pthread->cancelflags;
61 		if (oldval & THR_CANCEL_NEEDED)
62 			break;
63 		newval = oldval | THR_CANCEL_NEEDED;
64 	} while (!atomic_cmpset_acq_int(&pthread->cancelflags, oldval, newval));
65 
66 	if (!(oldval & THR_CANCEL_NEEDED) && SHOULD_ASYNC_CANCEL(newval))
67 		_thr_send_sig(pthread, SIGCANCEL);
68 
69 	_thr_ref_delete(curthread, pthread);
70 	_pthread_setcanceltype(oldtype, NULL);
71 	return (0);
72 }
73 
74 static inline void
75 testcancel(struct pthread *curthread)
76 {
77 	int newval;
78 
79 	newval = curthread->cancelflags;
80 	if (SHOULD_CANCEL(newval))
81 		pthread_exit(PTHREAD_CANCELED);
82 }
83 
84 int
85 _pthread_setcancelstate(int state, int *oldstate)
86 {
87 	struct pthread *curthread = tls_get_curthread();
88 	int oldval, ret;
89 
90 	oldval = curthread->cancelflags;
91 	if (oldstate != NULL)
92 		*oldstate = ((oldval & THR_CANCEL_DISABLE) ?
93 		    PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE);
94 	switch (state) {
95 	case PTHREAD_CANCEL_DISABLE:
96 		atomic_set_int(&curthread->cancelflags, THR_CANCEL_DISABLE);
97 		ret = 0;
98 		break;
99 	case PTHREAD_CANCEL_ENABLE:
100 		atomic_clear_int(&curthread->cancelflags, THR_CANCEL_DISABLE);
101 		testcancel(curthread);
102 		ret = 0;
103 		break;
104 	default:
105 		ret = EINVAL;
106 	}
107 
108 	return (ret);
109 }
110 
111 int
112 _pthread_setcanceltype(int type, int *oldtype)
113 {
114 	struct pthread	*curthread = tls_get_curthread();
115 	int oldval, ret;
116 
117 	oldval = curthread->cancelflags;
118 	if (oldtype != NULL)
119 		*oldtype = ((oldval & THR_CANCEL_AT_POINT) ?
120 				 PTHREAD_CANCEL_ASYNCHRONOUS :
121 				 PTHREAD_CANCEL_DEFERRED);
122 	switch (type) {
123 	case PTHREAD_CANCEL_ASYNCHRONOUS:
124 		atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
125 		testcancel(curthread);
126 		ret = 0;
127 		break;
128 	case PTHREAD_CANCEL_DEFERRED:
129 		atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
130 		ret = 0;
131 		break;
132 	default:
133 		ret = EINVAL;
134 	}
135 
136 	return (ret);
137 }
138 
139 void
140 _pthread_testcancel(void)
141 {
142 	testcancel(tls_get_curthread());
143 }
144 
145 int
146 _thr_cancel_enter(struct pthread *curthread)
147 {
148 	int oldval;
149 
150 	oldval = curthread->cancelflags;
151 	if (!(oldval & THR_CANCEL_AT_POINT)) {
152 		atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
153 		testcancel(curthread);
154 	}
155 	return (oldval);
156 }
157 
158 void
159 _thr_cancel_leave(struct pthread *curthread, int previous)
160 {
161 	if (!(previous & THR_CANCEL_AT_POINT))
162 		atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT);
163 }
164