File: threads.c

package info (click to toggle)
neko 1.8.1-6
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,116 kB
  • sloc: ansic: 16,707; makefile: 125; sh: 37; xml: 4
file content (352 lines) | stat: -rwxr-xr-x 8,729 bytes parent folder | download | duplicates (2)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* ************************************************************************ */
/*																			*/
/*  Neko Virtual Machine													*/
/*  Copyright (c)2005 Motion-Twin											*/
/*																			*/
/* This library is free software; you can redistribute it and/or			*/
/* modify it under the terms of the GNU Lesser General Public				*/
/* License as published by the Free Software Foundation; either				*/
/* version 2.1 of the License, or (at your option) any later version.		*/
/*																			*/
/* This library 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 GNU		*/
/* Lesser General Public License or the LICENSE file for more details.		*/
/*																			*/
/* ************************************************************************ */
#ifdef __APPLE__
// prevent later redefinition of bool
#	include <dlfcn.h>
#endif
#include "vm.h"
#include <string.h>

#if !defined(NEKO_THREADS)

#include <stdlib.h>
struct _mt_local {
	void *value;
};

#else

#ifdef NEKO_WINDOWS
// necessary for TryEnterCriticalSection
// which is only available on 2000 PRO and XP
#	define _WIN32_WINNT 0x0400
#	define GC_NOT_DLL
#	define GC_WIN32_THREADS
#endif

#define GC_THREADS
#include <gc/gc.h>

#if GC_VERSION_MAJOR < 7
#	define GC_SUCCESS	0
#	define GC_DUPLICATE	1
#endif

#ifdef NEKO_WINDOWS

struct _mt_lock {
	CRITICAL_SECTION cs;
};

#else

#include <stdlib.h>
#include <pthread.h>

struct _mt_local {
	pthread_key_t key;
};
struct _mt_lock {
	pthread_mutex_t lock;
};

// should be enough to store any GC_stack_base
// implementation
typedef char __stack_base[64];

#endif

#endif // !NEKO_THREADS

typedef struct {
	thread_main_func init;
	thread_main_func main;
	void *param;
#ifdef NEKO_THREADS
#	ifdef NEKO_WINDOWS
	HANDLE lock;
#	else
	pthread_mutex_t lock;
#	endif
#endif
} tparams;

#ifdef NEKO_THREADS

#ifdef NEKO_WINDOWS
#	define THREAD_FUN DWORD WINAPI
#else
#	define THREAD_FUN void *
#endif

typedef int (*rec)( int, void * );

static int clean_c_stack( int n, void *f ) {
	char buf[256];
	memset(buf,n,sizeof(buf));
	if( n == 0 ) return *buf;
	return ((rec)f)(n-1,f) ? 1 : 0; // prevent tail-rec
}

static THREAD_FUN ThreadMain( void *_p ) {
	tparams *lp = (tparams*)_p;
	tparams p = *lp;
	p.init(p.param);
	// we have the 'param' value on this thread C stack
	// so it's safe to give back control to main thread
#	ifdef NEKO_WINDOWS
	ReleaseSemaphore(p.lock,1,NULL);
#	else
	pthread_mutex_unlock(&lp->lock);
#	endif
	clean_c_stack(40,clean_c_stack);
	p.main(p.param);
	return 0;
}

#endif

EXTERN int neko_thread_create( thread_main_func init, thread_main_func main, void *param, void **handle ) {
	tparams p;
	p.init = init;
	p.main = main;
	p.param = param;
#	if !defined(NEKO_THREADS)
	return 0;
#	elif defined(NEKO_WINDOWS)
	{
		HANDLE h;
		p.lock = CreateSemaphore(NULL,0,1,NULL);
		h = GC_CreateThread(NULL,0,ThreadMain,&p,0,(void*)handle);
		if( h == NULL ) {
			CloseHandle(p.lock);
			return 0;
		}
		WaitForSingleObject(p.lock,INFINITE);
		CloseHandle(p.lock);
		CloseHandle(h);
		return 1;
	}
#	else
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
	pthread_mutex_init(&p.lock,NULL);
	pthread_mutex_lock(&p.lock);
	// force the use of a the GC method to capture created threads
	// this function should be defined in gc/gc.h
	if( GC_pthread_create((pthread_t*)handle,&attr,&ThreadMain,&p) != 0 ) {
		pthread_attr_destroy(&attr);
		pthread_mutex_destroy(&p.lock);
		return 0;
	}
	pthread_mutex_lock(&p.lock);
	pthread_attr_destroy(&attr);
	pthread_mutex_destroy(&p.lock);
	return 1;
#	endif
}

#if defined(NEKO_POSIX) && defined(NEKO_THREADS)
#	include <dlfcn.h>
	typedef void (*callb_func)( thread_main_func, void * );
	typedef int (*std_func)();
	typedef int (*gc_stack_ptr)( __stack_base * );

static int do_nothing( __stack_base *sb ) {
	return -1;
}

#endif

EXTERN void neko_thread_blocking( thread_main_func f, void *p ) {
#	if !defined(NEKO_THREADS)
	f(p); // nothing
#	elif defined(NEKO_WINDOWS)
	f(p); // we don't have pthreads issues
#	else
	// we have different APIs depending on the GC version, make sure we load
	// the good one at runtime
	static callb_func do_blocking = NULL;
	static std_func start = NULL, end = NULL;
	if( do_blocking )
		do_blocking(f,p);
	else if( start ) {
		start();
		f(p);
		end();
	} else {
		void *self = dlopen(NULL,0);
		do_blocking = (callb_func)dlsym(self,"GC_do_blocking");
		if( !do_blocking ) {
			start = (std_func)dlsym(self,"GC_start_blocking");
			end = (std_func)dlsym(self,"GC_end_blocking");
			if( !start || !end )
				val_throw(alloc_string("Could not init GC blocking API"));
		}
		neko_thread_blocking(f,p);
	}
#	endif
}

