File: pthread_cancel.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (315 lines) | stat: -rw-r--r-- 7,880 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 1996, 1998, 1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * Cancel Code.
 */
#include <threads/pthread_internal.h>
#include <threads/pthread_ipc.h>
#include <oskit/c/malloc.h>
#include "pthread_cond.h"

int
pthread_cancel(pthread_t tid)
{
	pthread_thread_t  *pthread;

	assert_preemption_enabled();
	assert_interrupts_enabled();

	if ((pthread = tidtothread(tid)) == NULL_THREADPTR)
		return EINVAL;

	if (CURPTHREAD()->tid == tid) {
		pthread_exit((void *) -1);
		return 0;
	}

	disable_preemption();
	pthread_lock(&pthread->lock);

	if (pthread->flags & (THREAD_EXITING|THREAD_CANCELED)) {
		pthread_unlock(&pthread->lock);
		enable_preemption();
		return 0;
	}
	pthread->flags |= THREAD_CANCELED;

	/*
	 * Test cancel state. If disabled it must be deferred until
	 * cancelation is enabled.
	 */
	if (pthread->cancelstate == PTHREAD_CANCEL_DISABLE) {
		pthread_unlock(&pthread->lock);
		enable_preemption();
		return 0;
	}

	/*
	 * Test cancel type. In either case (ASYNC or DEFERRED), the thread
	 * is woken is up. The difference is that ASYNC forces the thread
	 * into pthread_exit immediately upon being switched back in, while
	 * DEFERRED allows the thread to continue executing until it
	 * reaches a cancelation point.  So, pthread_testcancel just calls
	 * pthread_exit if the cancelpending is set.
	 */
	if (pthread->canceltype != PTHREAD_CANCEL_DEFERRED)
		pthread->flags |= THREAD_KILLED;

#ifdef  PRI_INHERIT
	/*
	 * If this thread is inheriting from a thread (which means a thread
	 * or threads is waiting on it), its probably a bad thing cause
	 * some resource is locked up.
	 */
	if (! queue_empty(&pthread->waiters))
		printf("PTHREAD_CANCEL: TID(%p) was canceled, but other "
		       "threads are waiting for it. DEADLOCK?\n",
		       pthread->tid);

	/*
	 * If instead this thread is donating its priority to another
	 * thread that holds a resource, just undo the inheritance.
	 */
	if (pthread->waiting_for)
		pthread_priority_kill(pthread);
#endif
	
	/*
	 * All kinds of conditions are possible at this point.
	 */

	/*
	 * Below here, we muck with the waitflags, which are accessed
	 * from interrupt handlers. Must block interrupts and take
	 * that lock first.
	 */
	disable_interrupts();
	pthread_unlock(&pthread->lock);
	pthread_lock(&pthread->waitlock);

#ifdef  CPU_INHERIT
	/*
	 * CPUI recv wait.
	 */
	if (pthread->waitflags & THREAD_WS_CPUIRECV_WAIT) {
		pthread_sched_recv_cancel(pthread);
		goto done;
	}
#endif	

	/*
	 * IPC Wait.
	 */
	if (pthread->waitflags & THREAD_WS_IPCWAIT_FLAG) {
		pthread_ipc_cancel(pthread);
		enable_interrupts();
		goto done;
	}
	
	/*
	 * If the thread is in an osenv_sleep(), issue a wakeup.
	 * The thread will be allowed to return through the sleep, to
	 * be caught sometime later. This allows driver state to be
	 * cleaned up before the thread is actually killed off.
	 */
	if (pthread->waitflags & THREAD_WS_OSENVSLEEP) {
		pthread_unlock(&pthread->waitlock);
		osenv_wakeup(pthread->sleeprec, OSENV_SLEEP_CANCELED);
		enable_interrupts();
		goto done;
	}

	/*
	 * If the thread is THREAD_WS_SLEEPING, then restart it. The death 
	 * will be noticed before the thread is allowed to return from 
	 * the call.
	 */
	if (pthread->waitflags & THREAD_WS_SLEEPING) {
		pthread_wakeup_locked(pthread);
		enable_interrupts();
		goto done;
	}

	/*
	 * If the thread is THREAD_WS_CONDWAIT, then restart it. The wrinkle
	 * is a race condition between the time the thread is taken off
	 * the condition queue and the time the thread state is changed.
	 */
	if (pthread->waitflags & THREAD_WS_CONDWAIT) {
		struct pthread_cond_impl *pimpl = pthread->waitcond->impl;

		pthread_lock(&(pimpl->lock));

		/*
		 * The thread was still on the Q, so its safe to change
		 * its state to reflect that it is not longer waiting
		 * on the condition. 
		 *
		 * If the thread was not on the Q, we caught the race,
		 * and do not have to do anything.
		 */
		if (pthread_remove_fromQ(&(pimpl->waiters), pthread)) {
			pthread->waitflags &= ~THREAD_WS_CONDWAIT;
			pthread->waitcond  = 0;
			pthread_unlock(&(pimpl->lock));
			pthread_unlock(&pthread->waitlock);
			enable_interrupts();
			pthread_sched_setrunnable(pthread);
			goto done;
		}

		pthread_unlock(&(pimpl->lock));
		pthread_unlock(&pthread->waitlock);
		enable_interrupts();
		goto done;
	}
	pthread_unlock(&pthread->waitlock);
	enable_interrupts();

	/*
	 * Must be running on another CPU. Must wait for it to be noticed.
	 */
   done:
	enable_preemption();
	return 0;
}

