github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/runtime/cgo/gcc_linux_386.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 <pthread.h> 6 #include <string.h> 7 #include <signal.h> 8 #include "libcgo.h" 9 10 static void *threadentry(void*); 11 static void (*setg_gcc)(void*); 12 13 // These will be set in gcc_android_386.c for android-specific customization. 14 void (*x_cgo_inittls)(void); 15 void* (*x_cgo_threadentry)(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 setg_gcc = setg; 24 pthread_attr_init(&attr); 25 pthread_attr_getstacksize(&attr, &size); 26 g->stacklo = (uintptr)&attr - size + 4096; 27 pthread_attr_destroy(&attr); 28 29 if (x_cgo_inittls) { 30 x_cgo_inittls(); 31 } 32 } 33 34 35 void 36 _cgo_sys_thread_start(ThreadStart *ts) 37 { 38 pthread_attr_t attr; 39 sigset_t ign, oset; 40 pthread_t p; 41 size_t size; 42 int err; 43 44 sigfillset(&ign); 45 pthread_sigmask(SIG_SETMASK, &ign, &oset); 46 47 // Not sure why the memset is necessary here, 48 // but without it, we get a bogus stack size 49 // out of pthread_attr_getstacksize. C'est la Linux. 50 memset(&attr, 0, sizeof attr); 51 pthread_attr_init(&attr); 52 size = 0; 53 pthread_attr_getstacksize(&attr, &size); 54 // Leave stacklo=0 and set stackhi=size; mstack will do the rest. 55 ts->g->stackhi = size; 56 err = pthread_create(&p, &attr, threadentry, ts); 57 58 pthread_sigmask(SIG_SETMASK, &oset, nil); 59 60 if (err != 0) { 61 fatalf("pthread_create failed: %s", strerror(err)); 62 } 63 } 64 65 static void* 66 threadentry(void *v) 67 { 68 if (x_cgo_threadentry) { 69 return x_cgo_threadentry(v); 70 } 71 72 ThreadStart ts; 73 74 ts = *(ThreadStart*)v; 75 free(v); 76 77 /* 78 * Set specific keys. 79 */ 80 setg_gcc((void*)ts.g); 81 82 crosscall_386(ts.fn); 83 return nil; 84 }