mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 15:46:51 +08:00
ringbuffer: eliminate idle load by using condition variables instead of sleeps
This commit is contained in:
38
pkg/ringbuffer/event.go
Normal file
38
pkg/ringbuffer/event.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user