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