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