github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/cgo/gcc_windows_386.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  #define WIN32_LEAN_AND_MEAN
     6  #include <windows.h>
     7  #include <process.h>
     8  #include "libcgo.h"
     9  
    10  static void threadentry(void*);
    11  
    12  /* 1MB is default stack size for 32-bit Windows.
    13     Allocation granularity on Windows is typically 64 KB.
    14     The constant is also hardcoded in cmd/ld/pe.c (keep synchronized). */
    15  #define STACKSIZE (1*1024*1024)
    16  
    17  void
    18  x_cgo_init(G *g)
    19  {
    20  	int tmp;
    21  	g->stackguard = (uintptr)&tmp - STACKSIZE + 8*1024;
    22  }
    23  
    24  
    25  void
    26  _cgo_sys_thread_start(ThreadStart *ts)
    27  {
    28  	_beginthread(threadentry, 0, ts);
    29  }
    30  
    31  static void
    32  threadentry(void *v)
    33  {
    34  	ThreadStart ts;
    35  	void *tls0;
    36  
    37  	ts = *(ThreadStart*)v;
    38  	free(v);
    39  
    40  	ts.g->stackbase = (uintptr)&ts;
    41  	ts.g->stackguard = (uintptr)&ts - STACKSIZE + 8*1024;
    42  
    43  	/*
    44  	 * Set specific keys in thread local storage.
    45  	 */
    46  	tls0 = (void*)LocalAlloc(LPTR, 32);
    47  	asm volatile (
    48  		"movl %0, %%fs:0x14\n"	// MOVL tls0, 0x14(FS)
    49  		"movl %%fs:0x14, %%eax\n"	// MOVL 0x14(FS), tmp
    50  		"movl %1, 0(%%eax)\n"	// MOVL g, 0(FS)
    51  		"movl %2, 4(%%eax)\n"	// MOVL m, 4(FS)
    52  		:: "r"(tls0), "r"(ts.g), "r"(ts.m) : "%eax"
    53  	);
    54  	
    55  	crosscall_386(ts.fn);
    56  	
    57  	LocalFree(tls0);
    58  }