github.com/aloncn/graphics-go@v0.0.1/src/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  // +build cgo
     6  
     7  #define WIN32_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  /* 1MB is default stack size for 32-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 (1*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  		"movl %0, %%fs:0x14\n"	// MOVL tls0, 0x14(FS)
    57  		"movl %%fs:0x14, %%eax\n"	// MOVL 0x14(FS), tmp
    58  		"movl %1, 0(%%eax)\n"	// MOVL g, 0(FS)
    59  		:: "r"(ts.tls), "r"(ts.g) : "%eax"
    60  	);
    61  	
    62  	crosscall_386(ts.fn);
    63  }