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 openbsd && (386 || arm || amd64 || arm64 || riscv64)
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
x_cgo_init(G * g,void (* setg)(void *))18 x_cgo_init(G *g, void (*setg)(void*))
19 {
20 	setg_gcc = setg;
21 	_cgo_set_stacklo(g, NULL);
22 }
23 
24 void
_cgo_sys_thread_start(ThreadStart * ts)25 _cgo_sys_thread_start(ThreadStart *ts)
26 {
27 	pthread_attr_t attr;
28 	sigset_t ign, oset;
29 	pthread_t p;
30 	size_t size;
31 	int err;
32 
33 	sigfillset(&ign);
34 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
35 
36 	pthread_attr_init(&attr);
37 	pthread_attr_getstacksize(&attr, &size);
38 
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*
threadentry(void * v)52 threadentry(void *v)
53 {
54 	ThreadStart ts;
55 
56 	ts = *(ThreadStart*)v;
57 	free(v);
58 
59 	crosscall1(ts.fn, setg_gcc, ts.g);
60 	return nil;
61 }
62