| 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 |
| 18 |
x_cgo_init(G *g, void (*setg)(void*)) |
| 19 |
{ |
| 20 |
setg_gcc = setg; |
| 21 |
_cgo_set_stacklo(g, NULL); |
| 22 |
} |
| 23 |
|
| 24 |
void |
| 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_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 38 |
pthread_attr_getstacksize(&attr, &size); |
| 39 |
|
| 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 |
|
| 57 |
ts = *(ThreadStart*)v; |
| 58 |
free(v); |
| 59 |
|
| 60 |
crosscall1(ts.fn, setg_gcc, ts.g); |
| 61 |
return nil; |
| 62 |
} |