github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_linux_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  // +build cgo
     6  
     7  #include <pthread.h>
     8  #include <string.h>
     9  #include <signal.h>
    10  #include "libcgo.h"
    11  
    12  static void *threadentry(void*);
    13  static void (*setg_gcc)(void*);
    14  
    15  // These will be set in gcc_android_386.c for android-specific customization.
    16  void (*x_cgo_inittls)(void);
    17  void* (*x_cgo_threadentry)(void*);
    18  
    19  void
    20  x_cgo_init(G *g, void (*setg)(void*))
    21  {
    22  	pthread_attr_t attr;
    23  	size_t size;
    24  
    25  	setg_gcc = setg;
    26  	pthread_attr_init(&attr);
    27  	pthread_attr_getstacksize(&attr, &size);
    28  	g->stacklo = (uintptr)&attr - size + 4096;
    29  	pthread_attr_destroy(&attr);
    30  
    31  	if (x_cgo_inittls) {
    32  		x_cgo_inittls();
    33  	}
    34  }
    35  
    36  
    37  void
    38  _cgo_sys_thread_start(ThreadStart *ts)
    39  {
    40  	pthread_attr_t attr;
    41  	sigset_t ign, oset;
    42  	pthread_t p;
    43  	size_t size;
    44  	int err;
    45  
    46  	sigfillset(&ign);
    47  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    48  
    49  	// Not sure why the memset is necessary here,
    50  	// but without it, we get a bogus stack size
    51  	// out of pthread_attr_getstacksize.  C'est la Linux.
    52  	memset(&attr, 0, sizeof attr);
    53  	pthread_attr_init(&attr);
    54  	size = 0;
    55  	pthread_attr_getstacksize(&attr, &size);
    56  	// Leave stacklo=0 and set stackhi=size; mstack will do the rest.
    57  	ts->g->stackhi = size;
    58  	err = pthread_create(&p, &attr, threadentry, ts);
    59  
    60  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    61  
    62  	if (err != 0) {
    63  		fatalf("pthread_create failed: %s", strerror(err));
    64  	}
    65  }
    66  
    67  static void*
    68  threadentry(void *v)
    69  {
    70  	if (x_cgo_threadentry) {
    71  		return x_cgo_threadentry(v);
    72  	}
    73  
    74  	ThreadStart ts;
    75  
    76  	ts = *(ThreadStart*)v;
    77  	free(v);
    78  
    79  	/*
    80  	 * Set specific keys.
    81  	 */
    82  	setg_gcc((void*)ts.g);
    83  
    84  	crosscall_386(ts.fn);
    85  	return nil;
    86  }