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 void 14 x_cgo_init(G *g, void (*setg)(void*)) 15 { 16 pthread_attr_t attr; 17 size_t size; 18 19 setg_gcc = setg; 20 pthread_attr_init(&attr); 21 pthread_attr_getstacksize(&attr, &size); 22 g->stacklo = (uintptr)&attr - size + 4096; 23 pthread_attr_destroy(&attr); 24 } 25 26 27 void 28 _cgo_sys_thread_start(ThreadStart *ts) 29 { 30 pthread_attr_t attr; 31 sigset_t ign, oset; 32 pthread_t p; 33 size_t size; 34 int err; 35 36 sigfillset(&ign); 37 pthread_sigmask(SIG_SETMASK, &ign, &oset); 38 39 // Not sure why the memset is necessary here, 40 // but without it, we get a bogus stack size 41 // out of pthread_attr_getstacksize. C'est la Linux. 42 memset(&attr, 0, sizeof attr); 43 pthread_attr_init(&attr); 44 size = 0; 45 pthread_attr_getstacksize(&attr, &size); 46 // Leave stacklo=0 and set stackhi=size; mstack will do the rest. 47 ts->g->stackhi = size; 48 err = pthread_create(&p, &attr, threadentry, ts); 49 50 pthread_sigmask(SIG_SETMASK, &oset, nil); 51 52 if (err != 0) { 53 fatalf("pthread_create failed: %s", strerror(err)); 54 } 55 } 56 57 static void* 58 threadentry(void *v) 59 { 60 ThreadStart ts; 61 62 ts = *(ThreadStart*)v; 63 free(v); 64 65 /* 66 * Set specific keys. 67 */ 68 setg_gcc((void*)ts.g); 69 70 crosscall_386(ts.fn); 71 return nil; 72 } 73