1 // Copyright 2016 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 <pthread.h>
6 #include <string.h>
7 #include <signal.h>
8 #include "libcgo.h"
9 #include "libcgo_unix.h"
10 
11 static void *threadentry(void*);
12 
13 void (*x_cgo_inittls)(void **tlsg, void **tlsbase);
14 static void (*setg_gcc)(void*);
15 
16 void
x_cgo_init(G * g,void (* setg)(void *),void ** tlsbase)17 x_cgo_init(G *g, void (*setg)(void*), void **tlsbase)
18 {
19 	setg_gcc = setg;
20 	_cgo_set_stacklo(g, NULL);
21 }
22 
23 void
_cgo_sys_thread_start(ThreadStart * ts)24 _cgo_sys_thread_start(ThreadStart *ts)
25 {
26 	pthread_attr_t attr;
27 	sigset_t ign, oset;
28 	pthread_t p;
29 	size_t size;
30 	int err;
31 
32 	sigfillset(&ign);
33 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
34 
35 	pthread_attr_init(&attr);
36 	pthread_attr_getstacksize(&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 		fatalf("pthread_create failed: %s", strerror(err));
45 	}
46 }
47 
48 extern void crosscall_s390x(void (*fn)(void), void *g);
49 
50 static void*
threadentry(void * v)51 threadentry(void *v)
52 {
53 	ThreadStart ts;
54 
55 	ts = *(ThreadStart*)v;
56 	free(v);
57 
58 	// Save g for this thread in C TLS
59 	setg_gcc((void*)ts.g);
60 
61 	crosscall_s390x(ts.fn, (void*)ts.g);
62 	return nil;
63 }
64