File: thread.c

package info (click to toggle)
mol 0.9.61-6
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,140 kB
  • ctags: 8,491
  • sloc: ansic: 50,560; asm: 2,826; sh: 458; makefile: 373; perl: 165; lex: 135; yacc: 131
file content (323 lines) | stat: -rw-r--r-- 6,816 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
316
317
318
319
320
321
322
323
/**************************************************************
*   
*   Creation Date: <1999-02-01 04:41:19 samuel>
*   Time-stamp: <2000/10/16 22:22:19 samuel>
*   
*	<thread.c>
*	
*	Thread manager
*   
*   Copyright (C) 1999, 2000 Samuel Rydh (samuel@ibrium.se)
*   
*   This program is free software; you can redistribute it and/or
*   modify it under the terms of the GNU General Public License
*   as published by the Free Software Foundation
*   
**************************************************************/

#include "mol_config.h"
#include <signal.h>
#include <pthread.h>

#include "debugger.h"
#include "thread.h"

/* GLOBALS */
pthread_t __main_th=0;

/* There seem to be a problem in glibc/the kernel,
 * which sometimes causes two threads to sleep waiting
 * on the same (unlocked) mutex. The workaround signals
 * each thread periodically to make sure no thread sleeps
 * forever.
 */
#define BUG_WORKAROUND

struct pool_thread
{
	pthread_t	thread_id;
	const char	*thread_name;

	volatile int	active;
	pthread_cond_t	active_cond;
	pthread_mutex_t	activate_mutex;
	
	volatile int	cancel;

	void 		(*func)(void*);
	void		*data;

	struct pool_thread 	*next;
	struct pool_thread 	*all_next;
};

static pthread_mutex_t pool_mutex;
static struct pool_thread *all_threads;
static struct pool_thread *free_threads;
static int inited = 0;

#define LOCK 	pthread_mutex_lock( &pool_mutex );
#define UNLOCK	pthread_mutex_unlock( &pool_mutex );

static void *thread_entry( struct pool_thread *th );

#ifdef BUG_WORKAROUND
static void *bug_workaround_entry( void *dummy );
static void bug_workaround_signal( int sig_num );

static pthread_t workaround_th_id;
#endif

void 
threadpool_init( void )
{
#ifdef BUG_WORKAROUND
	sigset_t set;
#endif
	pthread_mutex_init( &pool_mutex, NULL );

	all_threads = NULL;
	free_threads = NULL;

	__main_th = pthread_self();
	inited = 1;

	/* SIGALRM is used to wakeup a sleeping thread */
	signal( SIGALRM, (void*)bug_workaround_signal );
	sigemptyset( &set );
	sigaddset( &set, SIGALRM );
       	pthread_sigmask( SIG_UNBLOCK, &set, NULL );

#ifdef BUG_WORKAROUND
	pthread_create( &workaround_th_id, NULL, (void*)bug_workaround_entry, NULL );
#endif
}

/* It's important that schedule_cleanup is called before
 * the drivers are cleaned up.
 */
void
threadpool_cleanup( void )
{
	struct pool_thread *th, *next;

#ifdef BUG_WORKAROUND
	pthread_cancel( workaround_th_id );
	pthread_join( workaround_th_id, NULL );
#endif

	printm("Terminating threads...\n");
	for( th = all_threads; th; th=next ) {
		if( th->active )
			printm("thread '%s' is active!\n", th->thread_name ? th->thread_name : "<noname>");
		if( !th->cancel ) {
			/* sometimes pthread_cancel doesn't seem to work when 
			 * the thread is in the pthread_cond_wait() loop. Thus
			 * we wake it instead.
			 */
			th->cancel = 1;
			pthread_mutex_lock( &th->activate_mutex );
			pthread_cond_signal( &th->active_cond );
			pthread_mutex_unlock( &th->activate_mutex );

			pthread_cancel( th->thread_id );
			pthread_join( th->thread_id, NULL );
		}
		pthread_cond_destroy( &th->active_cond );
		pthread_mutex_destroy( &th->activate_mutex );
		next = th->all_next;
		free( th );
	}
	/* printm("Done\n"); */
	inited = 0;
}

