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.
5 #define WIN32_LEAN_AND_MEAN
14 #include "libcgo_windows.h"
16 // Ensure there's one symbol marked __declspec(dllexport).
17 // If there are no exported symbols, the unfortunate behavior of
18 // the binutils linker is to also strip the relocations table,
19 // resulting in non-PIE binary. The other option is the
20 // --export-all-symbols flag, but we don't need to export all symbols
21 // and this may overflow the export table (#40795).
22 // See https://sourceware.org/bugzilla/show_bug.cgi?id=19011
23 __declspec(dllexport) int _cgo_dummy_export;
25 static volatile LONG runtime_init_once_gate = 0;
26 static volatile LONG runtime_init_once_done = 0;
28 static CRITICAL_SECTION runtime_init_cs;
30 static HANDLE runtime_init_wait;
31 static int runtime_init_done;
33 // Pre-initialize the runtime synchronization objects
36 runtime_init_wait = CreateEvent(NULL, TRUE, FALSE, NULL);
37 if (runtime_init_wait == NULL) {
38 fprintf(stderr, "runtime: failed to create runtime initialization wait event.\n");
42 InitializeCriticalSection(&runtime_init_cs);
45 // Make sure that the preinit sequence has run.
47 _cgo_maybe_run_preinit() {
48 if (!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
49 if (InterlockedIncrement(&runtime_init_once_gate) == 1) {
51 InterlockedIncrement(&runtime_init_once_done);
53 // Decrement to avoid overflow.
54 InterlockedDecrement(&runtime_init_once_gate);
55 while(!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
63 x_cgo_sys_thread_create(void (*func)(void*), void* arg) {
64 _cgo_beginthread(func, arg);
68 _cgo_is_runtime_initialized() {
69 EnterCriticalSection(&runtime_init_cs);
70 int status = runtime_init_done;
71 LeaveCriticalSection(&runtime_init_cs);
76 _cgo_wait_runtime_init_done(void) {
77 void (*pfn)(struct context_arg*);
79 _cgo_maybe_run_preinit();
80 while (!_cgo_is_runtime_initialized()) {
81 WaitForSingleObject(runtime_init_wait, INFINITE);
83 pfn = _cgo_get_context_function();
85 struct context_arg arg;
95 x_cgo_notify_runtime_init_done(void* dummy) {
96 _cgo_maybe_run_preinit();
98 EnterCriticalSection(&runtime_init_cs);
99 runtime_init_done = 1;
100 LeaveCriticalSection(&runtime_init_cs);
102 if (!SetEvent(runtime_init_wait)) {
103 fprintf(stderr, "runtime: failed to signal runtime initialization complete.\n");
108 // The context function, used when tracing back C calls into Go.
109 static void (*cgo_context_function)(struct context_arg*);
111 // Sets the context function to call to record the traceback context
112 // when calling a Go function from C code. Called from runtime.SetCgoTraceback.
113 void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
114 EnterCriticalSection(&runtime_init_cs);
115 cgo_context_function = context;
116 LeaveCriticalSection(&runtime_init_cs);
119 // Gets the context function.
120 void (*(_cgo_get_context_function(void)))(struct context_arg*) {
121 void (*ret)(struct context_arg*);
123 EnterCriticalSection(&runtime_init_cs);
124 ret = cgo_context_function;
125 LeaveCriticalSection(&runtime_init_cs);
129 void _cgo_beginthread(void (*func)(void*), void* arg) {
133 for (tries = 0; tries < 20; tries++) {
134 thandle = _beginthread(func, 0, arg);
135 if (thandle == -1 && errno == EACCES) {
136 // "Insufficient resources", try again in a bit.
138 // Note that the first Sleep(0) is a yield.
139 Sleep(tries); // milliseconds
141 } else if (thandle == -1) {
147 fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno);