github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/cgo/gcc_freebsd_amd64.c (about) 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 #include <sys/types.h> 6 #include <errno.h> 7 #include <sys/signalvar.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 pthread_attr_t *attr; 21 size_t size; 22 23 // Deal with memory sanitizer/clang interaction. 24 // See gcc_linux_amd64.c for details. 25 setg_gcc = setg; 26 attr = (pthread_attr_t*)malloc(sizeof *attr); 27 if (attr == NULL) { 28 fatalf("malloc failed: %s", strerror(errno)); 29 } 30 pthread_attr_init(attr); 31 pthread_attr_getstacksize(attr, &size); 32 g->stacklo = (uintptr)&attr - size + 4096; 33 pthread_attr_destroy(attr); 34 free(attr); 35 } 36 37 void 38 _cgo_sys_thread_start(ThreadStart *ts) 39 { 40 pthread_attr_t attr; 41 sigset_t ign, oset; 42 pthread_t p; 43 size_t size; 44 int err; 45 46 SIGFILLSET(ign); 47 pthread_sigmask(SIG_SETMASK, &ign, &oset); 48 49 pthread_attr_init(&attr); 50 pthread_attr_getstacksize(&attr, &size); 51 // Leave stacklo=0 and set stackhi=size; mstart will do the rest. 52 ts->g->stackhi = size; 53 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts); 54 55 pthread_sigmask(SIG_SETMASK, &oset, nil); 56 57 if (err != 0) { 58 fatalf("pthread_create failed: %s", strerror(err)); 59 } 60 } 61 62 static void* 63 threadentry(void *v) 64 { 65 ThreadStart ts; 66 67 ts = *(ThreadStart*)v; 68 _cgo_tsan_acquire(); 69 free(v); 70 _cgo_tsan_release(); 71 72 /* 73 * Set specific keys. 74 */ 75 setg_gcc((void*)ts.g); 76 77 crosscall_amd64(ts.fn); 78 return nil; 79 }