github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/src/runtime/cgo/gcc_solaris_amd64.c (about)

     1  // Copyright 2015 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 <ucontext.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  	ucontext_t ctx;
    18  
    19  	setg_gcc = setg;
    20  	if (getcontext(&ctx) != 0)
    21  		perror("runtime/cgo: getcontext failed");
    22  	g->stacklo = (uintptr_t)ctx.uc_stack.ss_sp;
    23  
    24  	// Solaris processes report a tiny stack when run with "ulimit -s unlimited".
    25  	// Correct that as best we can: assume it's at least 1 MB.
    26  	// See golang.org/issue/12210.
    27  	if(ctx.uc_stack.ss_size < 1024*1024)
    28  		g->stacklo -= 1024*1024 - ctx.uc_stack.ss_size;
    29  }
    30  
    31  void
    32  _cgo_sys_thread_start(ThreadStart *ts)
    33  {
    34  	pthread_attr_t attr;
    35  	sigset_t ign, oset;
    36  	pthread_t p;
    37  	void *base;
    38  	size_t size;
    39  	int err;
    40  
    41  	sigfillset(&ign);
    42  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    43  
    44  	pthread_attr_init(&attr);
    45  
    46  	if (pthread_attr_getstack(&attr, &base, &size) != 0)
    47  		perror("runtime/cgo: pthread_attr_getstack failed");
    48  	if (size == 0) {
    49  		ts->g->stackhi = 2 << 20;
    50  		if (pthread_attr_setstack(&attr, NULL, ts->g->stackhi) != 0)
    51  			perror("runtime/cgo: pthread_attr_setstack failed");
    52  	} else {
    53  		ts->g->stackhi = size;
    54  	}
    55  	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    56  	err = pthread_create(&p, &attr, threadentry, ts);
    57  
    58  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    59  
    60  	if (err != 0) {
    61  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    62  		abort();
    63  	}
    64  }
    65  
    66  static void*
    67  threadentry(void *v)
    68  {
    69  	ThreadStart ts;
    70  
    71  	ts = *(ThreadStart*)v;
    72  	free(v);
    73  
    74  	/*
    75  	 * Set specific keys.
    76  	 */
    77  	setg_gcc((void*)ts.g);
    78  
    79  	crosscall_amd64(ts.fn);
    80  	return nil;
    81  }