-
Notifications
You must be signed in to change notification settings - Fork 327
/
forward.go
154 lines (136 loc) · 4.81 KB
/
forward.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
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dependencygraph2
import (
"context"
"github.com/google/gapid/core/log"
)
type ForwardAccessMode uint
const (
FORWARD_OPEN ForwardAccessMode = iota + 1
FORWARD_CLOSE
FORWARD_DROP
)
type ForwardNodes struct {
Open NodeID
Close NodeID
Drop NodeID
}
type ForwardAccess struct {
Nodes *ForwardNodes
DependencyID interface{}
Mode ForwardAccessMode
}
type Accesses struct {
nodeAccesses map[NodeID][]ForwardAccess
isUnopened bool
}
type ForwardWatcher interface {
OpenForwardDependency(ctx context.Context, cmdCtx CmdContext, dependencyID interface{})
CloseForwardDependency(ctx context.Context, cmdCtx CmdContext, dependencyID interface{})
DropForwardDependency(ctx context.Context, cmdCtx CmdContext, dependencyID interface{})
OnBeginCmd(ctx context.Context, cmdCtx CmdContext)
OnEndCmd(ctx context.Context, cmdCtx CmdContext) Accesses
OnBeginSubCmd(ctx context.Context, cmdCtx CmdContext, subCmdCtx CmdContext)
OnEndSubCmd(ctx context.Context, cmdCtx CmdContext)
}
type forwardWatcher struct {
openForwardDependencies map[interface{}]*ForwardNodes
forwardAccesses []ForwardAccess
nodeAccesses map[NodeID][]ForwardAccess
isUnopened bool
}
func NewForwardWatcher() *forwardWatcher {
return &forwardWatcher{
openForwardDependencies: make(map[interface{}]*ForwardNodes),
nodeAccesses: make(map[NodeID][]ForwardAccess),
}
}
func (b *forwardWatcher) OpenForwardDependency(ctx context.Context, cmdCtx CmdContext, dependencyID interface{}) {
if len(b.forwardAccesses) == 0 {
if forwardAccesses, ok := b.nodeAccesses[cmdCtx.nodeID]; ok {
b.forwardAccesses = forwardAccesses
}
}
nodes := ForwardNodes{
Open: cmdCtx.nodeID,
Close: NodeNoID,
Drop: NodeNoID,
}
acc := ForwardAccess{
Nodes: &nodes,
DependencyID: dependencyID,
Mode: FORWARD_OPEN,
}
if _, ok := b.openForwardDependencies[dependencyID]; ok {
log.D(ctx, "OpenForwardDependency: Forward dependency opened multiple times before being closed. DependencyID: %v, close node: %v", dependencyID, cmdCtx.nodeID)
} else {
b.openForwardDependencies[dependencyID] = acc.Nodes
}
b.forwardAccesses = append(b.forwardAccesses, acc)
}
func (b *forwardWatcher) CloseForwardDependency(ctx context.Context, cmdCtx CmdContext, dependencyID interface{}) {
if len(b.forwardAccesses) == 0 {
if forwardAccesses, ok := b.nodeAccesses[cmdCtx.nodeID]; ok {
b.forwardAccesses = forwardAccesses
}
}
if open, ok := b.openForwardDependencies[dependencyID]; ok {
delete(b.openForwardDependencies, dependencyID)
open.Close = cmdCtx.nodeID
b.forwardAccesses = append(b.forwardAccesses, ForwardAccess{
Nodes: open,
DependencyID: dependencyID,
Mode: FORWARD_CLOSE,
})
} else {
b.isUnopened = true
log.D(ctx, "CloseForwardDependency: Forward dependency closed before being opened. DependencyID: %v, close node: %v", dependencyID, cmdCtx.nodeID)
}
}
func (b *forwardWatcher) DropForwardDependency(ctx context.Context, cmdCtx CmdContext, dependencyID interface{}) {
if open, ok := b.openForwardDependencies[dependencyID]; ok {
delete(b.openForwardDependencies, dependencyID)
open.Drop = cmdCtx.nodeID
b.forwardAccesses = append(b.forwardAccesses, ForwardAccess{
Nodes: open,
DependencyID: dependencyID,
Mode: FORWARD_DROP,
})
} else {
log.D(ctx, "DropForwardDependency: Forward dependency dropped before being opened. DependencyID: %v, close node: %v", dependencyID, cmdCtx.nodeID)
}
}
func (b *forwardWatcher) Flush(ctx context.Context, cmdCtx CmdContext) {
b.nodeAccesses[cmdCtx.nodeID] = b.forwardAccesses
b.forwardAccesses = []ForwardAccess{}
}
func (b *forwardWatcher) OnBeginCmd(ctx context.Context, cmdCtx CmdContext) {}
func (b *forwardWatcher) OnEndCmd(ctx context.Context, cmdCtx CmdContext) Accesses {
b.Flush(ctx, cmdCtx)
acc := Accesses{
nodeAccesses: b.nodeAccesses,
isUnopened: b.isUnopened,
}
b.nodeAccesses = make(map[NodeID][]ForwardAccess)
b.forwardAccesses = []ForwardAccess{}
b.isUnopened = false
return acc
}
func (b *forwardWatcher) OnBeginSubCmd(ctx context.Context, cmdCtx CmdContext, subCmdCtx CmdContext) {
b.Flush(ctx, cmdCtx)
}
func (b *forwardWatcher) OnEndSubCmd(ctx context.Context, cmdCtx CmdContext) {
b.Flush(ctx, cmdCtx)
}