github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/runtime/cgo/gcc_libinit_windows.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  #define WIN32_LEAN_AND_MEAN
     8  #include <windows.h>
     9  #include <process.h>
    10  
    11  #include <stdio.h>
    12  #include <stdlib.h>
    13  #include <errno.h>
    14  
    15  #include "libcgo.h"
    16  #include "libcgo_windows.h"
    17  
    18  // Ensure there's one symbol marked __declspec(dllexport).
    19  // If there are no exported symbols, the unfortunate behavior of
    20  // the binutils linker is to also strip the relocations table,
    21  // resulting in non-PIE binary. The other option is the
    22  // --export-all-symbols flag, but we don't need to export all symbols
    23  // and this may overflow the export table (#40795).
    24  // See https://sourceware.org/bugzilla/show_bug.cgi?id=19011
    25  __declspec(dllexport) int _cgo_dummy_export;
    26  
    27  static volatile LONG runtime_init_once_gate = 0;
    28  static volatile LONG runtime_init_once_done = 0;
    29  
    30  static CRITICAL_SECTION runtime_init_cs;
    31  
    32  static HANDLE runtime_init_wait;
    33  static int runtime_init_done;
    34  
    35  // Pre-initialize the runtime synchronization objects
    36  void
    37  _cgo_preinit_init() {
    38  	 runtime_init_wait = CreateEvent(NULL, TRUE, FALSE, NULL);
    39  	 if (runtime_init_wait == NULL) {
    40  		fprintf(stderr, "runtime: failed to create runtime initialization wait event.\n");
    41  		abort();
    42  	 }
    43  
    44  	 InitializeCriticalSection(&runtime_init_cs);
    45  }
    46  
    47  // Make sure that the preinit sequence has run.
    48  void
    49  _cgo_maybe_run_preinit() {
    50  	 if (!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
    51  			if (InterlockedIncrement(&runtime_init_once_gate) == 1) {
    52  				 _cgo_preinit_init();
    53  				 InterlockedIncrement(&runtime_init_once_done);
    54  			} else {
    55  				 // Decrement to avoid overflow.
    56  				 InterlockedDecrement(&runtime_init_once_gate);
    57  				 while(!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
    58  						Sleep(0);
    59  				 }
    60  			}
    61  	 }
    62  }
    63  
    64  void
    65  x_cgo_sys_thread_create(void (*func)(void*), void* arg) {
    66  	_cgo_beginthread(func, arg);
    67  }
    68  
    69  int
    70  _cgo_is_runtime_initialized() {
    71  	 EnterCriticalSection(&runtime_init_cs);
    72  	 int status = runtime_init_done;
    73  	 LeaveCriticalSection(&runtime_init_cs);
    74  	 return status;
    75  }
    76  
    77  uintptr_t
    78  _cgo_wait_runtime_init_done(void) {
    79  	void (*pfn)(struct context_arg*);
    80  
    81  	 _cgo_maybe_run_preinit();
    82  	while (!_cgo_is_runtime_initialized()) {
    83  			WaitForSingleObject(runtime_init_wait, INFINITE);
    84  	}
    85  	pfn = _cgo_get_context_function();
    86  	if (pfn != nil) {
    87  		struct context_arg arg;
    88  
    89  		arg.Context = 0;
    90  		(*pfn)(&arg);
    91  		return arg.Context;
    92  	}
    93  	return 0;
    94  }
    95  
    96  void
    97  x_cgo_notify_runtime_init_done(void* dummy) {
    98  	 _cgo_maybe_run_preinit();
    99  
   100  	 EnterCriticalSection(&runtime_init_cs);
   101  	runtime_init_done = 1;
   102  	 LeaveCriticalSection(&runtime_init_cs);
   103  
   104  	 if (!SetEvent(runtime_init_wait)) {
   105  		fprintf(stderr, "runtime: failed to signal runtime initialization complete.\n");
   106  		abort();
   107  	}
   108  }
   109  
   110  // The context function, used when tracing back C calls into Go.
   111  static void (*cgo_context_function)(struct context_arg*);
   112  
   113  // Sets the context function to call to record the traceback context
   114  // when calling a Go function from C code. Called from runtime.SetCgoTraceback.
   115  void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
   116  	EnterCriticalSection(&runtime_init_cs);
   117  	cgo_context_function = context;
   118  	LeaveCriticalSection(&runtime_init_cs);
   119  }
   120  
   121  // Gets the context function.
   122  void (*(_cgo_get_context_function(void)))(struct context_arg*) {
   123  	void (*ret)(struct context_arg*);
   124  
   125  	EnterCriticalSection(&runtime_init_cs);
   126  	ret = cgo_context_function;
   127  	LeaveCriticalSection(&runtime_init_cs);
   128  	return ret;
   129  }
   130  
   131  void _cgo_beginthread(void (*func)(void*), void* arg) {
   132  	int tries;
   133  	uintptr_t thandle;
   134  
   135  	for (tries = 0; tries < 20; tries++) {
   136  		thandle = _beginthread(func, 0, arg);
   137  		if (thandle == -1 && errno == EACCES) {
   138  			// "Insufficient resources", try again in a bit.
   139  			//
   140  			// Note that the first Sleep(0) is a yield.
   141  			Sleep(tries); // milliseconds
   142  			continue;
   143  		} else if (thandle == -1) {
   144  			break;
   145  		}
   146  		return; // Success!
   147  	}
   148  
   149  	fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno);
   150  	abort();
   151  }