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
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
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_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
44 pthread_attr_getstacksize(&attr, &size);
45 // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
46 ts->g->stackhi = size;
47 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
48
49 pthread_sigmask(SIG_SETMASK, &oset, nil);
50
51 if (err != 0) {
52 fatalf("pthread_create failed: %s", strerror(err));
53 }
54 }
55
56 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
57 static void*
58 threadentry(void *v)
59 {
60 ThreadStart ts;
61
62 ts = *(ThreadStart*)v;
63 free(v);
64
65 crosscall1(ts.fn, setg_gcc, ts.g);
66 return nil;
67 }
68
View as plain text