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 netbsd && (386 || amd64 || arm || arm64)
6
7 #include <sys/types.h>
8 #include <pthread.h>
9 #include <signal.h>
10 #include <string.h>
11 #include "libcgo.h"
12 #include "libcgo_unix.h"
13
14 static void* threadentry(void*);
15 static void (*setg_gcc)(void*);
16
17 void
18 x_cgo_init(G *g, void (*setg)(void*))
19 {
20 setg_gcc = setg;
21 _cgo_set_stacklo(g, NULL);
22 }
23
24
25 void
26 _cgo_sys_thread_start(ThreadStart *ts)
27 {
28 pthread_attr_t attr;
29 sigset_t ign, oset;
30 pthread_t p;
31 size_t size;
32 int err;
33
34 sigfillset(&ign);
35 pthread_sigmask(SIG_SETMASK, &ign, &oset);
36
37 pthread_attr_init(&attr);
38 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
39 pthread_attr_getstacksize(&attr, &size);
40 // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
41 ts->g->stackhi = size;
42 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
43
44 pthread_sigmask(SIG_SETMASK, &oset, nil);
45
46 if (err != 0) {
47 fatalf("pthread_create failed: %s", strerror(err));
48 }
49 }
50
51 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
52 static void*
53 threadentry(void *v)
54 {
55 ThreadStart ts;
56 stack_t ss;
57
58 ts = *(ThreadStart*)v;
59 free(v);
60
61 // On NetBSD, a new thread inherits the signal stack of the
62 // creating thread. That confuses minit, so we remove that
63 // signal stack here before calling the regular mstart. It's
64 // a bit baroque to remove a signal stack here only to add one
65 // in minit, but it's a simple change that keeps NetBSD
66 // working like other OS's. At this point all signals are
67 // blocked, so there is no race.
68 memset(&ss, 0, sizeof ss);
69 ss.ss_flags = SS_DISABLE;
70 sigaltstack(&ss, nil);
71
72 crosscall1(ts.fn, setg_gcc, ts.g);
73 return nil;
74 }
75
View as plain text