From 80e53c2971f512519f458ff53d3b9ab25b47f83d Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Fri, 19 Jul 2024 07:29:57 -0700 Subject: [PATCH] load DLL only once --- backend/nosleep_windows.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/backend/nosleep_windows.go b/backend/nosleep_windows.go index 6a10d2d9..d605e5a0 100644 --- a/backend/nosleep_windows.go +++ b/backend/nosleep_windows.go @@ -2,16 +2,30 @@ package backend -import "syscall" +import ( + "sync/atomic" + "syscall" +) const ( ES_CONTINUOUS uint = 0x80000000 ES_SYSTEM_REQUIRED uint = 0x00000001 ) +var ( + sleepDisabled atomic.Bool + executionState *syscall.LazyProc +) + func SetSystemSleepDisabled(disable bool) { - kernel32 := syscall.NewLazyDLL("kernel32.dll") - executionState := kernel32.NewProc("SetThreadExecutionState") + if old := sleepDisabled.Swap(disable); old == disable { + return + } + + if executionState == nil { + kernel32 := syscall.NewLazyDLL("kernel32.dll") + executionState = kernel32.NewProc("SetThreadExecutionState") + } uType := ES_CONTINUOUS if disable {