github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_android_amd64.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 <string.h> /* for strerror */ 8 #include <pthread.h> 9 #include <signal.h> 10 #include "libcgo.h" 11 12 static void* threadentry(void*); 13 static pthread_key_t k1; 14 15 #define magic1 (0x23581321345589ULL) 16 17 static void 18 inittls(void) 19 { 20 uint64 x; 21 pthread_key_t tofree[128], k; 22 int i, ntofree; 23 24 /* 25 * Same logic, code as gcc_darwin_386.c:/inittls. 26 * Note that this is a temporary hack that should be fixed soon. 27 * Android-L and M bionic's pthread implementation differ 28 * significantly, and can change any time. 29 * https://android-review.googlesource.com/#/c/134202 30 * 31 * We chose %fs:0x1d0 which seems to work in testing with Android 32 * emulators (API22, API23) but it may break any time. 33 * 34 * TODO: fix this. 35 * 36 * The linker and runtime hard-code this constant offset 37 * from %fs where we expect to find g. Disgusting. 38 * 39 * Known to src/cmd/link/internal/ld/sym.go:/0x1d0 40 * and to src/runtime/sys_linux_amd64.s:/0x1d0 or /GOOS_android. 41 * 42 * As disgusting as on the darwin/386, darwin/amd64. 43 */ 44 ntofree = 0; 45 for(;;) { 46 if(pthread_key_create(&k, nil) < 0) { 47 fprintf(stderr, "runtime/cgo: pthread_key_create failed\n"); 48 abort(); 49 } 50 pthread_setspecific(k, (void*)magic1); 51 asm volatile("movq %%fs:0x1d0, %0" : "=r"(x)); 52 pthread_setspecific(k, 0); 53 if(x == magic1) { 54 k1 = k; 55 break; 56 } 57 if(ntofree >= nelem(tofree)) { 58 fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n"); 59 fprintf(stderr, "\ttried"); 60 for(i=0; i<ntofree; i++) 61 fprintf(stderr, " %#x", (unsigned)tofree[i]); 62 fprintf(stderr, "\n"); 63 abort(); 64 } 65 tofree[ntofree++] = k; 66 } 67 // TODO: output to stderr is not useful for apps. 68 // Can we fall back to Android's log library? 69 70 /* 71 * We got the key we wanted. Free the others. 72 */ 73 for(i=0; i<ntofree; i++) { 74 pthread_key_delete(tofree[i]); 75 } 76 } 77 78 79 static void* 80 threadentry(void *v) 81 { 82 ThreadStart ts; 83 84 ts = *(ThreadStart*)v; 85 free(v); 86 87 pthread_setspecific(k1, (void*)ts.g); 88 89 crosscall_amd64(ts.fn); 90 return nil; 91 } 92 93 void (*x_cgo_inittls)(void) = inittls; 94 void* (*x_cgo_threadentry)(void*) = threadentry;