github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_linux_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 <pthread.h>
     8  #include <errno.h>
     9  #include <string.h> // strerror
    10  #include <signal.h>
    11  #include <stdlib.h>
    12  #include "libcgo.h"
    13  
    14  static void* threadentry(void*);
    15  static void (*setg_gcc)(void*);
    16  
    17  // These will be set in gcc_android_amd64.c for android-specific customization.
    18  void (*x_cgo_inittls)(void);
    19  void* (*x_cgo_threadentry)(void*);
    20  
    21  void
    22  x_cgo_init(G* g, void (*setg)(void*))
    23  {
    24  	pthread_attr_t *attr;
    25  	size_t size;
    26  
    27  	/* The memory sanitizer distributed with versions of clang
    28  	   before 3.8 has a bug: if you call mmap before malloc, mmap
    29  	   may return an address that is later overwritten by the msan
    30  	   library.  Avoid this problem by forcing a call to malloc
    31  	   here, before we ever call malloc.
    32  
    33  	   This is only required for the memory sanitizer, so it's
    34  	   unfortunate that we always run it.  It should be possible
    35  	   to remove this when we no longer care about versions of
    36  	   clang before 3.8.  The test for this is
    37  	   misc/cgo/testsanitizers.
    38  
    39  	   GCC works hard to eliminate a seemingly unnecessary call to
    40  	   malloc, so we actually use the memory we allocate.  */
    41  
    42  	setg_gcc = setg;
    43  	attr = (pthread_attr_t*)malloc(sizeof *attr);
    44  	if (attr == NULL) {
    45  		fatalf("malloc failed: %s", strerror(errno));
    46  	}
    47  	pthread_attr_init(attr);
    48  	pthread_attr_getstacksize(attr, &size);
    49  	g->stacklo = (uintptr)&size - size + 4096;
    50  	pthread_attr_destroy(attr);
    51  	free(attr);
    52  
    53  	if (x_cgo_inittls) {
    54  		x_cgo_inittls();
    55  	}
    56  }
    57  
    58  
    59  void
    60  _cgo_sys_thread_start(ThreadStart *ts)
    61  {
    62  	pthread_attr_t attr;
    63  	sigset_t ign, oset;
    64  	pthread_t p;
    65  	size_t size;
    66  	int err;
    67  
    68  	sigfillset(&ign);
    69  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    70  
    71  	pthread_attr_init(&attr);
    72  	pthread_attr_getstacksize(&attr, &size);
    73  	// Leave stacklo=0 and set stackhi=size; mstack will do the rest.
    74  	ts->g->stackhi = size;
    75  	err = pthread_create(&p, &attr, threadentry, ts);
    76  
    77  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    78  
    79  	if (err != 0) {
    80  		fatalf("pthread_create failed: %s", strerror(err));
    81  	}
    82  }
    83  
    84  static void*
    85  threadentry(void *v)
    86  {
    87  	if (x_cgo_threadentry) {
    88  		return x_cgo_threadentry(v);
    89  	}
    90  
    91  	ThreadStart ts;
    92  
    93  	ts = *(ThreadStart*)v;
    94  	free(v);
    95  
    96  	/*
    97  	 * Set specific keys.
    98  	 */
    99  	setg_gcc((void*)ts.g);
   100  
   101  	crosscall_amd64(ts.fn);
   102  	return nil;
   103  }