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_getstacksize(&attr, &size);
39 // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
40 ts->g->stackhi = size;
41 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
42
43 pthread_sigmask(SIG_SETMASK, &oset, nil);
44
45 if (err != 0) {
46 fatalf("pthread_create failed: %s", strerror(err));
47 }
48 }
49
50 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
51 static void*
52 threadentry(void *v)
53 {
54 ThreadStart ts;
55 stack_t ss;
56
57 ts = *(ThreadStart*)v;
58 free(v);
59
60 // On NetBSD, a new thread inherits the signal stack of the
61 // creating thread. That confuses minit, so we remove that
62 // signal stack here before calling the regular mstart. It's
63 // a bit baroque to remove a signal stack here only to add one
64 // in minit, but it's a simple change that keeps NetBSD
65 // working like other OS's. At this point all signals are
66 // blocked, so there is no race.
67 memset(&ss, 0, sizeof ss);
68 ss.ss_flags = SS_DISABLE;
69 sigaltstack(&ss, nil);
70
71 crosscall1(ts.fn, setg_gcc, ts.g);
72 return nil;
73 }
74
View as plain text