// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// CPU affinity functions

package unix

import (
	
	
)

const cpuSetSize = _CPU_SETSIZE / _NCPUBITS

// CPUSet represents a bit mask of CPUs, to be used with [SchedGetaffinity], [SchedSetaffinity],
// and [SetMemPolicy].
//
// Note this type can only represent CPU IDs 0 through 1023.
// Use [CPUSetDynamic]/[NewCPUSet] instead to avoid this limit.
type CPUSet [cpuSetSize]cpuMask

// CPUSetDynamic represents a bit mask of CPUs, to be used with [SchedGetaffinityDynamic],
// [SchedSetaffinityDynamic], and [SetMemPolicyDynamic]. Use [NewCPUSet] to allocate.
type CPUSetDynamic []cpuMask

func ( uintptr,  int,  uintptr,  unsafe.Pointer) error {
	, ,  := RawSyscall(, uintptr(), uintptr(), uintptr())
	if  != 0 {
		return errnoErr()
	}
	return nil
}

// SchedGetaffinity gets the CPU affinity mask of the thread specified by pid.
// If pid is 0 the calling thread is used.
func ( int,  *CPUSet) error {
	return schedAffinity(SYS_SCHED_GETAFFINITY, , unsafe.Sizeof(*), unsafe.Pointer())
}

// SchedSetaffinity sets the CPU affinity mask of the thread specified by pid.
// If pid is 0 the calling thread is used.
func ( int,  *CPUSet) error {
	return schedAffinity(SYS_SCHED_SETAFFINITY, , unsafe.Sizeof(*), unsafe.Pointer())
}

// Zero clears the set s, so that it contains no CPUs.
func ( *CPUSet) () {
	clear([:])
}

// Fill adds all possible CPU bits to the set s. On Linux, [SchedSetaffinity]
// will silently ignore any invalid CPU bits in [CPUSet] so this is an
// efficient way of resetting the CPU affinity of a process.
func ( *CPUSet) () {
	cpuMaskFill([:])
}

func ( int) int {
	return  / _NCPUBITS
}

func ( int) cpuMask {
	return cpuMask(1 << (uint() % _NCPUBITS))
}

func ( []cpuMask) {
	for  := range  {
		[] = ^cpuMask(0)
	}
}

func ( []cpuMask,  int) {
	 := cpuBitsIndex()
	if  < len() {
		[] |= cpuBitsMask()
	}
}

func ( []cpuMask,  int) {
	 := cpuBitsIndex()
	if  < len() {
		[] &^= cpuBitsMask()
	}
}

func ( []cpuMask,  int) bool {
	 := cpuBitsIndex()
	if  < len() {
		return []&cpuBitsMask() != 0
	}
	return false
}

func ( []cpuMask) int {
	 := 0
	for ,  := range  {
		 += bits.OnesCount64(uint64())
	}
	return 
}

// Set adds cpu to the set s. If cpu is out of bounds for s, no action is taken.
func ( *CPUSet) ( int) {
	cpuMaskSet([:], )
}

// Clear removes cpu from the set s. If cpu is out of bounds for s, no action is taken.
func ( *CPUSet) ( int) {
	cpuMaskClear([:], )
}

// IsSet reports whether cpu is in the set s.
func ( *CPUSet) ( int) bool {
	return cpuMaskIsSet([:], )
}

// Count returns the number of CPUs in the set s.
func ( *CPUSet) () int {
	return cpuMaskCount([:])
}

// NewCPUSet creates a CPU affinity mask capable of representing CPU IDs
// up to maxCPU (exclusive).
func ( int) CPUSetDynamic {
	 := ( + _NCPUBITS - 1) / _NCPUBITS
	if  == 0 {
		 = 1
	}
	return make(CPUSetDynamic, )
}

// Zero clears the set s, so that it contains no CPUs.
func ( CPUSetDynamic) () {
	clear()
}

// Fill adds all possible CPU bits to the set s. On Linux, [SchedSetaffinityDynamic]
// will silently ignore any invalid CPU bits in [CPUSetDynamic] so this is an
// efficient way of resetting the CPU affinity of a process.
func ( CPUSetDynamic) () {
	cpuMaskFill()
}

// Set adds cpu to the set s. If cpu is out of bounds for s, no action is taken.
func ( CPUSetDynamic) ( int) {
	cpuMaskSet(, )
}

// Clear removes cpu from the set s. If cpu is out of bounds for s, no action is taken.
func ( CPUSetDynamic) ( int) {
	cpuMaskClear(, )
}

// IsSet reports whether cpu is in the set s.
func ( CPUSetDynamic) ( int) bool {
	return cpuMaskIsSet(, )
}

// Count returns the number of CPUs in the set s.
func ( CPUSetDynamic) () int {
	return cpuMaskCount()
}

func ( CPUSetDynamic) () uintptr {
	return uintptr(len()) * unsafe.Sizeof(cpuMask(0))
}

func ( CPUSetDynamic) () unsafe.Pointer {
	if len() == 0 {
		return nil
	}
	return unsafe.Pointer(&[0])
}

// SchedGetaffinityDynamic gets the CPU affinity mask of the thread specified by pid.
// If pid is 0 the calling thread is used.
//
// If the set is smaller than the size of the affinity mask used by the kernel,
// [EINVAL] is returned.
func ( int,  CPUSetDynamic) error {
	return schedAffinity(SYS_SCHED_GETAFFINITY, , .size(), .pointer())
}

// SchedSetaffinityDynamic sets the CPU affinity mask of the thread specified by pid.
// If pid is 0 the calling thread is used.
func ( int,  CPUSetDynamic) error {
	return schedAffinity(SYS_SCHED_SETAFFINITY, , .size(), .pointer())
}