EXTERN bool neko_thread_register( bool t ) {
#	if !defined(NEKO_THREADS)
	return 0;
#	elif defined(NEKO_WINDOWS)
	struct GC_stack_base sb;
	int r;
	if( !t )
		return GC_unregister_my_thread() == GC_SUCCESS;
	if( GC_get_stack_base(&sb) != GC_SUCCESS )
		return 0;
	r = GC_register_my_thread(&sb);
	return( r == GC_SUCCESS || r == GC_DUPLICATE );
#	else
	// since the API is only available on GC 7.0,
	// we will do our best to locate it dynamically
	static gc_stack_ptr get_sb = NULL, my_thread = NULL;
	static std_func unreg_my_thread = NULL;
	if( !t && unreg_my_thread != NULL ) {
		return unreg_my_thread() == GC_SUCCESS;
	} else if( my_thread != NULL ) {
		__stack_base sb;
		int r;
		if( get_sb(&sb) != GC_SUCCESS )
			return 0;
		r = my_thread(&sb);
		return( r == GC_SUCCESS || r == GC_DUPLICATE );
	} else {
		void *self = dlopen(NULL,0);
		my_thread = (gc_stack_ptr)dlsym(self,"GC_register_my_thread");
		get_sb = (gc_stack_ptr)dlsym(self,"GC_get_stack_base");
		unreg_my_thread = (std_func)dlsym(self,"GC_unregister_my_thread");
		if( my_thread == NULL ) my_thread = do_nothing;
		if( get_sb == NULL ) get_sb = do_nothing;
		if( unreg_my_thread == NULL ) unreg_my_thread = (std_func)do_nothing;
		return neko_thread_register(t);
	}
#	endif
}


EXTERN mt_local *alloc_local() {
#	if !defined(NEKO_THREADS)
	mt_local *l = malloc(sizeof(mt_local));
	l->value = NULL;
	return l;
#	elif defined(NEKO_WINDOWS)
	DWORD t = TlsAlloc();
	TlsSetValue(t,NULL);
	return (mt_local*)(int_val)t;
#	else
	mt_local *l = malloc(sizeof(mt_local));
	pthread_key_create(&l->key,NULL);
	return l;
#	endif
}

EXTERN void free_local( mt_local *l ) {
#	if !defined(NEKO_THREADS)
	free(l);
#	elif defined(NEKO_WINDOWS)
	TlsFree((DWORD)(int_val)l);
#	else
	pthread_key_delete(l->key);
	free(l);
#	endif
}

EXTERN void local_set( mt_local *l, void *v ) {
#	if !defined(NEKO_THREADS)
	l->value = v;
#	elif defined(NEKO_WINDOWS)
	TlsSetValue((DWORD)(int_val)l,v);
#	else
	pthread_setspecific(l->key,v);
#	endif
}

EXTERN void *local_get( mt_local *l ) {
	if( l == NULL )
		return NULL;
#	if !defined(NEKO_THREADS)
	return l->value;
#	elif defined(NEKO_WINDOWS)
	return (void*)TlsGetValue((DWORD)(int_val)l);
#	else
	return pthread_getspecific(l->key);
#	endif
}

EXTERN mt_lock *alloc_lock() {
#	if !defined(NEKO_THREADS)
	return (mt_lock*)1;
#	elif defined(NEKO_WINDOWS)
	mt_lock *l = (mt_lock*)malloc(sizeof(mt_lock));
	InitializeCriticalSection(&l->cs);
	return l;
#	else
	mt_lock *l = (mt_lock*)malloc(sizeof(mt_lock));
	pthread_mutexattr_t a;
	pthread_mutexattr_init(&a);
	pthread_mutexattr_settype(&a,PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&l->lock,&a);
	pthread_mutexattr_destroy(&a);
	return l;
#	endif
}

EXTERN void lock_acquire( mt_lock *l ) {
#	if !defined(NEKO_THREADS)
#	elif defined(NEKO_WINDOWS)
	EnterCriticalSection(&l->cs);
#	else
	pthread_mutex_lock(&l->lock);
#	endif
}

EXTERN int lock_try( mt_lock *l ) {
#if	!defined(NEKO_THREADS)
	return 1;
#	elif defined(NEKO_WINDOWS)
	return TryEnterCriticalSection(&l->cs);
#	else
	return pthread_mutex_trylock(&l->lock) == 0;
#	endif
}

EXTERN void lock_release( mt_lock *l ) {
#	if !defined(NEKO_THREADS)
#	elif defined(NEKO_WINDOWS)
	LeaveCriticalSection(&l->cs);
#	else
	pthread_mutex_unlock(&l->lock);
#	endif
}

EXTERN void free_lock( mt_lock *l ) {
#	if !defined(NEKO_THREADS)
#	elif defined(NEKO_WINDOWS)
	DeleteCriticalSection(&l->cs);
	free(l);
#	else
	pthread_mutex_destroy(&l->lock);
	free(l);
#	endif
}

/* ************************************************************************ */