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 //go:build linux && (386 || arm || loong64 || mips || mipsle || mips64 || mips64le || riscv64)
6 
7 #include <pthread.h>
8 #include <string.h>
9 #include <signal.h>
10 #include "libcgo.h"
11 #include "libcgo_unix.h"
12 
13 static void *threadentry(void*);
14 
15 void (*x_cgo_inittls)(void **tlsg, void **tlsbase) __attribute__((common));
16 static void (*setg_gcc)(void*);
17 
18 void
x_cgo_init(G * g,void (* setg)(void *),void ** tlsg,void ** tlsbase)19 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
20 {
21 	setg_gcc = setg;
22 
23 	_cgo_set_stacklo(g, NULL);
24 
25 	if (x_cgo_inittls) {
26 		x_cgo_inittls(tlsg, tlsbase);
27 	}
28 }
29 
30 void
_cgo_sys_thread_start(ThreadStart * ts)31 _cgo_sys_thread_start(ThreadStart *ts)
32 {
33 	pthread_attr_t attr;
34 	sigset_t ign, oset;
35 	pthread_t p;
36 	size_t size;
37 	int err;
38 
39 	sigfillset(&ign);
40 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
41 
42 	pthread_attr_init(&attr);
43 	pthread_attr_getstacksize(&attr, &size);
44 	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
45 	ts->g->stackhi = size;
46 	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
47 
48 	pthread_sigmask(SIG_SETMASK, &oset, nil);
49 
50 	if (err != 0) {
51 		fatalf("pthread_create failed: %s", strerror(err));
52 	}
53 }
54 
55 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
56 static void*
threadentry(void * v)57 threadentry(void *v)
58 {
59 	ThreadStart ts;
60 
61 	ts = *(ThreadStart*)v;
62 	free(v);
63 
64 	crosscall1(ts.fn, setg_gcc, ts.g);
65 	return nil;
66 }
67