github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/src/runtime/cgo/gcc_linux_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 <pthread.h> 6 #include <errno.h> 7 #include <string.h> // strerror 8 #include <signal.h> 9 #include <stdlib.h> 10 #include "libcgo.h" 11 12 static void* threadentry(void*); 13 static void (*setg_gcc)(void*); 14 15 // These will be set in gcc_android_amd64.c for android-specific customization. 16 void (*x_cgo_inittls)(void); 17 void* (*x_cgo_threadentry)(void*); 18 19 void 20 x_cgo_init(G* g, void (*setg)(void*)) 21 { 22 pthread_attr_t *attr; 23 size_t size; 24 25 /* The memory sanitizer distributed with versions of clang 26 before 3.8 has a bug: if you call mmap before malloc, mmap 27 may return an address that is later overwritten by the msan 28 library. Avoid this problem by forcing a call to malloc 29 here, before we ever call malloc. 30 31 This is only required for the memory sanitizer, so it's 32 unfortunate that we always run it. It should be possible 33 to remove this when we no longer care about versions of 34 clang before 3.8. The test for this is 35 misc/cgo/testsanitizers. 36 37 GCC works hard to eliminate a seemingly unnecessary call to 38 malloc, so we actually use the memory we allocate. */ 39 40 setg_gcc = setg; 41 attr = (pthread_attr_t*)malloc(sizeof *attr); 42 if (attr == NULL) { 43 fatalf("malloc failed: %s", strerror(errno)); 44 } 45 pthread_attr_init(attr); 46 pthread_attr_getstacksize(attr, &size); 47 g->stacklo = (uintptr)&size - size + 4096; 48 pthread_attr_destroy(attr); 49 free(attr); 50 51 if (x_cgo_inittls) { 52 x_cgo_inittls(); 53 } 54 } 55 56 57 void 58 _cgo_sys_thread_start(ThreadStart *ts) 59 { 60 pthread_attr_t attr; 61 sigset_t ign, oset; 62 pthread_t p; 63 size_t size; 64 int err; 65 66 sigfillset(&ign); 67 pthread_sigmask(SIG_SETMASK, &ign, &oset); 68 69 pthread_attr_init(&attr); 70 pthread_attr_getstacksize(&attr, &size); 71 // Leave stacklo=0 and set stackhi=size; mstack will do the rest. 72 ts->g->stackhi = size; 73 err = pthread_create(&p, &attr, threadentry, ts); 74 75 pthread_sigmask(SIG_SETMASK, &oset, nil); 76 77 if (err != 0) { 78 fatalf("pthread_create failed: %s", strerror(err)); 79 } 80 } 81 82 static void* 83 threadentry(void *v) 84 { 85 if (x_cgo_threadentry) { 86 return x_cgo_threadentry(v); 87 } 88 89 ThreadStart ts; 90 91 ts = *(ThreadStart*)v; 92 _cgo_tsan_acquire(); 93 free(v); 94 _cgo_tsan_release(); 95 96 /* 97 * Set specific keys. 98 */ 99 setg_gcc((void*)ts.g); 100 101 crosscall_amd64(ts.fn); 102 return nil; 103 }