Skip to content

Commit

Permalink
load DLL only once
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jul 19, 2024
1 parent bc89cc2 commit 80e53c2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions backend/nosleep_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 80e53c2

Please sign in to comment.