lowprio_windows.go 785 B

12345678910111213141516171819202122232425262728
  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 err := windows.SetPriorityClass(handle, windows.BELOW_NORMAL_PRIORITY_CLASS); err != nil {
  20. return fmt.Errorf("set priority class: %w", err)
  21. }
  22. return nil
  23. }