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 uintptr_t x_cgo_pthread_key_created;
34 void (*x_crosscall2_ptr)(void (*fn)(void *), void *, int, size_t);
36 // Pre-initialize the runtime synchronization objects
39 runtime_init_wait = CreateEvent(NULL, TRUE, FALSE, NULL);
40 if (runtime_init_wait == NULL) {
41 fprintf(stderr, "runtime: failed to create runtime initialization wait event.\n");
45 InitializeCriticalSection(&runtime_init_cs);
48 // Make sure that the preinit sequence has run.
50 _cgo_maybe_run_preinit() {
51 if (!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
52 if (InterlockedIncrement(&runtime_init_once_gate) == 1) {
54 InterlockedIncrement(&runtime_init_once_done);
56 // Decrement to avoid overflow.
57 InterlockedDecrement(&runtime_init_once_gate);
58 while(!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
66 x_cgo_sys_thread_create(void (*func)(void*), void* arg) {
67 _cgo_beginthread(func, arg);
71 _cgo_is_runtime_initialized() {
72 EnterCriticalSection(&runtime_init_cs);
73 int status = runtime_init_done;
74 LeaveCriticalSection(&runtime_init_cs);
79 _cgo_wait_runtime_init_done(void) {
80 void (*pfn)(struct context_arg*);
82 _cgo_maybe_run_preinit();
83 while (!_cgo_is_runtime_initialized()) {
84 WaitForSingleObject(runtime_init_wait, INFINITE);
86 pfn = _cgo_get_context_function();
88 struct context_arg arg;
97 // Should not be used since x_cgo_pthread_key_created will always be zero.
98 void x_cgo_bindm(void* dummy) {
99 fprintf(stderr, "unexpected cgo_bindm on Windows\n");
104 x_cgo_notify_runtime_init_done(void* dummy) {
105 _cgo_maybe_run_preinit();
107 EnterCriticalSection(&runtime_init_cs);
108 runtime_init_done = 1;
109 LeaveCriticalSection(&runtime_init_cs);
111 if (!SetEvent(runtime_init_wait)) {
112 fprintf(stderr, "runtime: failed to signal runtime initialization complete.\n");
117 // The context function, used when tracing back C calls into Go.
118 static void (*cgo_context_function)(struct context_arg*);
120 // Sets the context function to call to record the traceback context
121 // when calling a Go function from C code. Called from runtime.SetCgoTraceback.
122 void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
123 EnterCriticalSection(&runtime_init_cs);
124 cgo_context_function = context;
125 LeaveCriticalSection(&runtime_init_cs);
128 // Gets the context function.
129 void (*(_cgo_get_context_function(void)))(struct context_arg*) {
130 void (*ret)(struct context_arg*);
132 EnterCriticalSection(&runtime_init_cs);
133 ret = cgo_context_function;
134 LeaveCriticalSection(&runtime_init_cs);
138 void _cgo_beginthread(void (*func)(void*), void* arg) {
142 for (tries = 0; tries < 20; tries++) {
143 thandle = _beginthread(func, 0, arg);
144 if (thandle == -1 && errno == EACCES) {
145 // "Insufficient resources", try again in a bit.
147 // Note that the first Sleep(0) is a yield.
148 Sleep(tries); // milliseconds
150 } else if (thandle == -1) {
156 fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno);