// 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 CPU affinity mask.
type CPUSet [cpuSetSize]cpuMask

func schedAffinity( uintptr,  int,  *CPUSet) error {
	, ,  := RawSyscall(, uintptr(), uintptr(unsafe.Sizeof(*)), uintptr(unsafe.Pointer()))
	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, , )
}

// 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, , )
}

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

func cpuBitsIndex( int) int {
	return  / _NCPUBITS
}

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

// Set adds cpu to the set s.
func ( *CPUSet) ( int) {
	 := cpuBitsIndex()
	if  < len() {
		[] |= cpuBitsMask()
	}
}

// Clear removes cpu from the set s.
func ( *CPUSet) ( int) {
	 := cpuBitsIndex()
	if  < len() {
		[] &^= cpuBitsMask()
	}
}

// IsSet reports whether cpu is in the set s.
func ( *CPUSet) ( int) bool {
	 := cpuBitsIndex()
	if  < len() {
		return []&cpuBitsMask() != 0
	}
	return false
}

// Count returns the number of CPUs in the set s.
func ( *CPUSet) () int {
	 := 0
	for ,  := range  {
		 += bits.OnesCount64(uint64())
	}
	return 
}