summaryrefslogtreecommitdiffstats
path: root/lib/libthr/thread/thr_cancel.c
blob: a539de7a1b04eb327759db7af2df966f09ad4c74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * David Leonard <d@openbsd.org>, 1999. Public domain.
 * $FreeBSD$
 */
#include <sys/errno.h>
#include <pthread.h>
#include <stdlib.h>
#include "thr_private.h"

/*
 * Static prototypes
 */
static void	testcancel(void);

__weak_reference(_pthread_cancel, pthread_cancel);
__weak_reference(_pthread_setcancelstate, pthread_setcancelstate);
__weak_reference(_pthread_setcanceltype, pthread_setcanceltype);
__weak_reference(_pthread_testcancel, pthread_testcancel);

int
_pthread_cancel(pthread_t pthread)
{
	int ret;
	pthread_t joined;

	/*
	 * When canceling a thread that has joined another thread, this
	 * routine breaks the normal lock order of locking first the
	 * joined and then the joiner. Therefore, it is necessary that
	 * if it can't obtain the second lock, that it release the first
	 * one and restart from the top.
	 */
retry:
	if ((ret = _find_thread(pthread)) != 0)
		/* The thread is not on the list of active threads */
		goto out;

	_thread_critical_enter(pthread);

	if (pthread->state == PS_DEAD || pthread->state == PS_DEADLOCK
	    || (pthread->flags & PTHREAD_EXITING) != 0) {
		/*
		 * The thread is in the process of (or has already) exited
		 * or is deadlocked.
		 */
		_thread_critical_exit(pthread);
		ret = 0;
		goto out;
	}

	/*
	 * The thread is on the active thread list and is not in the process
	 * of exiting.
	 */

	if (((pthread->cancelflags & PTHREAD_CANCEL_DISABLE) != 0) ||
	    (((pthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) == 0) &&
	    ((pthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0)))
		/* Just mark it for cancellation: */
		pthread->cancelflags |= PTHREAD_CANCELLING;
	else {
		/*
		 * Check if we need to kick it back into the
		 * run queue:
		 */
		switch (pthread->state) {
		case PS_RUNNING:
			/* No need to resume: */
			pthread->cancelflags |= PTHREAD_CANCELLING;
			break;

		case PS_SLEEP_WAIT:
		case PS_WAIT_WAIT:
			pthread->cancelflags |= PTHREAD_CANCELLING;
			PTHREAD_NEW_STATE(pthread, PS_RUNNING);
			break;

		case PS_JOIN:
			/*
			 * Disconnect the thread from the joinee:
			 */
			if ((joined = pthread->join_status.thread) != NULL) {
				UMTX_TRYLOCK(&joined->lock, ret);
				if (ret == EBUSY) {
					_thread_critical_exit(pthread);
					goto retry;
				}
				pthread->join_status.thread->joiner = NULL;
				UMTX_UNLOCK(&joined->lock);
				joined = pthread->join_status.thread = NULL;
			}
			pthread->cancelflags |= PTHREAD_CANCELLING;
			PTHREAD_NEW_STATE(pthread, PS_RUNNING);
			break;

		case PS_BARRIER_WAIT:
		case PS_MUTEX_WAIT:
		case PS_COND_WAIT:
			/*
			 * Threads in these states may be in queues.
			 * In order to preserve queue integrity, the
			 * cancelled thread must remove itself from the
			 * queue.  When the thread resumes, it will
			 * remove itself from the queue and call the
			 * cancellation routine.
			 */
			pthread->cancelflags |= PTHREAD_CANCELLING;
			PTHREAD_NEW_STATE(pthread, PS_RUNNING);
			break;

		case PS_DEAD:
		case PS_DEADLOCK:
		case PS_STATE_MAX:
			/* Ignore - only here to silence -Wall: */
			break;
		}
	}

	/* Unprotect the scheduling queues: */
	_thread_critical_exit(pthread);

	ret = 0;
out:
	return (ret);
}

int
_pthread_setcancelstate(int state, int *oldstate)
{
	int ostate, ret;

	ret = 0;

	_thread_critical_enter(curthread);

	ostate = curthread->cancelflags & PTHREAD_CANCEL_DISABLE;

	switch (state) {
	case PTHREAD_CANCEL_ENABLE:
		if (oldstate != NULL)
			*oldstate = ostate;
		curthread->cancelflags &= ~PTHREAD_CANCEL_DISABLE;
		if ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) == 0)
			break;
		testcancel();
		break;
	case PTHREAD_CANCEL_DISABLE:
		if (oldstate != NULL)
			*oldstate = ostate;
		curthread->cancelflags |= PTHREAD_CANCEL_DISABLE;
		break;
	default:
		ret = EINVAL;
	}

	_thread_critical_exit(curthread);
	return (ret);
}

int
_pthread_setcanceltype(int type, int *oldtype)
{
	int otype;

	_thread_critical_enter(curthread);
	otype = curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS;
	switch (type) {
	case PTHREAD_CANCEL_ASYNCHRONOUS:
		if (oldtype != NULL)
			*oldtype = otype;
		curthread->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS;
		testcancel();
		break;
	case PTHREAD_CANCEL_DEFERRED:
		if (oldtype != NULL)
			*oldtype = otype;
		curthread->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS;
		break;
	default:
		return (EINVAL);
	}

	_thread_critical_exit(curthread);
	return (0);
}

void
_pthread_testcancel(void)
{
	_thread_critical_enter(curthread);
	testcancel();
	_thread_critical_exit(curthread);
}

static void
testcancel()
{
	/*
	 * This pthread should already be locked by the caller.
	 */

	if (((curthread->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) &&
	    ((curthread->cancelflags & PTHREAD_CANCELLING) != 0) &&
	    ((curthread->flags & PTHREAD_EXITING) == 0)) {
		/*
		 * It is possible for this thread to be swapped out
		 * while performing cancellation; do not allow it
		 * to be cancelled again.
		 */
		curthread->cancelflags &= ~PTHREAD_CANCELLING;
		_thread_critical_exit(curthread);
		_thread_exit_cleanup();
		pthread_exit(PTHREAD_CANCELED);
		PANIC("cancel");
	}
}

void
_thread_enter_cancellation_point(void)
{
	_thread_critical_enter(curthread);
	testcancel();
	curthread->cancelflags |= PTHREAD_AT_CANCEL_POINT;
	_thread_critical_exit(curthread);
}

void
_thread_leave_cancellation_point(void)
{
	_thread_critical_enter(curthread);
	curthread->cancelflags &= ~PTHREAD_AT_CANCEL_POINT;
	testcancel();
	_thread_critical_exit(curthread);

}
OpenPOWER on IntegriCloud