lowprio_windows.go 973 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package osutil
  7. import (
  8. "fmt"
  9. "golang.org/x/sys/windows"
  10. )
  11. // SetLowPriority lowers the process CPU scheduling priority, and possibly
  12. // I/O priority depending on the platform and OS.
  13. func SetLowPriority() error {
  14. handle, err := windows.GetCurrentProcess()
  15. if err != nil {
  16. return fmt.Errorf("get process handle: %w", err)
  17. }
  18. defer windows.CloseHandle(handle)
  19. if cur, err := windows.GetPriorityClass(handle); err == nil && (cur == windows.IDLE_PRIORITY_CLASS || cur == windows.BELOW_NORMAL_PRIORITY_CLASS) {
  20. // We're done here.
  21. return nil
  22. }
  23. if err := windows.SetPriorityClass(handle, windows.BELOW_NORMAL_PRIORITY_CLASS); err != nil {
  24. return fmt.Errorf("set priority class: %w", err)
  25. }
  26. return nil
  27. }