thread_windows.c 11 KB
Newer Older
Hector Chu's avatar
Hector Chu committed
1 2 3 4 5
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "runtime.h"
6
#include "type.h"
Russ Cox's avatar
Russ Cox committed
7 8
#include "defs_GOOS_GOARCH.h"
#include "os_GOOS.h"
Hector Chu's avatar
Hector Chu committed
9

10
#pragma dynimport runtime·CloseHandle CloseHandle "kernel32.dll"
11 12
#pragma dynimport runtime·CreateEvent CreateEventA "kernel32.dll"
#pragma dynimport runtime·CreateThread CreateThread "kernel32.dll"
13 14
#pragma dynimport runtime·CreateWaitableTimer CreateWaitableTimerA "kernel32.dll"
#pragma dynimport runtime·DuplicateHandle DuplicateHandle "kernel32.dll"
15
#pragma dynimport runtime·ExitProcess ExitProcess "kernel32.dll"
16 17 18
#pragma dynimport runtime·FreeEnvironmentStringsW FreeEnvironmentStringsW "kernel32.dll"
#pragma dynimport runtime·GetEnvironmentStringsW GetEnvironmentStringsW "kernel32.dll"
#pragma dynimport runtime·GetProcAddress GetProcAddress "kernel32.dll"
19
#pragma dynimport runtime·GetStdHandle GetStdHandle "kernel32.dll"
Hector Chu's avatar
Hector Chu committed
20
#pragma dynimport runtime·GetSystemInfo GetSystemInfo "kernel32.dll"
Russ Cox's avatar
Russ Cox committed
21
#pragma dynimport runtime·GetSystemTimeAsFileTime GetSystemTimeAsFileTime "kernel32.dll"
22
#pragma dynimport runtime·GetThreadContext GetThreadContext "kernel32.dll"
23
#pragma dynimport runtime·LoadLibrary LoadLibraryW "kernel32.dll"
24
#pragma dynimport runtime·ResumeThread ResumeThread "kernel32.dll"
25
#pragma dynimport runtime·SetConsoleCtrlHandler SetConsoleCtrlHandler "kernel32.dll"
26
#pragma dynimport runtime·SetEvent SetEvent "kernel32.dll"
27 28
#pragma dynimport runtime·SetThreadPriority SetThreadPriority "kernel32.dll"
#pragma dynimport runtime·SetWaitableTimer SetWaitableTimer "kernel32.dll"
Hector Chu's avatar
Hector Chu committed
29
#pragma dynimport runtime·Sleep Sleep "kernel32.dll"
30 31
#pragma dynimport runtime·SuspendThread SuspendThread "kernel32.dll"
#pragma dynimport runtime·timeBeginPeriod timeBeginPeriod "winmm.dll"
32
#pragma dynimport runtime·WaitForSingleObject WaitForSingleObject "kernel32.dll"
33
#pragma dynimport runtime·WriteFile WriteFile "kernel32.dll"
Hector Chu's avatar
Hector Chu committed
34

Wei Guangjing's avatar
Wei Guangjing committed
35
extern void *runtime·CloseHandle;
36 37
extern void *runtime·CreateEvent;
extern void *runtime·CreateThread;
38 39
extern void *runtime·CreateWaitableTimer;
extern void *runtime·DuplicateHandle;
Wei Guangjing's avatar
Wei Guangjing committed
40
extern void *runtime·ExitProcess;
41 42 43
extern void *runtime·FreeEnvironmentStringsW;
extern void *runtime·GetEnvironmentStringsW;
extern void *runtime·GetProcAddress;
Wei Guangjing's avatar
Wei Guangjing committed
44
extern void *runtime·GetStdHandle;
Hector Chu's avatar
Hector Chu committed
45
extern void *runtime·GetSystemInfo;
Russ Cox's avatar
Russ Cox committed
46
extern void *runtime·GetSystemTimeAsFileTime;
47
extern void *runtime·GetThreadContext;
48
extern void *runtime·LoadLibrary;
49
extern void *runtime·ResumeThread;
50
extern void *runtime·SetConsoleCtrlHandler;
Wei Guangjing's avatar
Wei Guangjing committed
51
extern void *runtime·SetEvent;
52 53
extern void *runtime·SetThreadPriority;
extern void *runtime·SetWaitableTimer;
Hector Chu's avatar
Hector Chu committed
54
extern void *runtime·Sleep;
55 56
extern void *runtime·SuspendThread;
extern void *runtime·timeBeginPeriod;
57
extern void *runtime·WaitForSingleObject;
Wei Guangjing's avatar
Wei Guangjing committed
58
extern void *runtime·WriteFile;
Hector Chu's avatar
Hector Chu committed
59

