github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_freebsd_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  // +build cgo
     6  
     7  #include <sys/types.h>
     8  #include <sys/signalvar.h>
     9  #include <pthread.h>
    10  #include <signal.h>
    11  #include <string.h>
    12  #include "libcgo.h"
    13  
    14  static void* threadentry(void*);
    15  static void (*setg_gcc)(void*);
    16  
    17  void
    18  x_cgo_init(G *g, void (*setg)(void*))
    19  {
    20  	pthread_attr_t attr;
    21  	size_t size;
    22  
    23  	setg_gcc = setg;
    24  	pthread_attr_init(&attr);
    25  	pthread_attr_getstacksize(&attr, &size);
    26  	g->stacklo = (uintptr)&attr - size + 4096;
    27  	pthread_attr_destroy(&attr);
    28  }
    29  
    30  void
    31  _cgo_sys_thread_start(ThreadStart *ts)
    32  {
    33  	pthread_attr_t attr;
    34  	sigset_t ign, oset;
    35  	pthread_t p;
    36  	size_t size;
    37  	int err;
    38  
    39  	SIGFILLSET(ign);
    40  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    41  
    42  	pthread_attr_init(&attr);
    43  	pthread_attr_getstacksize(&attr, &size);
    44  
    45  	// Leave stacklo=0 and set stackhi=size; mstack will do the rest.
    46  	ts->g->stackhi = size;
    47  	err = pthread_create(&p, &attr, threadentry, ts);
    48  
    49  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    50  
    51  	if (err != 0) {
    52  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    53  		abort();
    54  	}
    55  }
    56  
    57  static void*
    58  threadentry(void *v)
    59  {
    60  	ThreadStart ts;
    61  
    62  	ts = *(ThreadStart*)v;
    63  	free(v);
    64  
    65  	/*
    66  	 * Set specific keys.
    67  	 */
    68  	setg_gcc((void*)ts.g);
    69  
    70  	crosscall_amd64(ts.fn);
    71  	return nil;
    72  }