github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_linux_arm64.c (about) 1 // Copyright 2015 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 // +build cgo 6 7 #include <pthread.h> 8 #include <string.h> 9 #include <signal.h> 10 #include "libcgo.h" 11 12 static void *threadentry(void*); 13 14 void (*x_cgo_inittls)(void **tlsg, void **tlsbase); 15 void (*setg_gcc)(void*); 16 17 void 18 _cgo_sys_thread_start(ThreadStart *ts) 19 { 20 pthread_attr_t attr; 21 sigset_t ign, oset; 22 pthread_t p; 23 size_t size; 24 int err; 25 26 sigfillset(&ign); 27 pthread_sigmask(SIG_SETMASK, &ign, &oset); 28 29 // Not sure why the memset is necessary here, 30 // but without it, we get a bogus stack size 31 // out of pthread_attr_getstacksize. C'est la Linux. 32 memset(&attr, 0, sizeof attr); 33 pthread_attr_init(&attr); 34 size = 0; 35 pthread_attr_getstacksize(&attr, &size); 36 // Leave stacklo=0 and set stackhi=size; mstack will do the rest. 37 ts->g->stackhi = size; 38 err = pthread_create(&p, &attr, threadentry, ts); 39 40 pthread_sigmask(SIG_SETMASK, &oset, nil); 41 42 if (err != 0) { 43 fatalf("pthread_create failed: %s", strerror(err)); 44 } 45 } 46 47 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g); 48 static void* 49 threadentry(void *v) 50 { 51 ThreadStart ts; 52 53 ts = *(ThreadStart*)v; 54 free(v); 55 56 crosscall1(ts.fn, setg_gcc, (void*)ts.g); 57 return nil; 58 } 59 60 void 61 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase) 62 { 63 pthread_attr_t attr; 64 size_t size; 65 66 setg_gcc = setg; 67 pthread_attr_init(&attr); 68 pthread_attr_getstacksize(&attr, &size); 69 g->stacklo = (uintptr)&attr - size + 4096; 70 pthread_attr_destroy(&attr); 71 72 if (x_cgo_inittls) { 73 x_cgo_inittls(tlsg, tlsbase); 74 } 75 }