Home | History | Annotate | Download | only in cgo
      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 ppc64 ppc64le
      6 
      7 #include <pthread.h>
      8 #include <string.h>
      9 #include <signal.h>
     10 #include "libcgo.h"
     11 #include "libcgo_unix.h"
     12 
     13 static void *threadentry(void*);
     14 
     15 void (*x_cgo_inittls)(void **tlsg, void **tlsbase);
     16 static void (*setg_gcc)(void*);
     17 
     18 void
     19 x_cgo_init(G *g, void (*setg)(void*), void **tlsbase)
     20 {
     21 	pthread_attr_t attr;
     22 	size_t size;
     23 
     24 	setg_gcc = setg;
     25 	pthread_attr_init(&attr);
     26 	pthread_attr_getstacksize(&attr, &size);
     27 	g->stacklo = (uintptr)&attr - size + 4096;
     28 	pthread_attr_destroy(&attr);
     29 }
     30 
     31 void
     32 _cgo_sys_thread_start(ThreadStart *ts)
     33 {
     34 	pthread_attr_t attr;
     35 	sigset_t ign, oset;
     36 	pthread_t p;
     37 	size_t size;
     38 	int err;
     39 
     40 	sigfillset(&ign);
     41 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
     42 
     43 	pthread_attr_init(&attr);
     44 	pthread_attr_getstacksize(&attr, &size);
     45 	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
     46 	ts->g->stackhi = size;
     47 	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
     48 
     49 	pthread_sigmask(SIG_SETMASK, &oset, nil);
     50 
     51 	if (err != 0) {
     52 		fatalf("pthread_create failed: %s", strerror(err));
     53 	}
     54 }
     55 
     56 extern void crosscall_ppc64(void (*fn)(void), void *g);
     57 
     58 static void*
     59 threadentry(void *v)
     60 {
     61 	ThreadStart ts;
     62 
     63 	ts = *(ThreadStart*)v;
     64 	free(v);
     65 
     66 	// Save g for this thread in C TLS
     67 	setg_gcc((void*)ts.g);
     68 
     69 	crosscall_ppc64(ts.fn, (void*)ts.g);
     70 	return nil;
     71 }
     72