aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/runtime/cond.go
blob: e382adb8b1f952b5a4450f737126141cb783b86b (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//go:build !scheduler.none
// +build !scheduler.none

package runtime

import (
	"internal/task"
	"sync/atomic"
	"unsafe"
)

// notifiedPlaceholder is a placeholder task which is used to indicate that the condition variable has been notified.
var notifiedPlaceholder task.Task

// Cond is a simplified condition variable, useful for notifying goroutines of interrupts.
type Cond struct {
	t *task.Task
}

// Notify sends a notification.
// If the condition variable already has a pending notification, this returns false.
func (c *Cond) Notify() bool {
	for {
		t := (*task.Task)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t))))
		switch t {
		case nil:
			// Nothing is waiting yet.
			// Apply the notification placeholder.
			if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t)), unsafe.Pointer(t), unsafe.Pointer(&notifiedPlaceholder)) {
				return true
			}
		case &notifiedPlaceholder:
			// The condition variable has already been notified.
			return false
		default:
			// Unblock the waiting task.
			if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t)), unsafe.Pointer(t), nil) {
				runqueuePushBack(t)
				return true
			}
		}
	}
}

// Poll checks for a notification.
// If a notification is found, it is cleared and this returns true.
func (c *Cond) Poll() bool {
	for {
		t := (*task.Task)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t))))
		switch t {
		case nil:
			// No notifications are present.
			return false
		case &notifiedPlaceholder:
			// A notification arrived and there is no waiting goroutine.
			// Clear the notification and return.
			if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t)), unsafe.Pointer(t), nil) {
				return true
			}
		default:
			// A task is blocked on the condition variable, which means it has not been notified.
			return false
		}
	}
}

// Wait for a notification.
// If the condition variable was previously notified, this returns immediately.
func (c *Cond) Wait() {
	cur := task.Current()
	for {
		t := (*task.Task)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t))))
		switch t {
		case nil:
			// Condition variable has not been notified.
			// Block the current task on the condition variable.
			if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t)), nil, unsafe.Pointer(cur)) {
				task.Pause()
				return
			}
		case &notifiedPlaceholder:
			// A notification arrived and there is no waiting goroutine.
			// Clear the notification and return.
			if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.t)), unsafe.Pointer(t), nil) {
				return
			}
		default:
			panic("interrupt.Cond: condition variable in use by another goroutine")
		}
	}
}