int
kill_thread( pthread_t th_id )
{
	struct pool_thread *th;
	int i;
	
	LOCK;
	for( th=all_threads; th && th_id != th->thread_id; th=th->all_next )
		;
	if( !th ){
		printm("kill_thread called with unknown thread id %ld!\n", th_id);
		UNLOCK;
		return -1;
	}
	th->cancel = 1;

	if( th->active )
		pthread_cancel( th_id );
	for( i=0; i<15 && th->active; i++ )
		usleep( 100000 );
	if( th->active ){
		printm("Cancelation of thread '%s' failed. Using the big sledge\n", 
		       th->thread_name );
		pthread_kill( th_id, 9 );
		th->active = 0;
	}
	UNLOCK;

	pthread_join( th_id, NULL );
	return 0;
}

static void
thread_exit_func( void *_th )
{
	struct pool_thread *th = _th;
	th->active=0;

	/* this thread might still be in the active list */
}

pthread_t
create_thread( void (*func)(void*), void *data, const char *thread_name )
{
	struct pool_thread *th;

	if( !inited )
		return 0;

	LOCK;
	if( !free_threads ){
		th = calloc( sizeof( struct pool_thread ),1);

		/* insert in lists */
		th->all_next = all_threads;
		all_threads = th;
		
		/* build */
		pthread_cond_init( &th->active_cond, NULL );
		
		if( pthread_create( &th->thread_id, NULL, (void*)thread_entry, (void*)th )){
			printm("Failed creating thread...exiting\n");
			UNLOCK;
			exit(1);
		}
	} else {
		th = free_threads;
		free_threads = th->next;
	}

	/* wake thread */
	pthread_mutex_init( &th->activate_mutex, NULL );
	th->data = data;
	th->func = func;
	th->thread_name = thread_name;
	th->active = 1;
	pthread_mutex_lock( &th->activate_mutex );
	pthread_cond_signal( &th->active_cond );
	pthread_mutex_unlock( &th->activate_mutex );

	UNLOCK;
	return th->thread_id;
}

static void *
thread_entry( struct pool_thread *th )
{
	set_thread_sigmask();

	pthread_cleanup_push( thread_exit_func, th );
	for( ;; ) {
		pthread_mutex_lock( &th->activate_mutex );
		while( !th->active && !th->cancel )
			pthread_cond_wait( &th->active_cond, &th->activate_mutex );
		pthread_mutex_unlock( &th->activate_mutex );

		if( th->cancel )
			break;
		
		th->func( th->data );
		th->active = 0;

		/* Add thread to free list */
		LOCK;
		if( !th->cancel ) {
			th->next = free_threads;
			free_threads = th;
		}
		UNLOCK;
	}
	pthread_cleanup_pop( 1 /*execute */ );
	return NULL;
}

const char *
get_thread_name( void )
{
	struct pool_thread *th;
	pthread_t self = pthread_self();

	for( th = all_threads; th; th=th->all_next ){
		if( th->thread_id == self )
			return th->thread_name ? th->thread_name : "<noname thread>";
	}
	if( self == __main_th )
		return "main-thread";

	return "<non-registered thread>";
}

int
is_main_thread( void )
{
	return (pthread_self() == __main_th) || !inited;
}

/* 
 * This function should be used to setup the default signal mask for all 
 * threads ever created.
 */
void
set_thread_sigmask( void )
{
	sigset_t set;

	/* Best to block SIGINT & SIGPROF */
        sigemptyset( &set );
        sigaddset( &set, SIGINT );
        sigaddset( &set, SIGPROF );
	sigaddset( &set, SIGIO );
	
	pthread_sigmask( SIG_BLOCK, &set, NULL );

	sigemptyset( &set );
	sigaddset( &set, SIGALRM );
       	pthread_sigmask( SIG_UNBLOCK, &set, NULL );
}



#ifdef BUG_WORKAROUND
static void 
bug_workaround_signal( int sig_num )
{
/*	printm("ALRM %ld\n", pthread_self() );*/
}


static void *
bug_workaround_entry( void *dummy ) 
{
	struct pool_thread *th;
	int odd=1;

	set_thread_sigmask();

	for( ;; ){
		odd = !odd;
		sleep(1);

		pthread_testcancel();
		
		pthread_kill( __main_th, SIGALRM );
		for( th=all_threads; odd && th; th=th->all_next )
			pthread_kill( th->thread_id, SIGALRM );
	}
	return NULL;
}
#endif