aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/runtime/cond_nosched.go
blob: f999a56c6d2835274c8283275789fa794a2c4a29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//go:build scheduler.none
// +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()
	}
}