Home | History | Annotate | Download | only in cgo
      1 // Copyright 2010 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 <string.h>
      7 #include <signal.h>
      8 #include "libcgo.h"
      9 
     10 static void *threadentry(void*);
     11 
     12 void (*x_cgo_inittls)(void **tlsg, void **tlsbase);
     13 void (*setg_gcc)(void*);
     14 
     15 void
     16 _cgo_sys_thread_start(ThreadStart *ts)
     17 {
     18 	pthread_attr_t attr;
     19 	sigset_t ign, oset;
     20 	pthread_t p;
     21 	size_t size;
     22 	int err;
     23 
     24 	sigfillset(&ign);
     25 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
     26 
     27 	// Not sure why the memset is necessary here,
     28 	// but without it, we get a bogus stack size
     29 	// out of pthread_attr_getstacksize.  C'est la Linux.
     30 	memset(&attr, 0, sizeof attr);
     31 	pthread_attr_init(&attr);
     32 	size = 0;
     33 	pthread_attr_getstacksize(&attr, &size);
     34 	// Leave stacklo=0 and set stackhi=size; mstack will do the rest.
     35 	ts->g->stackhi = size;
     36 	err = pthread_create(&p, &attr, threadentry, ts);
     37 
     38 	pthread_sigmask(SIG_SETMASK, &oset, nil);
     39 
     40 	if (err != 0) {
     41 		fatalf("pthread_create failed: %s", strerror(err));
     42 	}
     43 }
     44 
     45 extern void crosscall_arm1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
     46 static void*
     47 threadentry(void *v)
     48 {
     49 	ThreadStart ts;
     50 
     51 	ts = *(ThreadStart*)v;
     52 	free(v);
     53 
     54 	crosscall_arm1(ts.fn, setg_gcc, (void*)ts.g);
     55 	return nil;
     56 }
     57 
     58 void
     59 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
     60 {
     61 	pthread_attr_t attr;
     62 	size_t size;
     63 
     64 	setg_gcc = setg;
     65 	pthread_attr_init(&attr);
     66 	pthread_attr_getstacksize(&attr, &size);
     67 	g->stacklo = (uintptr)&attr - size + 4096;
     68 	pthread_attr_destroy(&attr);
     69 
     70 	if (x_cgo_inittls) {
     71 		x_cgo_inittls(tlsg, tlsbase);
     72 	}
     73 }
     74