github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/runtime/cgo/gcc_freebsd_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 <sys/signalvar.h>
     7  #include <pthread.h>
     8  #include <signal.h>
     9  #include <string.h>
    10  #include "libcgo.h"
    11  
    12  static void* threadentry(void*);
    13  static void (*setmg_gcc)(void*, void*);
    14  
    15  void
    16  x_cgo_init(G *g, void (*setmg)(void*, void*))
    17  {
    18  	pthread_attr_t attr;
    19  	size_t size;
    20  
    21  	setmg_gcc = setmg;
    22  	pthread_attr_init(&attr);
    23  	pthread_attr_getstacksize(&attr, &size);
    24  	g->stackguard = (uintptr)&attr - size + 4096;
    25  	pthread_attr_destroy(&attr);
    26  }
    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  	ts->g->stackguard = 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  
    59  	ts = *(ThreadStart*)v;
    60  	free(v);
    61  
    62  	ts.g->stackbase = (uintptr)&ts;
    63  
    64  	/*
    65  	 * _cgo_sys_thread_start set stackguard to stack size;
    66  	 * change to actual guard pointer.
    67  	 */
    68  	ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096;
    69  
    70  	/*
    71  	 * Set specific keys.
    72  	 */
    73  	setmg_gcc((void*)ts.m, (void*)ts.g);
    74  
    75  	crosscall_386(ts.fn);
    76  	return nil;
    77  }