github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/pkg/sysinfo/numcpu_windows.go (about)

     1  package sysinfo
     2  
     3  import (
     4  	"runtime"
     5  	"unsafe"
     6  
     7  	"golang.org/x/sys/windows"
     8  )
     9  
    10  var (
    11  	kernel32               = windows.NewLazySystemDLL("kernel32.dll")
    12  	getCurrentProcess      = kernel32.NewProc("GetCurrentProcess")
    13  	getProcessAffinityMask = kernel32.NewProc("GetProcessAffinityMask")
    14  )
    15  
    16  func numCPU() int {
    17  	// Gets the affinity mask for a process
    18  	var mask, sysmask uintptr
    19  	currentProcess, _, _ := getCurrentProcess.Call()
    20  	ret, _, _ := getProcessAffinityMask.Call(currentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask)))
    21  	if ret == 0 {
    22  		return 0
    23  	}
    24  	// For every available thread a bit is set in the mask.
    25  	ncpu := int(popcnt(uint64(mask)))
    26  	return ncpu
    27  }
    28  
    29  // NumCPU returns the number of CPUs which are currently online
    30  func NumCPU() int {
    31  	if ncpu := numCPU(); ncpu > 0 {
    32  		return ncpu
    33  	}
    34  	return runtime.NumCPU()
    35  }