github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_darwin_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 // +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 (0x23581321U) 16 17 static void 18 inittls(void) 19 { 20 uint32 x; 21 pthread_key_t tofree[128], k; 22 int i, ntofree; 23 24 /* 25 * Allocate thread-local storage slot for g. 26 * The key numbers start at 0x100, and we expect to be 27 * one of the early calls to pthread_key_create, so we 28 * should be able to get a pretty low number. 29 * 30 * In Darwin/386 pthreads, %gs points at the thread 31 * structure, and each key is an index into the thread-local 32 * storage array that begins at offset 0x48 within in that structure. 33 * It may happen that we are not quite the first function to try 34 * to allocate thread-local storage keys, so instead of depending 35 * on getting 0x100, we try for 0x108, allocating keys until 36 * we get the one we want and then freeing the ones we didn't want. 37 * 38 * Thus the final offset to use in %gs references is 39 * 0x48+4*0x108 = 0x468. 40 * 41 * The linker and runtime hard-code this constant offset 42 * from %gs where we expect to find g. 43 * Known to ../../../liblink/sym.c:/468 44 * and to ../sys_darwin_386.s:/468 45 * 46 * This is truly disgusting and a bit fragile, but taking care 47 * of it here protects the rest of the system from damage. 48 * The alternative would be to use a global variable that 49 * held the offset and refer to that variable each time we 50 * need a %gs variable (g). That approach would 51 * require an extra instruction and memory reference in 52 * every stack growth prolog and would also require 53 * rewriting the code that 8c generates for extern registers. 54 * 55 * Things get more disgusting on OS X 10.7 Lion. 56 * The 0x48 base mentioned above is the offset of the tsd 57 * array within the per-thread structure on Leopard and Snow Leopard. 58 * On Lion, the base moved a little, so while the math above 59 * still applies, the base is different. Thus, we cannot 60 * look for specific key values if we want to build binaries 61 * that run on both systems. Instead, forget about the 62 * specific key values and just allocate and initialize per-thread 63 * storage until we find a key that writes to the memory location 64 * we want. Then keep that key. 65 */ 66 ntofree = 0; 67 for(;;) { 68 if(pthread_key_create(&k, nil) < 0) { 69 fprintf(stderr, "runtime/cgo: pthread_key_create failed\n"); 70 abort(); 71 } 72 pthread_setspecific(k, (void*)magic1); 73 asm volatile("movl %%gs:0x468, %0" : "=r"(x)); 74 pthread_setspecific(k, 0); 75 if(x == magic1) { 76 k1 = k; 77 break; 78 } 79 if(ntofree >= nelem(tofree)) { 80 fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n"); 81 fprintf(stderr, "\ttried"); 82 for(i=0; i<ntofree; i++) 83 fprintf(stderr, " %#x", (unsigned)tofree[i]); 84 fprintf(stderr, "\n"); 85 abort(); 86 } 87 tofree[ntofree++] = k; 88 } 89 90 /* 91 * We got the key we wanted. Free the others. 92 */ 93 for(i=0; i<ntofree; i++) 94 pthread_key_delete(tofree[i]); 95 } 96 97 void 98 x_cgo_init(G *g) 99 { 100 pthread_attr_t attr; 101 size_t size; 102 103 pthread_attr_init(&attr); 104 pthread_attr_getstacksize(&attr, &size); 105 g->stacklo = (uintptr)&attr - size + 4096; 106 pthread_attr_destroy(&attr); 107 108 inittls(); 109 } 110 111 112 void 113 _cgo_sys_thread_start(ThreadStart *ts) 114 { 115 pthread_attr_t attr; 116 sigset_t ign, oset; 117 pthread_t p; 118 size_t size; 119 int err; 120 121 sigfillset(&ign); 122 pthread_sigmask(SIG_SETMASK, &ign, &oset); 123 124 pthread_attr_init(&attr); 125 pthread_attr_getstacksize(&attr, &size); 126 // Leave stacklo=0 and set stackhi=size; mstack will do the rest. 127 ts->g->stackhi = size; 128 err = pthread_create(&p, &attr, threadentry, ts); 129 130 pthread_sigmask(SIG_SETMASK, &oset, nil); 131 132 if (err != 0) { 133 fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err)); 134 abort(); 135 } 136 } 137 138 static void* 139 threadentry(void *v) 140 { 141 ThreadStart ts; 142 143 ts = *(ThreadStart*)v; 144 free(v); 145 146 pthread_setspecific(k1, (void*)ts.g); 147 148 crosscall_386(ts.fn); 149 return nil; 150 }