Hector Chu's avatar
Hector Chu committed
60 61 62 63 64 65 66 67 68
static int32
getproccount(void)
{
	SystemInfo info;

	runtime·stdcall(runtime·GetSystemInfo, 1, &info);
	return info.dwNumberOfProcessors;
}

Hector Chu's avatar
Hector Chu committed
69
void
70
runtime·osinit(void)
Hector Chu's avatar
Hector Chu committed
71
{
72 73 74 75
	// -1 = current process, -2 = current thread
	runtime·stdcall(runtime·DuplicateHandle, 7,
		(uintptr)-1, (uintptr)-2, (uintptr)-1, &m->thread,
		(uintptr)0, (uintptr)0, (uintptr)DUPLICATE_SAME_ACCESS);
76
	runtime·stdcall(runtime·SetConsoleCtrlHandler, 2, runtime·ctrlhandler, (uintptr)1);
77
	runtime·stdcall(runtime·timeBeginPeriod, 1, (uintptr)1);
Hector Chu's avatar
Hector Chu committed
78
	runtime·ncpu = getproccount();
Hector Chu's avatar
Hector Chu committed
79 80
}

81 82 83
void
runtime·goenvs(void)
{
84
	extern Slice syscall·envs;
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	uint16 *env;
	String *s;
	int32 i, n;
	uint16 *p;

	env = runtime·stdcall(runtime·GetEnvironmentStringsW, 0);

	n = 0;
	for(p=env; *p; n++)
		p += runtime·findnullw(p)+1;

	s = runtime·malloc(n*sizeof s[0]);

	p = env;
	for(i=0; i<n; i++) {
		s[i] = runtime·gostringw(p);
		p += runtime·findnullw(p)+1;
	}
104 105 106
	syscall·envs.array = (byte*)s;
	syscall·envs.len = n;
	syscall·envs.cap = n;
107 108 109 110

	runtime·stdcall(runtime·FreeEnvironmentStringsW, 1, env);
}

Hector Chu's avatar
Hector Chu committed
111
void
112
runtime·exit(int32 code)
Hector Chu's avatar
Hector Chu committed
113
{
114
	runtime·stdcall(runtime·ExitProcess, 1, (uintptr)code);
Hector Chu's avatar
Hector Chu committed
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
int32
runtime·write(int32 fd, void *buf, int32 n)
{
	void *handle;
	uint32 written;

	written = 0;
	switch(fd) {
	case 1:
		handle = runtime·stdcall(runtime·GetStdHandle, 1, (uintptr)-11);
		break;
	case 2:
		handle = runtime·stdcall(runtime·GetStdHandle, 1, (uintptr)-12);
		break;
	default:
		return -1;
	}
	runtime·stdcall(runtime·WriteFile, 5, handle, buf, (uintptr)n, &written, (uintptr)0);
	return written;
}

138
#pragma textflag 7
Hector Chu's avatar
Hector Chu committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
void
runtime·osyield(void)
{
	runtime·stdcall(runtime·Sleep, 1, (uintptr)0);
}

void
runtime·usleep(uint32 us)
{
	us /= 1000;
	if(us == 0)
		us = 1;
	runtime·stdcall(runtime·Sleep, 1, (uintptr)us);
}

154 155 156 157
#define INFINITE ((uintptr)0xFFFFFFFF)

int32
runtime·semasleep(int64 ns)
Hector Chu's avatar
Hector Chu committed
158
{
159 160 161 162 163 164 165 166 167 168 169 170 171 172
	uintptr ms;

	if(ns < 0)
		ms = INFINITE;
	else if(ns/1000000 > 0x7fffffffLL)
		ms = 0x7fffffff;
	else {
		ms = ns/1000000;
		if(ms == 0)
			ms = 1;
	}
	if(runtime·stdcall(runtime·WaitForSingleObject, 2, m->waitsema, ms) != 0)
		return -1;  // timeout
	return 0;
Hector Chu's avatar
Hector Chu committed
173 174 175
}

void
176
runtime·semawakeup(M *mp)
Hector Chu's avatar
Hector Chu committed
177
{
178
	runtime·stdcall(runtime·SetEvent, 1, mp->waitsema);
Hector Chu's avatar
Hector Chu committed
179 180
}

181 182
uintptr
runtime·semacreate(void)
Hector Chu's avatar
Hector Chu committed
183
{
184
	return (uintptr)runtime·stdcall(runtime·CreateEvent, 4, (uintptr)0, (uintptr)0, (uintptr)0, (uintptr)0);
Hector Chu's avatar
Hector Chu committed
185 186
}

187 188
#define STACK_SIZE_PARAM_IS_A_RESERVATION ((uintptr)0x00010000)

Hector Chu's avatar
Hector Chu committed
189
void
190
runtime·newosproc(M *mp, G *gp, void *stk, void (*fn)(void))
Hector Chu's avatar
Hector Chu committed
191
{
192 193
	void *thandle;

194
	USED(stk);
195 196 197 198 199

	// assume gp == mp->g0
	if(gp != mp->g0)
		runtime·throw("invalid newosproc gp");

Russ Cox's avatar
Russ Cox committed
200
	mp->mstartfn = fn;
201

202
	thandle = runtime·stdcall(runtime·CreateThread, 6,
203
		nil, (uintptr)0x20000, runtime·tstart_stdcall, mp,
204
		STACK_SIZE_PARAM_IS_A_RESERVATION, nil);
205
	if(thandle == nil) {
206 207 208
		runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), runtime·getlasterror());
		runtime·throw("runtime.newosproc");
	}
