Home | History | Annotate | Download | only in cgo
      1 // Copyright 2012 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 <sys/types.h>
      6 #include <machine/sysarch.h>
      7 #include <sys/signalvar.h>
      8 #include <pthread.h>
      9 #include <signal.h>
     10 #include <string.h>
     11 #include "libcgo.h"
     12 #include "libcgo_unix.h"
     13 
     14 #ifdef ARM_TP_ADDRESS
     15 // ARM_TP_ADDRESS is (ARM_VECTORS_HIGH + 0x1000) or 0xffff1000
     16 // and is known to runtime.read_tls_fallback. Verify it with
     17 // cpp.
     18 #if ARM_TP_ADDRESS != 0xffff1000
     19 #error Wrong ARM_TP_ADDRESS!
     20 #endif
     21 #endif
     22 
     23 static void *threadentry(void*);
     24 
     25 static void (*setg_gcc)(void*);
     26 
     27 void
     28 x_cgo_init(G *g, void (*setg)(void*))
     29 {
     30 	pthread_attr_t attr;
     31 	size_t size;
     32 
     33 	setg_gcc = setg;
     34 	pthread_attr_init(&attr);
     35 	pthread_attr_getstacksize(&attr, &size);
     36 	g->stacklo = (uintptr)&attr - size + 4096;
     37 	pthread_attr_destroy(&attr);
     38 }
     39 
     40 
     41 void
     42 _cgo_sys_thread_start(ThreadStart *ts)
     43 {
     44 	pthread_attr_t attr;
     45 	sigset_t ign, oset;
     46 	pthread_t p;
     47 	size_t size;
     48 	int err;
     49 
     50 	SIGFILLSET(ign);
     51 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
     52 
     53 	// Not sure why the memset is necessary here,
     54 	// but without it, we get a bogus stack size
     55 	// out of pthread_attr_getstacksize. C'est la Linux.
     56 	memset(&attr, 0, sizeof attr);
     57 	pthread_attr_init(&attr);
     58 	size = 0;
     59 	pthread_attr_getstacksize(&attr, &size);
     60 	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
     61 	ts->g->stackhi = size;
     62 	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
     63 
     64 	pthread_sigmask(SIG_SETMASK, &oset, nil);
     65 
     66 	if (err != 0) {
     67 		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
     68 		abort();
     69 	}
     70 }
     71 
     72 extern void crosscall_arm1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
     73 static void*
     74 threadentry(void *v)
     75 {
     76 	ThreadStart ts;
     77 
     78 	ts = *(ThreadStart*)v;
     79 	free(v);
     80 
     81 	crosscall_arm1(ts.fn, setg_gcc, (void*)ts.g);
     82 	return nil;
     83 }
     84