github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/src/runtime/cgo/gcc_linux_s390x.c (about)

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