runtime: add cheap atomic condition variable

This commit is contained in:
Nia Weiss
2020-06-15 18:57:13 -04:00
committed by Ron Evans
parent b40f250530
commit ed9b97cc0d
3 changed files with 172 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
// +build scheduler.none
package runtime
import "runtime/interrupt"
// Cond is a simplified condition variable, useful for notifying goroutines of interrupts.
type Cond struct {
notified bool
}
// Notify sends a notification.
// If the condition variable already has a pending notification, this returns false.
func (c *Cond) Notify() bool {
i := interrupt.Disable()
prev := c.notified
c.notified = true
interrupt.Restore(i)
return !prev
}
// Poll checks for a notification.
// If a notification is found, it is cleared and this returns true.
func (c *Cond) Poll() bool {
i := interrupt.Disable()
notified := c.notified
c.notified = false
interrupt.Restore(i)
return notified
}
// Wait for a notification.
// If the condition variable was previously notified, this returns immediately.
func (c *Cond) Wait() {
for !c.Poll() {
waitForEvents()
}
}