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