github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/runtime/cgo/gcc_netbsd_386.c (about)

     1  // Copyright 2009 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 <pthread.h>
     7  #include <signal.h>
     8  #include <string.h>
     9  #include "libcgo.h"
    10  
    11  static void* threadentry(void*);
    12  static void (*setg_gcc)(void*);
    13  
    14  void
    15  x_cgo_init(G *g, void (*setg)(void*))
    16  {
    17  	pthread_attr_t attr;
    18  	size_t size;
    19  
    20  	setg_gcc = setg;
    21  	pthread_attr_init(&attr);
    22  	pthread_attr_getstacksize(&attr, &size);
    23  	g->stacklo = (uintptr)&attr - size + 4096;
    24  	pthread_attr_destroy(&attr);
    25  }
    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  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    50  		abort();
    51  	}
    52  }
    53  
    54  static void*
    55  threadentry(void *v)
    56  {
    57  	ThreadStart ts;
    58  	stack_t ss;
    59  
    60  	ts = *(ThreadStart*)v;
    61  	free(v);
    62  
    63  	/*
    64  	 * Set specific keys.
    65  	 */
    66  	setg_gcc((void*)ts.g);
    67  
    68  	// On NetBSD, a new thread inherits the signal stack of the
    69  	// creating thread. That confuses minit, so we remove that
    70  	// signal stack here before calling the regular mstart. It's
    71  	// a bit baroque to remove a signal stack here only to add one
    72  	// in minit, but it's a simple change that keeps NetBSD
    73  	// working like other OS's. At this point all signals are
    74  	// blocked, so there is no race.
    75  	memset(&ss, 0, sizeof ss);
    76  	ss.ss_flags = SS_DISABLE;
    77  	sigaltstack(&ss, nil);
    78  
    79  	crosscall_386(ts.fn);
    80  	return nil;
    81  }