-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
200 lines (174 loc) · 5.57 KB
/
run.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"io"
)
// Pi represents the state of a Pi program.
type Pi struct {
Cycle uint64
Queue []Node
Ether []Message
Stdio []*Channel
}
// Channel holds channel and subscription information.
type Channel struct {
IOIndex int // -1 or IO channel index
Listeners []Node // Current channel listeners
PrevCycle uint64 // Previous cycle in which a message was delivered
}
// Node represents a process with a number of bound channels. This follows the
// signalling network metaphor. A process is a pointer to the program AST.
type Node struct {
Proc *Proc // Process at which this node is paused
Refs []*Channel // Referenced channels
}
// Message represents a single message.
type Message struct {
Channel *Channel
Content *Channel
}
// Schedule adds child processes to the queue with the provided references.
func (pi *Pi) Schedule(proc []*Proc, refs []*Channel) {
// Make sure each process gets a copy of the references such that they can
// modify the references in place without interferring with other processes.
for i, p := range proc {
if i == 0 {
pi.Queue = append(pi.Queue, Node{p, refs})
} else {
pi.Queue = append(pi.Queue, Node{p, copyRefs(refs)})
}
}
}
// Initialize sets up the initial program state.
func (pi *Pi) Initialize(proc []*Proc) {
// Create IO channels.
pi.Stdio = make([]*Channel, ioChannelOffset)
for i := 0; i < ioChannelOffset; i++ {
pi.Stdio[i] = &Channel{i, nil, 0}
}
pi.Schedule(proc, copyRefs(pi.Stdio))
}
// RunNextNode executes the top node in the process queue.
func (pi *Pi) RunNextNode() {
if len(pi.Queue) == 0 {
return
}
var node Node
node, pi.Queue = pi.Queue[0], pi.Queue[1:]
switch node.Proc.Command {
case PINewRef:
assert(len(node.Refs) == node.Proc.Channel)
refs := append(node.Refs, &Channel{-1, nil, 0})
pi.Schedule(node.Proc.Children, refs)
case PIDeref:
refs := deleteRef(node.Refs, node.Proc.Channel)
pi.Schedule(node.Proc.Children, refs)
case PISubsOne:
fallthrough
case PISubsAll:
channel := node.Refs[node.Proc.Channel]
channel.Listeners = append(channel.Listeners, node)
case PISend:
channel := node.Refs[node.Proc.Channel]
message := node.Refs[node.Proc.Message]
pi.Ether = append(pi.Ether, Message{channel, message})
pi.Schedule(node.Proc.Children, node.Refs)
// Messages to the debug channel are handled immediately. This is practical
// because if we wait the listeners may change.
if channel.IOIndex == miscIOChannels["DEBUG"] {
message.PrintDebugInfo()
}
}
}
// DeliverMessages delivers up to one message per channel from the ether.
func (pi *Pi) DeliverMessages(input io.Reader, output io.Writer) {
pi.Cycle++
messages := pi.Ether
pi.Ether = pi.Ether[0:0]
for _, m := range messages {
// Check if we can send a message on this channel in the current cycle. We
// send only one message per channel per cycle!
if m.Channel.PrevCycle < pi.Cycle {
m.Channel.PrevCycle = pi.Cycle
} else {
// Put message back into the ether.
pi.Ether = append(pi.Ether, m)
continue
}
listeners := m.Channel.Listeners
m.Channel.Listeners = m.Channel.Listeners[0:0]
for _, node := range listeners {
assert(len(node.Refs) == node.Proc.Message)
// Copy references of a PISubsAll subscription and renew subscription.
refs := node.Refs
if node.Proc.Command == PISubsAll {
refs = copyRefs(node.Refs)
m.Channel.Listeners = append(m.Channel.Listeners, node)
}
// Append message content to references and queue child processes.
refs = append(refs, m.Content)
pi.Schedule(node.Proc.Children, refs)
}
// Clear part of the listeners that we did not overwrite (for GC).
for i := len(m.Channel.Listeners); i < len(listeners); i++ {
listeners[i] = Node{}
}
// Handle IO messages. Note that the way we iterate and overwrite the ether
// buffer at the same time is only ok as long as this function returns at
// most one message.
if m.Channel.IOIndex != -1 {
ioMessages := handleStdioMessage(pi.Stdio, input, output, m)
pi.Ether = append(pi.Ether, ioMessages...)
}
}
// Clear part of the ether that we did not overwrite (for GC).
for i := len(pi.Ether); i < len(messages); i++ {
messages[i] = Message{}
}
}
func handleStdioMessage(stdio []*Channel, in io.Reader, out io.Writer, m Message) []Message {
// + Standard input read trigger.
// + Standard output byte trigger.
// + Debug info channel.
id := m.Channel.IOIndex
if id == miscIOChannels["stdin_read"] {
// Wait for next byte (or EOF)
buf := make([]byte, 1)
if _, err := in.Read(buf); err == nil {
// Send byte read trigger.
byteReadChannel := stdio[buf[0]]
return []Message{Message{byteReadChannel, m.Content}}
} else if err == io.EOF {
// Send EOF trigger.
eofChannel := stdio[miscIOChannels["stdin_EOF"]]
return []Message{Message{eofChannel, m.Content}}
}
} else if stdoutOffset <= id && id < stdoutOffset+256 {
// Write byte to stdout and send acknowledgement message.
b := byte(id - stdoutOffset)
out.Write([]byte{b})
return []Message{Message{m.Content, m.Content}}
}
return nil
}
// PrintDebugInfo prints the listeners.
func (c *Channel) PrintDebugInfo() {
println()
println("--- DEBUG SECTION ---")
fmt.Printf("channel address: %p\n", c)
for _, node := range c.Listeners {
fmt.Printf("+ %v\n", node.Proc.Location)
}
println("---------------------")
}
func copyRefs(src []*Channel) []*Channel {
return append(src[:0:0], src...)
}
func deleteRef(src []*Channel, i int) []*Channel {
// See https://github.com/golang/go/wiki/SliceTricks
if i < len(src)-1 {
copy(src[i:], src[i+1:])
}
src[len(src)-1] = nil
return src[:len(src)-1]
}