ringbuffer: eliminate idle load by using condition variables instead of sleeps

This commit is contained in:
aler9
2021-03-05 20:14:57 +01:00
parent 5f15e8e3b6
commit 0b43bd2f19
4 changed files with 61 additions and 5 deletions

38
pkg/ringbuffer/event.go Normal file
View File

@@ -0,0 +1,38 @@
package ringbuffer
import (
"sync"
)
type event struct {
mutex sync.Mutex
cond *sync.Cond
value bool
}
func newEvent() *event {
cv := &event{}
cv.cond = sync.NewCond(&cv.mutex)
return cv
}
func (cv *event) signal() {
func() {
cv.mutex.Lock()
defer cv.mutex.Unlock()
cv.value = true
}()
cv.cond.Broadcast()
}
func (cv *event) wait() {
cv.mutex.Lock()
defer cv.mutex.Unlock()
if !cv.value {
cv.cond.Wait()
}
cv.value = false
}