int
pthread_setcancelstate(int state, int *oldstate)
{
	pthread_thread_t	*pthread = CURPTHREAD();

	if (state != PTHREAD_CANCEL_ENABLE &&
	    state != PTHREAD_CANCEL_DISABLE)
		return EINVAL;

	assert_preemption_enabled();

	disable_preemption();
	pthread_lock(&pthread->lock);
	*oldstate = pthread->cancelstate;
	pthread->cancelstate = state;
	pthread_unlock(&pthread->lock);
	enable_preemption();
	
	return 0;
}

int
pthread_setcanceltype(int type, int *oldtype)
{
	pthread_thread_t	*pthread = CURPTHREAD();

	if (type != PTHREAD_CANCEL_DEFERRED &&
	    type != PTHREAD_CANCEL_ASYNCHRONOUS)
		return EINVAL;

	assert_preemption_enabled();

	disable_preemption();
	pthread_lock(&pthread->lock);
	*oldtype = pthread->canceltype;
	pthread->canceltype = type;
	pthread_unlock(&pthread->lock);
	enable_preemption();
	
	return 0;
}

void
pthread_testcancel(void)
{
	pthread_thread_t	*pthread = CURPTHREAD();

	assert_preemption_enabled();

	disable_preemption();
	pthread_lock(&pthread->lock);

	if (pthread->cancelstate == PTHREAD_CANCEL_ENABLE &&
	    pthread->flags & THREAD_CANCELED) {
		pthread_exit_locked((void *) PTHREAD_CANCELED);

		/*
		 * Never returns.
		 */
	}

	pthread_unlock(&pthread->lock);
	enable_preemption();
}

/*
 * Push a cleanup handler. This is the internal version that is called
 * from the pthread_cleanup_push macro. 
 */
void
oskit_pthread_cleanup_push(pthread_cleanup_t *pcleanup,
			   void (*routine)(void *), void *arg)
{
	pthread_thread_t	*pthread = CURPTHREAD();

	assert_preemption_enabled();

	pcleanup->func = routine;
	pcleanup->arg  = arg;

	/*
	 * Link it into the chain. This maintains a backwards chain
	 * through the stack of cleanup handlers.
	 */
	pcleanup->prev    = pthread->cleanups;
	pthread->cleanups = pcleanup;
}

/*
 * Pop a cleanup handler, possibly executing it.
 */
void
oskit_pthread_cleanup_pop(pthread_cleanup_t *pcleanup, int execute)
{
	pthread_thread_t	*pthread = CURPTHREAD();

	assert_preemption_enabled();

	if (pcleanup != pthread->cleanups) {
		pthread_lock(&pthread->lock);
		panic("oskit_pthread_cleanup_pop: Mismatch!");
	}
	
	pthread->cleanups = pcleanup->prev;

	if (execute)
		pcleanup->func(pcleanup->arg);
}

/*
 * Call the cleanup handlers. Called from pthread_exit at termination.
 */
void
pthread_call_cleanup_handlers()
{
	pthread_thread_t	*pthread = CURPTHREAD();

	/*
	 * Don't have to lock the thread for accessing the cleanups, since
	 * only the current thread can muck with them.
	 */
	while (pthread->cleanups)
		oskit_pthread_cleanup_pop(pthread->cleanups, 1);
}