/
execution.go
149 lines (141 loc) · 3.97 KB
/
execution.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package concurrency
import (
"fmt"
"sort"
)
// execution represents an execution of the test.
type execution struct {
// strategy describes the initial sequence of scheduling decisions
// to make.
strategy []TID
// nsteps records the number of scheduling decision made.
nsteps int
// nthreads records the number of threads in the system.
nthreads int
// nrequests records the number of currently pending requests.
nrequests int
// requests is a channel on which scheduling requests are received.
requests chan request
// done is a channel that the request handlers can use to wake up
// the main scheduling loop.
done chan struct{}
// nextTID is a function that can be used to generate unique thread
// identifiers.
nextTID func() TID
// activeTID records the identifier of the currently active thread.
activeTID TID
// ctx stores the abstract state of resources used by the
// execution.
ctx *context
// threads records the abstract state of threads active in the
// execution.
threads map[TID]*thread
}
// newExecution is the execution factory.
func newExecution(strategy []TID) *execution {
execution := &execution{
strategy: strategy,
nthreads: 1,
requests: make(chan request),
nextTID: TIDGenerator(),
ctx: newContext(),
threads: make(map[TID]*thread),
}
clock := newClock()
clock[0] = 0
execution.threads[0] = newThread(0, clock)
return execution
}
// Run executes the body of the test, exploring a sequence of
// scheduling decisions, and returns a vector of the scheduling
// decisions it made as well as the alternative scheduling decisions
// it could have made instead.
func (e *execution) Run(testBody func()) []*choice {
go testBody()
choices := make([]*choice, 0)
// Keep scheduling requests until there are threads left in the
// system.
for e.nthreads != 0 {
// Keep receiving scheduling requests until all threads are
// blocked on a decision.
for e.nrequests != e.nthreads {
request, ok := <-e.requests
if !ok {
panic("Command channel closed unexpectedly.")
}
e.nrequests++
request.process(e)
}
choice := e.generateChoice()
choices = append(choices, choice)
e.activeTID = choice.next
e.schedule(choice.next)
}
return choices
}
// findThread uses the given thread identifier to find a thread among
// the known threads.
func (e *execution) findThread(tid TID) *thread {
thread, ok := e.threads[tid]
if !ok {
panic(fmt.Sprintf("Could not find thread %v.", tid))
}
return thread
}
// generateChoice describes the scheduling choices available at the
// current abstract program state.
func (e *execution) generateChoice() *choice {
c := newChoice()
enabled := make([]TID, 0)
for tid, thread := range e.threads {
t := &transition{
tid: tid,
clock: thread.clock.clone(),
enabled: thread.enabled(e.ctx),
kind: thread.kind(),
readSet: thread.readSet(),
writeSet: thread.writeSet(),
}
c.transitions[tid] = t
if t.enabled {
enabled = append(enabled, tid)
}
}
if len(c.transitions) == 0 {
panic("Encountered a deadlock.")
}
if e.nsteps < len(e.strategy) {
// Follow the scheduling strategy.
c.next = e.strategy[e.nsteps]
} else {
// Schedule an enabled thread using a deterministic round-robin
// scheduler.
sort.Sort(IncreasingTID(enabled))
index := 0
for ; index < len(enabled) && enabled[index] <= e.activeTID; index++ {
}
if index == len(enabled) {
index = 0
}
c.next = enabled[index]
}
return c
}
// schedule advances the execution of the given thread.
func (e *execution) schedule(tid TID) {
e.nrequests--
e.nsteps++
thread, ok := e.threads[tid]
if !ok {
panic(fmt.Sprintf("Could not find thread %v.\n", tid))
}
if !thread.enabled(e.ctx) {
panic(fmt.Sprintf("Thread %v is about to be scheduled and is not enabled.", tid))
}
e.done = make(chan struct{})
close(thread.ready)
<-e.done
}