Skip to content

Commit 93f0599

Browse files
zx2c4dmitshur
authored andcommitted
[release-branch.go1.12] runtime: monitor for suspend/resume to kick timeouts
Starting in Windows 8, the wait functions don't take into account suspend time, even though the monotonic counters do. This results in timer buckets stalling on resume. Therefore, this commit makes it so that on resume, we return from the wait functions and recalculate the amount of time left to wait. This is a cherry pick of CL 191957 and its cleanup, CL 198417. Updates #31528 Fixes #36376 Change-Id: I0db02cc72188cb620954e87a0180e0a3c83f4a56 Reviewed-on: https://go-review.googlesource.com/c/go/+/193607 Run-TryBot: Jason A. Donenfeld <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/213197
1 parent e4e29ee commit 93f0599

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

src/runtime/os_windows.go

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
//go:cgo_import_dynamic runtime._VirtualFree VirtualFree%3 "kernel32.dll"
5050
//go:cgo_import_dynamic runtime._VirtualQuery VirtualQuery%3 "kernel32.dll"
5151
//go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject%2 "kernel32.dll"
52+
//go:cgo_import_dynamic runtime._WaitForMultipleObjects WaitForMultipleObjects%4 "kernel32.dll"
5253
//go:cgo_import_dynamic runtime._WriteConsoleW WriteConsoleW%5 "kernel32.dll"
5354
//go:cgo_import_dynamic runtime._WriteFile WriteFile%5 "kernel32.dll"
5455

@@ -96,6 +97,7 @@ var (
9697
_VirtualFree,
9798
_VirtualQuery,
9899
_WaitForSingleObject,
100+
_WaitForMultipleObjects,
99101
_WriteConsoleW,
100102
_WriteFile,
101103
_ stdFunction
@@ -138,7 +140,8 @@ func tstart_stdcall(newm *m) uint32
138140
func ctrlhandler(_type uint32) uint32
139141

140142
type mOS struct {
141-
waitsema uintptr // semaphore for parking on locks
143+
waitsema uintptr // semaphore for parking on locks
144+
resumesema uintptr // semaphore to indicate suspend/resume
142145
}
143146

144147
//go:linkname os_sigpipe os.sigpipe
@@ -257,6 +260,40 @@ func loadOptionalSyscalls() {
257260
}
258261
}
259262

263+
func monitorSuspendResume() {
264+
const _DEVICE_NOTIFY_CALLBACK = 2
265+
type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct {
266+
callback uintptr
267+
context uintptr
268+
}
269+
270+
powrprof := windowsLoadSystemLib([]byte("powrprof.dll\000"))
271+
if powrprof == 0 {
272+
return // Running on Windows 7, where we don't need it anyway.
273+
}
274+
powerRegisterSuspendResumeNotification := windowsFindfunc(powrprof, []byte("PowerRegisterSuspendResumeNotification\000"))
275+
if powerRegisterSuspendResumeNotification == nil {
276+
return // Running on Windows 7, where we don't need it anyway.
277+
}
278+
var fn interface{} = func(context uintptr, changeType uint32, setting uintptr) uintptr {
279+
for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
280+
if mp.resumesema != 0 {
281+
stdcall1(_SetEvent, mp.resumesema)
282+
}
283+
}
284+
return 0
285+
}
286+
params := _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS{
287+
callback: compileCallback(*efaceOf(&fn), true),
288+
}
289+
handle := uintptr(0)
290+
if stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
291+
uintptr(unsafe.Pointer(&params)),
292+
uintptr(unsafe.Pointer(&handle))) != 0 {
293+
throw("PowerRegisterSuspendResumeNotification failure")
294+
}
295+
}
296+
260297
//go:nosplit
261298
func getLoadLibrary() uintptr {
262299
return uintptr(unsafe.Pointer(_LoadLibraryW))
@@ -487,6 +524,10 @@ func goenvs() {
487524
}
488525

489526
stdcall1(_FreeEnvironmentStringsW, uintptr(strings))
527+
528+
// We call this all the way here, late in init, so that malloc works
529+
// for the callback function this generates.
530+
monitorSuspendResume()
490531
}
491532

492533
// exiting is set to non-zero when the process is exiting.
@@ -605,19 +646,32 @@ func semasleep(ns int64) int32 {
605646
_WAIT_FAILED = 0xFFFFFFFF
606647
)
607648

608-
// store ms in ns to save stack space
649+
var result uintptr
609650
if ns < 0 {
610-
ns = _INFINITE
651+
result = stdcall2(_WaitForSingleObject, getg().m.waitsema, uintptr(_INFINITE))
611652
} else {
612-
ns = int64(timediv(ns, 1000000, nil))
613-
if ns == 0 {
614-
ns = 1
653+
start := nanotime()
654+
elapsed := int64(0)
655+
for {
656+
ms := int64(timediv(ns-elapsed, 1000000, nil))
657+
if ms == 0 {
658+
ms = 1
659+
}
660+
result = stdcall4(_WaitForMultipleObjects, 2,
661+
uintptr(unsafe.Pointer(&[2]uintptr{getg().m.waitsema, getg().m.resumesema})),
662+
0, uintptr(ms))
663+
if result != _WAIT_OBJECT_0+1 {
664+
// Not a suspend/resume event
665+
break
666+
}
667+
elapsed = nanotime() - start
668+
if elapsed >= ns {
669+
return -1
670+
}
615671
}
616672
}
617-
618-
result := stdcall2(_WaitForSingleObject, getg().m.waitsema, uintptr(ns))
619673
switch result {
620-
case _WAIT_OBJECT_0: //signaled
674+
case _WAIT_OBJECT_0: // Signaled
621675
return 0
622676

623677
case _WAIT_TIMEOUT:
@@ -666,6 +720,15 @@ func semacreate(mp *m) {
666720
throw("runtime.semacreate")
667721
})
668722
}
723+
mp.resumesema = stdcall4(_CreateEventA, 0, 0, 0, 0)
724+
if mp.resumesema == 0 {
725+
systemstack(func() {
726+
print("runtime: createevent failed; errno=", getlasterror(), "\n")
727+
throw("runtime.semacreate")
728+
})
729+
stdcall1(_CloseHandle, mp.waitsema)
730+
mp.waitsema = 0
731+
}
669732
}
670733

671734
// May run with m.p==nil, so write barriers are not allowed. This

src/runtime/syscall_windows.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ func compileCallback(fn eface, cleanstack bool) (code uintptr) {
7474
argsize += uintptrSize
7575
}
7676

77-
lock(&cbs.lock)
78-
defer unlock(&cbs.lock)
77+
lock(&cbs.lock) // We don't unlock this in a defer because this is used from the system stack.
7978

8079
n := cbs.n
8180
for i := 0; i < n; i++ {
8281
if cbs.ctxt[i].gobody == fn.data && cbs.ctxt[i].isCleanstack() == cleanstack {
83-
return callbackasmAddr(i)
82+
r := callbackasmAddr(i)
83+
unlock(&cbs.lock)
84+
return r
8485
}
8586
}
8687
if n >= cb_max {
88+
unlock(&cbs.lock)
8789
throw("too many callback functions")
8890
}
8991

@@ -99,7 +101,9 @@ func compileCallback(fn eface, cleanstack bool) (code uintptr) {
99101
cbs.ctxt[n] = c
100102
cbs.n++
101103

102-
return callbackasmAddr(n)
104+
r := callbackasmAddr(n)
105+
unlock(&cbs.lock)
106+
return r
103107
}
104108

105109
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800

0 commit comments

Comments
 (0)