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