github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/runtime/cgo/gcc_linux_arm.c (about)

     1  // Copyright 2010 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  static void (*setmg_gcc)(void*, void*);
    13  
    14  void
    15  x_cgo_init(G *g, void (*setmg)(void*, void*))
    16  {
    17  	pthread_attr_t attr;
    18  	size_t size;
    19  
    20  	setmg_gcc = setmg;
    21  	pthread_attr_init(&attr);
    22  	pthread_attr_getstacksize(&attr, &size);
    23  	g->stackguard = (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  	// Not sure why the memset is necessary here,
    41  	// but without it, we get a bogus stack size
    42  	// out of pthread_attr_getstacksize.  C'est la Linux.
    43  	memset(&attr, 0, sizeof attr);
    44  	pthread_attr_init(&attr);
    45  	size = 0;
    46  	pthread_attr_getstacksize(&attr, &size);
    47  	ts->g->stackguard = size;
    48  	err = pthread_create(&p, &attr, threadentry, ts);
    49  
    50  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    51  
    52  	if (err != 0) {
    53  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    54  		abort();
    55  	}
    56  }
    57  
    58  extern void crosscall_arm2(void (*fn)(void), void (*setmg_gcc)(void*, void*), void*, void*);
    59  static void*
    60  threadentry(void *v)
    61  {
    62  	ThreadStart ts;
    63  
    64  	ts = *(ThreadStart*)v;
    65  	free(v);
    66  
    67  	ts.g->stackbase = (uintptr)&ts;
    68  
    69  	/*
    70  	 * _cgo_sys_thread_start set stackguard to stack size;
    71  	 * change to actual guard pointer.
    72  	 */
    73  	ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096 * 2;
    74  
    75  	crosscall_arm2(ts.fn, setmg_gcc, (void*)ts.m, (void*)ts.g);
    76  	return nil;
    77  }