github.com/aloncn/graphics-go@v0.0.1/src/runtime/cgo/gcc_windows_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 #define WIN64_LEAN_AND_MEAN 8 #include <windows.h> 9 #include <process.h> 10 #include <stdlib.h> 11 #include <stdio.h> 12 #include "libcgo.h" 13 14 static void threadentry(void*); 15 16 /* 2MB is default stack size for 64-bit Windows. 17 Allocation granularity on Windows is typically 64 KB. 18 The constant is also hardcoded in cmd/ld/pe.c (keep synchronized). */ 19 #define STACKSIZE (2*1024*1024) 20 21 void 22 x_cgo_init(G *g) 23 { 24 int tmp; 25 g->stacklo = (uintptr)&tmp - STACKSIZE + 8*1024; 26 } 27 28 29 void 30 _cgo_sys_thread_start(ThreadStart *ts) 31 { 32 uintptr_t thandle; 33 34 thandle = _beginthread(threadentry, 0, ts); 35 if(thandle == -1) { 36 fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno); 37 abort(); 38 } 39 } 40 41 static void 42 threadentry(void *v) 43 { 44 ThreadStart ts; 45 46 ts = *(ThreadStart*)v; 47 free(v); 48 49 ts.g->stackhi = (uintptr)&ts; 50 ts.g->stacklo = (uintptr)&ts - STACKSIZE + 8*1024; 51 52 /* 53 * Set specific keys in thread local storage. 54 */ 55 asm volatile ( 56 "movq %0, %%gs:0x28\n" // MOVL tls0, 0x28(GS) 57 "movq %%gs:0x28, %%rax\n" // MOVQ 0x28(GS), tmp 58 "movq %1, 0(%%rax)\n" // MOVQ g, 0(GS) 59 :: "r"(ts.tls), "r"(ts.g) : "%rax" 60 ); 61 62 crosscall_amd64(ts.fn); 63 }