//go:build linux

package main

import (
	"runtime"
	"syscall"
)

const (
	ioprioWhoProcess = 1
	ioprioClassIdle  = 3
	ioprioClassShift = 13
)

// lowerThreadPriority pins the calling goroutine to its OS thread and gives that thread the lowest
// CPU priority (nice 19) and the idle I/O class: the kernel runs it only when nothing else wants
// the CPU or the disk. Errors are ignored (the systemd unit sets the same for the whole process).
func lowerThreadPriority() {
	runtime.LockOSThread()
	tid := syscall.Gettid()
	_ = syscall.Setpriority(syscall.PRIO_PROCESS, tid, 19)
	_, _, _ = syscall.Syscall(sysIoprioSet, ioprioWhoProcess, uintptr(tid), ioprioClassIdle<<ioprioClassShift)
}
