github.com/Cloud-Foundations/Dominator@v0.3.4/lib/cpusharer/lib.go (about) 1 package cpusharer 2 3 import ( 4 "time" 5 ) 6 7 func grabSemaphore(s CpuSharer, semaphore chan<- struct{}) { 8 select { 9 case semaphore <- struct{}{}: // Non-blocking: don't release the CPU. 10 return 11 default: 12 // Semaphore will block. Release the CPU and go back to the end of the 13 // queue once the semaphore is grabbed. 14 s.ReleaseCpu() 15 semaphore <- struct{}{} 16 s.GrabCpu() 17 } 18 } 19 20 func startGoroutine(s CpuSharer, goFunc func()) { 21 go func() { 22 s.GrabCpu() 23 goFunc() 24 s.ReleaseCpu() 25 }() 26 } 27 28 func startGoroutineWhenAvailable(s CpuSharer, goFunc func()) { 29 s.GrabCpu() 30 go func() { 31 goFunc() 32 s.ReleaseCpu() 33 }() 34 } 35 36 func sleep(s CpuSharer, duration time.Duration) { 37 s.ReleaseCpu() 38 time.Sleep(duration) 39 s.GrabCpu() 40 }