1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 
5 #include <string.h> /* for strerror */
6 #include <pthread.h>
7 #include <signal.h>
8 #include "libcgo.h"
9 #include "libcgo_unix.h"
10 
11 static void* threadentry(void*);
12 static void (*setg_gcc)(void*);
13 
14 void
x_cgo_init(G * g,void (* setg)(void *),void ** tlsg,void ** tlsbase)15 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
16 {
17 	setg_gcc = setg;
18 	_cgo_set_stacklo(g, NULL);
19 }
20 
21 
22 void
_cgo_sys_thread_start(ThreadStart * ts)23 _cgo_sys_thread_start(ThreadStart *ts)
24 {
25 	pthread_attr_t attr;
26 	sigset_t ign, oset;
27 	pthread_t p;
28 	size_t size;
29 	int err;
30 
31 	sigfillset(&ign);
32 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
33 
34 	size = pthread_get_stacksize_np(pthread_self());
35 	pthread_attr_init(&attr);
36 	pthread_attr_setstacksize(&attr, size);
37 	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
38 	ts->g->stackhi = size;
39 	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
40 
41 	pthread_sigmask(SIG_SETMASK, &oset, nil);
42 
43 	if (err != 0) {
44 		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
45 		abort();
46 	}
47 }
48 
49 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
50 static void*
threadentry(void * v)51 threadentry(void *v)
52 {
53 	ThreadStart ts;
54 
55 	ts = *(ThreadStart*)v;
56 	free(v);
57 
58 	crosscall1(ts.fn, setg_gcc, (void*)ts.g);
59 	return nil;
60 }
61