209
	runtime·atomicstorep(&mp->thread, thandle);
Hector Chu's avatar
Hector Chu committed
210 211 212
}

// Called to initialize a new m (including the bootstrap m).
213 214 215 216 217 218 219 220 221
// Called on the parent thread (main thread in case of bootstrap), can allocate memory.
void
runtime·mpreinit(M *mp)
{
	USED(mp);
}

// Called to initialize a new m (including the bootstrap m).
// Called on the new thread, can not allocate memory.
Hector Chu's avatar
Hector Chu committed
222
void
223
runtime·minit(void)
Hector Chu's avatar
Hector Chu committed
224
{
225
	runtime·install_exception_handler();
Hector Chu's avatar
Hector Chu committed
226
}
227

228 229 230 231 232 233 234
// Called from dropm to undo the effect of an minit.
void
runtime·unminit(void)
{
	runtime·remove_exception_handler();
}

Russ Cox's avatar
Russ Cox committed
235 236
int64
runtime·nanotime(void)
237
{
Russ Cox's avatar
Russ Cox committed
238
	int64 filetime;
239

Russ Cox's avatar
Russ Cox committed
240
	runtime·stdcall(runtime·GetSystemTimeAsFileTime, 1, &filetime);
241

Russ Cox's avatar
Russ Cox committed
242 243 244
	// Filetime is 100s of nanoseconds since January 1, 1601.
	// Convert to nanoseconds since January 1, 1970.
	return (filetime - 116444736000000000LL) * 100LL;
245 246
}

247 248 249 250
void
time·now(int64 sec, int32 usec)
{
	int64 ns;
251

252 253 254 255 256 257 258
	ns = runtime·nanotime();
	sec = ns / 1000000000LL;
	usec = ns - sec * 1000000000LL;
	FLUSH(&sec);
	FLUSH(&usec);
}

259 260 261
// Calling stdcall on os stack.
#pragma textflag 7
void *
262
runtime·stdcall(void *fn, int32 count, ...)
263
{
264 265 266 267 268 269
	WinCall c;

	c.fn = fn;
	c.n = count;
	c.args = (uintptr*)&count + 1;
	runtime·asmcgocall(runtime·asmstdcall, &c);
270
	return (void*)c.r1;
271
}
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

uint32
runtime·issigpanic(uint32 code)
{
	switch(code) {
	case EXCEPTION_ACCESS_VIOLATION:
	case EXCEPTION_INT_DIVIDE_BY_ZERO:
	case EXCEPTION_INT_OVERFLOW:
	case EXCEPTION_FLT_DENORMAL_OPERAND:
	case EXCEPTION_FLT_DIVIDE_BY_ZERO:
	case EXCEPTION_FLT_INEXACT_RESULT:
	case EXCEPTION_FLT_OVERFLOW:
	case EXCEPTION_FLT_UNDERFLOW:
		return 1;
	}
	return 0;
}

void
runtime·sigpanic(void)
{
	switch(g->sig) {
	case EXCEPTION_ACCESS_VIOLATION:
295 296 297
		if(g->sigcode1 < 0x1000) {
			if(g->sigpc == 0)
				runtime·panicstring("call of nil func value");
298
			runtime·panicstring("invalid memory address or nil pointer dereference");
299
		}
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
		runtime·printf("unexpected fault address %p\n", g->sigcode1);
		runtime·throw("fault");
	case EXCEPTION_INT_DIVIDE_BY_ZERO:
		runtime·panicstring("integer divide by zero");
	case EXCEPTION_INT_OVERFLOW:
		runtime·panicstring("integer overflow");
	case EXCEPTION_FLT_DENORMAL_OPERAND:
	case EXCEPTION_FLT_DIVIDE_BY_ZERO:
	case EXCEPTION_FLT_INEXACT_RESULT:
	case EXCEPTION_FLT_OVERFLOW:
	case EXCEPTION_FLT_UNDERFLOW:
		runtime·panicstring("floating point error");
	}
	runtime·throw("fault");
}
315

