github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/src/runtime/cgo/gcc_akaros_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 <pthread.h>
     6  #include <string.h>
     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  	int dummy;
    17  	// The system stack is set to a fixed size of 256 pages
    18  	g->stackguard = (uintptr)&dummy - 256*4096 + 4096;
    19  	setmg_gcc = setmg;
    20  }
    21  
    22  
    23  void
    24  _cgo_sys_thread_start(ThreadStart *ts)
    25  {
    26  	pthread_attr_t attr;
    27  	sigset_t ign, oset;
    28  	pthread_t p;
    29  	size_t size;
    30  	int err;
    31  
    32  	sigfillset(&ign);
    33  	sigprocmask(SIG_SETMASK, &ign, &oset);
    34  
    35  	// Not sure why the memset is necessary here,
    36  	// but without it, we get a bogus stack size
    37  	// out of pthread_attr_getstacksize.  C'est la Linux.
    38  	memset(&attr, 0, sizeof attr);
    39  	pthread_attr_init(&attr);
    40  	size = 0;
    41  	pthread_attr_getstacksize(&attr, &size);
    42  	ts->g->stackguard = size;
    43  	err = pthread_create(&p, &attr, threadentry, ts);
    44  
    45  	sigprocmask(SIG_SETMASK, &oset, nil);
    46  
    47  	if (err != 0) {
    48  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    49  		abort();
    50  	}
    51  }
    52  
    53  static void*
    54  threadentry(void *v)
    55  {
    56  	ThreadStart ts;
    57  
    58  	ts = *(ThreadStart*)v;
    59  	free(v);
    60  
    61  	ts.g->stackbase = (uintptr)&ts;
    62  
    63  	/*
    64  	 * _cgo_sys_thread_start set stackguard to stack size;
    65  	 * change to actual guard pointer.
    66  	 */
    67  	ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096;
    68  
    69  	/*
    70  	 * Set specific keys.
    71  	 */
    72  	setmg_gcc((void*)ts.m, (void*)ts.g);
    73  
    74  	crosscall_386(ts.fn);
    75  	return nil;
    76  }