File: pthread_exit.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 (190 lines) | stat: -rw-r--r-- 4,897 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
/*
 * 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.
 */

/*
 * Exit code.
 */
#include <threads/pthread_internal.h>
#include "pthread_mutex.h"

static pthread_thread_t *last_dead_thread;
static pthread_lock_t	reaper_lock;

/*
 * Just a dummy lock to satisfy the switch code.
 */
pthread_lock_t		threads_exit_lock    = PTHREAD_LOCK_INITIALIZER;

extern int		threads_alive;

void
pthread_exit(void *status)
{
	pthread_thread_t  *pthread = CURPTHREAD();

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

	pthread_exit_locked(status);

	/*
	 * Never returns.
	 */
}

/*
 * Internal version. Thread lock is locked.
 */
void
pthread_exit_locked(void *status)
{
	pthread_thread_t  *pthread = CURPTHREAD();
	int		  detached;

	DPRINTF("(%d): %p(%p) %p\n", THISCPU, pthread, pthread->tid, status);

	if (pthread->flags & THREAD_EXITING) {
		panic("pthread_exit: Recursive call: 0x%x(%p)\n",
		      (int) pthread, pthread->tid);
	}

	detached         = pthread->flags & THREAD_DETACHED;
	pthread->flags  |= THREAD_EXITING;
	pthread->exitval = (oskit_u32_t) status;
	pthread_unlock(&pthread->lock);

	/*
	 * Enable preemption and interrupts while the cleanups and
	 * destructors are run, in case they misbehave.
	 */
	enable_interrupts();
	enable_preemption();

	/*
	 * Call the cancelation handlers.
	 */
	pthread_call_cleanup_handlers();
		
	/*
	 * Call the key destructors.
	 */
	pthread_call_key_destructors();

#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_EXIT: TID(%p) is exiting, but other "
		       "threads are waiting for it. DEADLOCK?\n",
		       pthread->tid);
#endif
	if (--threads_alive == 0)
		exit(0);

	/*
	 * Let the idle thread clean up the thread if its detached since
	 * we cannot reap ourselves. Note that the detached flag was
	 * checked above, and then the lock released. This is okay since
	 * the program needs to arrange its own synchronization between
	 * thread creation and detach/join.
	 */
	disable_preemption();
	if (detached) {
		pthread_lock(&reaper_lock);
		while (last_dead_thread) {
			pthread_thread_t	*dead_thread;

			/*
			 * Clear last_dead_thread and release the reaper
			 * lock so that it will not be held during the
			 * thread destruction (memory deallocation might
			 * block). The idle thread checks last_dead_thread
			 * too, so it must be cleared before the lock is
			 * released.
			 */
			dead_thread      = last_dead_thread;
			last_dead_thread = NULL_THREADPTR;
			pthread_unlock(&reaper_lock);
			
			pthread_lock(&dead_thread->lock);
			pthread_destroy_internal(dead_thread);

			/*
			 * Reacquire the lock before continuing.
			 */
			pthread_lock(&reaper_lock);
		}
		last_dead_thread = pthread;
		pthread_sched_reschedule(RESCHED_BLOCK, &reaper_lock);
	}
	else {
		/*
		 * Not detached, but perhaps someone is waiting for it.
		 * Wake up the joiner thread and let it reap us.
		 */
		pthread_mutex_lock(&pthread->mutex);
		pthread->dead = 1;
		pthread_cond_signal(&pthread->cond);
		pthread_mutex_unlock(&pthread->mutex);
		pthread_sched_reschedule(RESCHED_BLOCK, NULL);
	}
	
	/*
	 * Never returns.
	 */
}

/*
 * Called from the idle loop each time through to look for dead threads.
 */
void
pthread_reap_threads(void)
{
	pthread_thread_t	*pthread;
	
	pthread_lock(&reaper_lock);
	while (last_dead_thread) {
		/*
		 * Clear last_dead_thread and release the reaper
		 * lock so that it will not be held during the
		 * thread destruction (memory deallocation might
		 * block). 
		 */
		pthread = last_dead_thread;
		last_dead_thread = NULL_THREADPTR;
		pthread_unlock(&reaper_lock);
		
		pthread_lock(&pthread->lock);
		assert(pthread->flags & THREAD_DETACHED);
		pthread_destroy_internal(pthread);

		pthread_lock(&reaper_lock);
	}
	pthread_unlock(&reaper_lock);
}

void
pthread_init_exit()
{
	pthread_lock_init(&reaper_lock);
}