316 317 318 319 320 321 322 323 324 325 326
extern void *runtime·sigtramp;

void
runtime·initsig(void)
{
	// following line keeps sigtramp alive at link stage
	// if there's a better way please write it here
	void *p = runtime·sigtramp;
	USED(p);
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
uint32
runtime·ctrlhandler1(uint32 type)
{
	int32 s;

	switch(type) {
	case CTRL_C_EVENT:
	case CTRL_BREAK_EVENT:
		s = SIGINT;
		break;
	default:
		return 0;
	}

	if(runtime·sigsend(s))
		return 1;
	runtime·exit(2);	// SIGINT, SIGTERM, etc
	return 0;
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
extern void runtime·dosigprof(Context *r, G *gp);
extern void runtime·profileloop(void);
static void *profiletimer;

static void
profilem(M *mp)
{
	extern M runtime·m0;
	extern uint32 runtime·tls0[];
	byte rbuf[sizeof(Context)+15];
	Context *r;
	void *tls;
	G *gp;

	tls = mp->tls;
	if(mp == &runtime·m0)
		tls = runtime·tls0;
	gp = *(G**)tls;

	if(gp != nil && gp != mp->g0 && gp->status != Gsyscall) {
		// align Context to 16 bytes
		r = (Context*)((uintptr)(&rbuf[15]) & ~15);
		r->ContextFlags = CONTEXT_CONTROL;
		runtime·stdcall(runtime·GetThreadContext, 2, mp->thread, r);
		runtime·dosigprof(r, gp);
	}
}

void
runtime·profileloop1(void)
{
	M *mp, *allm;
	void *thread;

	runtime·stdcall(runtime·SetThreadPriority, 2,
		(uintptr)-2, (uintptr)THREAD_PRIORITY_HIGHEST);

	for(;;) {
		runtime·stdcall(runtime·WaitForSingleObject, 2, profiletimer, (uintptr)-1);
		allm = runtime·atomicloadp(&runtime·allm);
		for(mp = allm; mp != nil; mp = mp->alllink) {
			thread = runtime·atomicloadp(&mp->thread);
			if(thread == nil)
				continue;
			runtime·stdcall(runtime·SuspendThread, 1, thread);
			if(mp->profilehz != 0)
				profilem(mp);
			runtime·stdcall(runtime·ResumeThread, 1, thread);
		}
	}
}

void
runtime·resetcpuprofiler(int32 hz)
{
	static Lock lock;
	void *timer, *thread;
	int32 ms;
	int64 due;

	runtime·lock(&lock);
	if(profiletimer == nil) {
		timer = runtime·stdcall(runtime·CreateWaitableTimer, 3, nil, nil, nil);
		runtime·atomicstorep(&profiletimer, timer);
		thread = runtime·stdcall(runtime·CreateThread, 6,
			nil, nil, runtime·profileloop, nil, nil, nil);
		runtime·stdcall(runtime·CloseHandle, 1, thread);
	}
	runtime·unlock(&lock);

	ms = 0;
	due = 1LL<<63;
	if(hz > 0) {
		ms = 1000 / hz;
		if(ms == 0)
			ms = 1;
		due = ms * -10000;
	}
	runtime·stdcall(runtime·SetWaitableTimer, 6,
		profiletimer, &due, (uintptr)ms, nil, nil, nil);
	runtime·atomicstore((uint32*)&m->profilehz, hz);
}

430 431 432 433 434
void
os·sigpipe(void)
{
	runtime·throw("too many writes on closed pipe");
}
435 436 437 438 439 440

uintptr
runtime·memlimit(void)
{
	return 0;
}
441 442 443 444 445 446

void
runtime·setprof(bool on)
{
	USED(on);
}
447

Russ Cox's avatar
Russ Cox committed
448 449
int8 runtime·badcallbackmsg[] = "runtime: cgo callback on thread not created by Go.\n";
int32 runtime·badcallbacklen = sizeof runtime·badcallbackmsg - 1;
450 451 452

int8 runtime·badsignalmsg[] = "runtime: signal received on thread not created by Go.\n";
int32 runtime·badsignallen = sizeof runtime·badsignalmsg - 1;