github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_darwin_arm.c (about) 1 // Copyright 2014 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 <limits.h> 8 #include <pthread.h> 9 #include <signal.h> 10 #include <string.h> /* for strerror */ 11 #include <sys/param.h> 12 #include <unistd.h> 13 14 #include "libcgo.h" 15 16 #include <CoreFoundation/CFBundle.h> 17 #include <CoreFoundation/CFString.h> 18 19 #define magic (0xe696c4f4U) 20 21 // inittls allocates a thread-local storage slot for g. 22 // 23 // It finds the first available slot using pthread_key_create and uses 24 // it as the offset value for runtime.tlsg. 25 static void 26 inittls(void **tlsg, void **tlsbase) 27 { 28 pthread_key_t k; 29 int i, err; 30 31 err = pthread_key_create(&k, nil); 32 if(err != 0) { 33 fprintf(stderr, "runtime/cgo: pthread_key_create failed: %d\n", err); 34 abort(); 35 } 36 //fprintf(stderr, "runtime/cgo: k = %d, tlsbase = %p\n", (int)k, tlsbase); // debug 37 pthread_setspecific(k, (void*)magic); 38 // The first key should be at 258. 39 for (i=0; i<PTHREAD_KEYS_MAX; i++) { 40 if (*(tlsbase+i) == (void*)magic) { 41 *tlsg = (void*)(i*sizeof(void *)); 42 pthread_setspecific(k, 0); 43 return; 44 } 45 } 46 fprintf(stderr, "runtime/cgo: could not find pthread key.\n"); 47 abort(); 48 } 49 50 static void *threadentry(void*); 51 void (*setg_gcc)(void*); 52 53 void 54 _cgo_sys_thread_start(ThreadStart *ts) 55 { 56 pthread_attr_t attr; 57 sigset_t ign, oset; 58 pthread_t p; 59 size_t size; 60 int err; 61 62 sigfillset(&ign); 63 pthread_sigmask(SIG_SETMASK, &ign, &oset); 64 65 pthread_attr_init(&attr); 66 size = 0; 67 pthread_attr_getstacksize(&attr, &size); 68 // Leave stacklo=0 and set stackhi=size; mstack will do the rest. 69 ts->g->stackhi = size; 70 err = pthread_create(&p, &attr, threadentry, ts); 71 72 pthread_sigmask(SIG_SETMASK, &oset, nil); 73 74 if (err != 0) { 75 fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err)); 76 abort(); 77 } 78 } 79 80 extern void crosscall_arm1(void (*fn)(void), void (*setg_gcc)(void*), void *g); 81 static void* 82 threadentry(void *v) 83 { 84 ThreadStart ts; 85 86 ts = *(ThreadStart*)v; 87 free(v); 88 89 darwin_arm_init_thread_exception_port(); 90 91 crosscall_arm1(ts.fn, setg_gcc, (void*)ts.g); 92 return nil; 93 } 94 95 // init_working_dir sets the current working directory to the app root. 96 // By default darwin/arm processes start in "/". 97 static void 98 init_working_dir() 99 { 100 CFBundleRef bundle = CFBundleGetMainBundle(); 101 if (bundle == NULL) { 102 fprintf(stderr, "runtime/cgo: no main bundle\n"); 103 return; 104 } 105 CFURLRef url_ref = CFBundleCopyResourceURL(bundle, CFSTR("Info"), CFSTR("plist"), NULL); 106 if (url_ref == NULL) { 107 fprintf(stderr, "runtime/cgo: no Info.plist URL\n"); 108 return; 109 } 110 CFStringRef url_str_ref = CFURLGetString(url_ref); 111 char url[MAXPATHLEN]; 112 if (!CFStringGetCString(url_str_ref, url, sizeof(url), kCFStringEncodingUTF8)) { 113 fprintf(stderr, "runtime/cgo: cannot get URL string\n"); 114 return; 115 } 116 117 // url is of the form "file:///path/to/Info.plist". 118 // strip it down to the working directory "/path/to". 119 int url_len = strlen(url); 120 if (url_len < sizeof("file://")+sizeof("/Info.plist")) { 121 fprintf(stderr, "runtime/cgo: bad URL: %s\n", url); 122 return; 123 } 124 url[url_len-sizeof("/Info.plist")+1] = 0; 125 char *dir = &url[0] + sizeof("file://")-1; 126 127 if (chdir(dir) != 0) { 128 fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", dir); 129 } 130 131 // No-op to set a breakpoint on, immediately after the real chdir. 132 // Gives the test harness in go_darwin_arm_exec (which uses lldb) a 133 // chance to move the working directory. 134 getwd(dir); 135 } 136 137 void 138 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase) 139 { 140 pthread_attr_t attr; 141 size_t size; 142 143 setg_gcc = setg; 144 pthread_attr_init(&attr); 145 pthread_attr_getstacksize(&attr, &size); 146 g->stacklo = (uintptr)&attr - size + 4096; 147 pthread_attr_destroy(&attr); 148 149 // yes, tlsbase from mrc might not be correctly aligned. 150 inittls(tlsg, (void**)((uintptr)tlsbase & ~3)); 151 152 darwin_arm_init_mach_exception_handler(); 153 darwin_arm_init_thread_exception_port(); 154 init_working_dir(); 155 }