Text file src/runtime/cgo/gcc_unix.c

     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  //go:build unix
     6  
     7  #include "libcgo.h"
     8  #include "libcgo_unix.h"
     9  
    10  // Platform-specific hooks.
    11  void (*x_cgo_inittls)(void **tlsg, void **tlsbase) __attribute__((weak));
    12  void (*x_cgo_init_platform)(void) __attribute__((weak));
    13  void (*x_cgo_threadentry_platform)(void) __attribute__((weak));
    14  
    15  static void (*setg_gcc)(void*);
    16  
    17  // _cgo_set_stacklo sets g->stacklo based on the stack size.
    18  // This is common code called from x_cgo_init, which is itself
    19  // called by rt0_go in the runtime package.
    20  static void
    21  _cgo_set_stacklo(G *g)
    22  {
    23  	uintptr bounds[2];
    24  
    25  	x_cgo_getstackbound(bounds);
    26  
    27  	g->stacklo = bounds[0];
    28  
    29  	// Sanity check the results now, rather than getting a
    30  	// morestack on g0 crash.
    31  	if (g->stacklo >= g->stackhi) {
    32  		fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi));
    33  		abort();
    34  	}
    35  }
    36  
    37  void
    38  x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    39  {
    40  	setg_gcc = setg;
    41  	_cgo_set_stacklo(g);
    42  
    43  	if (x_cgo_inittls) {
    44  		x_cgo_inittls(tlsg, tlsbase);
    45  	}
    46  	if (x_cgo_init_platform) {
    47  		x_cgo_init_platform();
    48  	}
    49  }
    50  
    51  void*
    52  threadentry(void *v)
    53  {
    54  	ThreadStart ts;
    55  
    56  	ts = *(ThreadStart*)v;
    57  	_cgo_tsan_acquire();
    58  	free(v);
    59  	_cgo_tsan_release();
    60  
    61  	if (x_cgo_threadentry_platform != NULL) {
    62  		x_cgo_threadentry_platform();
    63  	}
    64  
    65  	crosscall1(ts.fn, setg_gcc, ts.g);
    66  	return NULL;
    67  }
    68  

View as plain text