mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 15:03:41 +00:00
runtime: add cheap atomic condition variable
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user