github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/cond_nosched.go (about)

     1  //go:build scheduler.none
     2  
     3  package runtime
     4  
     5  import "runtime/interrupt"
     6  
     7  // Cond is a simplified condition variable, useful for notifying goroutines of interrupts.
     8  type Cond struct {
     9  	notified bool
    10  }
    11  
    12  // Notify sends a notification.
    13  // If the condition variable already has a pending notification, this returns false.
    14  func (c *Cond) Notify() bool {
    15  	i := interrupt.Disable()
    16  	prev := c.notified
    17  	c.notified = true
    18  	interrupt.Restore(i)
    19  	return !prev
    20  }
    21  
    22  // Poll checks for a notification.
    23  // If a notification is found, it is cleared and this returns true.
    24  func (c *Cond) Poll() bool {
    25  	i := interrupt.Disable()
    26  	notified := c.notified
    27  	c.notified = false
    28  	interrupt.Restore(i)
    29  	return notified
    30  }
    31  
    32  // Wait for a notification.
    33  // If the condition variable was previously notified, this returns immediately.
    34  func (c *Cond) Wait() {
    35  	for !c.Poll() {
    36  		waitForEvents()
    37  	}
    38  }