-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgraph.go
81 lines (67 loc) · 1.33 KB
/
graph.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
package gotaskflow
import (
"fmt"
"sync"
"sync/atomic"
)
type eGraph struct { // execution graph
name string
nodes []*innerNode
joinCounter uint
entries []*innerNode
scheCond *sync.Cond
instantiated bool
rw *sync.RWMutex
canceled atomic.Bool // only changes when task in graph panic
}
func newGraph(name string) *eGraph {
return &eGraph{
name: name,
nodes: make([]*innerNode, 0),
scheCond: sync.NewCond(&sync.Mutex{}),
joinCounter: 0,
rw: &sync.RWMutex{},
}
}
func (g *eGraph) ref() {
g.rw.Lock()
defer g.rw.Unlock()
g.joinCounter++
}
func (g *eGraph) deref() {
g.rw.Lock()
defer g.rw.Unlock()
if g.joinCounter == 0 {
panic(fmt.Sprintf("graph %v ref counter is zero, cannot deref", g.name))
}
g.joinCounter--
}
func (g *eGraph) reset() {
g.joinCounter = 0
g.entries = g.entries[:0]
for _, n := range g.nodes {
n.joinCounter = 0
}
}
func (g *eGraph) push(n ...*innerNode) {
g.nodes = append(g.nodes, n...)
for _, node := range n {
node.g = g
}
}
func (g *eGraph) setup() {
g.reset()
for _, node := range g.nodes {
node.setup()
if len(node.dependents) == 0 {
g.entries = append(g.entries, node)
}
}
}
func (g *eGraph) recyclable(lockup bool) bool {
if lockup {
g.rw.RLock()
defer g.rw.RUnlock()
}
return g.joinCounter == 0
}