github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/cgo/gcc_linux_riscv64.c (about) 1 // Copyright 2020 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 #include <pthread.h> 6 #include <string.h> 7 #include <signal.h> 8 #include "libcgo.h" 9 #include "libcgo_unix.h" 10 11 static void *threadentry(void*); 12 13 void (*x_cgo_inittls)(void **tlsg, void **tlsbase); 14 static void (*setg_gcc)(void*); 15 16 void 17 _cgo_sys_thread_start(ThreadStart *ts) 18 { 19 pthread_attr_t attr; 20 sigset_t ign, oset; 21 pthread_t p; 22 size_t size; 23 int err; 24 25 sigfillset(&ign); 26 pthread_sigmask(SIG_SETMASK, &ign, &oset); 27 28 pthread_attr_init(&attr); 29 pthread_attr_getstacksize(&attr, &size); 30 // Leave stacklo=0 and set stackhi=size; mstart will do the rest. 31 ts->g->stackhi = size; 32 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts); 33 34 pthread_sigmask(SIG_SETMASK, &oset, nil); 35 36 if (err != 0) { 37 fatalf("pthread_create failed: %s", strerror(err)); 38 } 39 } 40 41 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g); 42 static void* 43 threadentry(void *v) 44 { 45 ThreadStart ts; 46 47 ts = *(ThreadStart*)v; 48 free(v); 49 50 crosscall1(ts.fn, setg_gcc, (void*)ts.g); 51 return nil; 52 } 53 54 void 55 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase) 56 { 57 pthread_attr_t attr; 58 size_t size; 59 60 setg_gcc = setg; 61 pthread_attr_init(&attr); 62 pthread_attr_getstacksize(&attr, &size); 63 g->stacklo = (uintptr)&attr - size + 4096; 64 pthread_attr_destroy(&attr); 65 66 if (x_cgo_inittls) { 67 x_cgo_inittls(tlsg, tlsbase); 68 } 69 }