lib/osutil: Use x/sys/windows for SetLowPriority
This commit is contained in:
@@ -7,43 +7,19 @@
|
|||||||
package osutil
|
package osutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
"golang.org/x/sys/windows"
|
||||||
|
|
||||||
const (
|
|
||||||
// https://docs.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
|
|
||||||
aboveNormalPriorityClass = 0x00008000
|
|
||||||
belowNormalPriorityClass = 0x00004000
|
|
||||||
highPriorityClass = 0x00000080
|
|
||||||
idlePriorityClass = 0x00000040
|
|
||||||
normalPriorityClass = 0x00000020
|
|
||||||
processModeBackgroundBegin = 0x00100000
|
|
||||||
processModeBackgroundEnd = 0x00200000
|
|
||||||
realtimePriorityClass = 0x00000100
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetLowPriority lowers the process CPU scheduling priority, and possibly
|
// SetLowPriority lowers the process CPU scheduling priority, and possibly
|
||||||
// I/O priority depending on the platform and OS.
|
// I/O priority depending on the platform and OS.
|
||||||
func SetLowPriority() error {
|
func SetLowPriority() error {
|
||||||
modkernel32 := syscall.NewLazyDLL("kernel32.dll")
|
handle, err := windows.GetCurrentProcess()
|
||||||
setPriorityClass := modkernel32.NewProc("SetPriorityClass")
|
|
||||||
|
|
||||||
if err := setPriorityClass.Find(); err != nil {
|
|
||||||
return errors.Wrap(err, "find proc")
|
|
||||||
}
|
|
||||||
|
|
||||||
handle, err := syscall.GetCurrentProcess()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "get process handler")
|
return errors.Wrap(err, "get process handle")
|
||||||
}
|
}
|
||||||
defer syscall.CloseHandle(handle)
|
defer windows.CloseHandle(handle)
|
||||||
|
|
||||||
res, _, err := setPriorityClass.Call(uintptr(handle), belowNormalPriorityClass)
|
err = windows.SetPriorityClass(handle, windows.BELOW_NORMAL_PRIORITY_CLASS)
|
||||||
if res != 0 {
|
|
||||||
// "If the function succeeds, the return value is nonzero."
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return errors.Wrap(err, "set priority class") // wraps nil as nil
|
return errors.Wrap(err, "set priority class") // wraps nil as nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user