github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/runtime/cgo/gcc_linux_amd64.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 <pthread.h>
     6  #include <string.h> // strerror
     7  #include <signal.h>
     8  #include "libcgo.h"
     9  
    10  static void* threadentry(void*);
    11  static void (*setmg_gcc)(void*, void*);
    12  
    13  void
    14  x_cgo_init(G* g, void (*setmg)(void*, void*))
    15  {
    16  	pthread_attr_t attr;
    17  	size_t size;
    18  
    19  	setmg_gcc = setmg;
    20  	pthread_attr_init(&attr);
    21  	pthread_attr_getstacksize(&attr, &size);
    22  	g->stackguard = (uintptr)&attr - size + 4096;
    23  	pthread_attr_destroy(&attr);
    24  }
    25  
    26  
    27  void
    28  _cgo_sys_thread_start(ThreadStart *ts)
    29  {
    30  	pthread_attr_t attr;
    31  	sigset_t ign, oset;
    32  	pthread_t p;
    33  	size_t size;
    34  	int err;
    35  
    36  	sigfillset(&ign);
    37  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    38  
    39  	pthread_attr_init(&attr);
    40  	pthread_attr_getstacksize(&attr, &size);
    41  	ts->g->stackguard = size;
    42  	err = pthread_create(&p, &attr, threadentry, ts);
    43  
    44  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    45  
    46  	if (err != 0) {
    47  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    48  		abort();
    49  	}
    50  }
    51  
    52  static void*
    53  threadentry(void *v)
    54  {
    55  	ThreadStart ts;
    56  
    57  	ts = *(ThreadStart*)v;
    58  	free(v);
    59  
    60  	ts.g->stackbase = (uintptr)&ts;
    61  
    62  	/*
    63  	 * _cgo_sys_thread_start set stackguard to stack size;
    64  	 * change to actual guard pointer.
    65  	 */
    66  	ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096;
    67  
    68  	/*
    69  	 * Set specific keys.
    70  	 */
    71  	setmg_gcc((void*)ts.m, (void*)ts.g);
    72  
    73  	crosscall_amd64(ts.fn);
    74  	return nil;
    75  }