-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
tables.go
244 lines (219 loc) · 4.64 KB
/
tables.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package rpc
import (
"log"
"zombiezen.com/go/capnproto2"
"zombiezen.com/go/capnproto2/rpc/internal/refcount"
)
// Table IDs
type (
questionID uint32
answerID uint32
exportID uint32
importID uint32
embargoID uint32
)
// impent is an entry in the import table.
type impent struct {
rc *refcount.RefCount
refs int
}
type importTable struct {
tab map[importID]*impent
manager *manager
calls chan<- *appCall
releases chan<- *outgoingRelease
}
// addRef increases the counter of the times the import ID was sent to this vat.
func (it *importTable) addRef(id importID) capnp.Client {
if it.tab == nil {
it.tab = make(map[importID]*impent)
}
ent := it.tab[id]
var ref capnp.Client
if ent == nil {
client := &importClient{
id: id,
manager: it.manager,
calls: it.calls,
releases: it.releases,
}
var rc *refcount.RefCount
rc, ref = refcount.New(client)
ent = &impent{rc: rc, refs: 0}
it.tab[id] = ent
}
if ref == nil {
ref = ent.rc.Ref()
}
ent.refs++
return ref
}
// pop removes the import ID and returns the number of times the import ID was sent to this vat.
func (it *importTable) pop(id importID) (refs int) {
if it.tab != nil {
if ent := it.tab[id]; ent != nil {
refs = ent.refs
}
delete(it.tab, id)
}
return
}
// An outgoingRelease is a message sent to the coordinate goroutine to
// indicate that an import should be released.
type outgoingRelease struct {
id importID
echan chan<- error
}
// An importClient implements capnp.Client for a remote capability.
type importClient struct {
id importID
manager *manager
calls chan<- *appCall
releases chan<- *outgoingRelease
}
func (ic *importClient) Call(cl *capnp.Call) capnp.Answer {
// TODO(light): don't send if closed.
ac, achan := newAppImportCall(ic.id, cl)
select {
case ic.calls <- ac:
select {
case a := <-achan:
return a
case <-ic.manager.finish:
return capnp.ErrorAnswer(ic.manager.err())
}
case <-ic.manager.finish:
return capnp.ErrorAnswer(ic.manager.err())
}
}
func (ic *importClient) Close() error {
echan := make(chan error, 1)
r := &outgoingRelease{
id: ic.id,
echan: echan,
}
select {
case ic.releases <- r:
select {
case err := <-echan:
return err
case <-ic.manager.finish:
return ic.manager.err()
}
case <-ic.manager.finish:
return ic.manager.err()
}
}
type export struct {
id exportID
client capnp.Client
// for use by the table only
refs int
}
type exportTable struct {
tab []*export
gen idgen
}
func (et *exportTable) get(id exportID) *export {
var e *export
if int(id) < len(et.tab) {
e = et.tab[id]
}
return e
}
// add ensures that the client is present in the table, returning its ID.
// If the client is already in the table, the previous ID is returned.
func (et *exportTable) add(client capnp.Client) exportID {
for i, e := range et.tab {
if e != nil && e.client == client {
e.refs++
return exportID(i)
}
}
id := exportID(et.gen.next())
export := &export{
id: id,
client: client,
refs: 1,
}
if int(id) == len(et.tab) {
et.tab = append(et.tab, export)
} else {
et.tab[id] = export
}
return id
}
func (et *exportTable) release(id exportID, refs int) {
if int(id) >= len(et.tab) {
return
}
e := et.tab[id]
if e == nil {
return
}
e.refs -= refs
if e.refs > 0 {
return
}
if e.refs < 0 {
log.Printf("rpc: warning: export %v has negative refcount (%d)", id, e.refs)
}
if err := e.client.Close(); err != nil {
log.Printf("rpc: export %v close: %v", id, err)
}
et.tab[id] = nil
et.gen.remove(uint32(id))
}
// releaseList decrements the reference count of each of the given exports by 1.
func (et *exportTable) releaseList(ids []exportID) {
for _, id := range ids {
et.release(id, 1)
}
}
type embargoTable struct {
tab []chan<- struct{}
gen idgen
}
type embargo <-chan struct{}
func (et *embargoTable) new() (embargoID, embargo) {
id := embargoID(et.gen.next())
e := make(chan struct{})
if int(id) == len(et.tab) {
et.tab = append(et.tab, e)
} else {
et.tab[id] = e
}
return id, e
}
func (et *embargoTable) disembargo(id embargoID) {
if int(id) >= len(et.tab) {
return
}
e := et.tab[id]
if e == nil {
return
}
close(e)
et.tab[id] = nil
et.gen.remove(uint32(id))
}
// idgen returns a sequence of monotonically increasing IDs with
// support for replacement. The zero value is a generator that
// starts at zero.
type idgen struct {
i uint32
free []uint32
}
func (gen *idgen) next() uint32 {
if n := len(gen.free); n > 0 {
i := gen.free[n-1]
gen.free = gen.free[:n-1]
return i
}
i := gen.i
gen.i++
return i
}
func (gen *idgen) remove(i uint32) {
gen.free = append(gen.free, i)
}