-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathnode.go
More file actions
100 lines (87 loc) · 1.9 KB
/
node.go
File metadata and controls
100 lines (87 loc) · 1.9 KB
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
package gotaskflow
import (
"strconv"
"sync"
"sync/atomic"
"time"
)
const (
kNodeStateIdle = int32(iota + 1)
kNodeStateWaiting
kNodeStateRunning
kNodeStateFinished
)
type nodeType string
const (
nodeSubflow nodeType = "subflow" // subflow
nodeStatic nodeType = "static" // static
nodeCondition nodeType = "condition" // static
)
type innerNode struct {
name string
successors []*innerNode
dependents []*innerNode
Typ nodeType
ptr interface{}
mu *sync.Mutex
state atomic.Int32
joinCounter atomic.Int32
g *eGraph
priority TaskPriority
}
func (n *innerNode) recyclable() bool {
return n.joinCounter.Load() == 0
}
func (n *innerNode) ref() {
n.joinCounter.Add(1)
}
func (n *innerNode) deref() {
n.joinCounter.Add(-1)
}
func (n *innerNode) setup() {
n.mu.Lock()
defer n.mu.Unlock()
n.state.Store(kNodeStateIdle)
for _, dep := range n.dependents {
if dep.Typ == nodeCondition {
continue
}
n.ref()
}
}
func (n *innerNode) drop() {
// release every deps
for _, node := range n.successors {
if n.Typ != nodeCondition {
node.deref()
}
}
}
// precede sets a dependency: V depends on N, N must complete before V.
func (n *innerNode) precede(v *innerNode) {
n.successors = append(n.successors, v)
v.dependents = append(v.dependents, n)
}
// hasCondPredecessor reports whether any predecessor of this node is a condition node.
func (n *innerNode) hasCondPredecessor() bool {
for _, dep := range n.dependents {
if dep.Typ == nodeCondition {
return true
}
}
return false
}
func newNode(name string) *innerNode {
if len(name) == 0 {
name = "N_" + strconv.Itoa(time.Now().Nanosecond())
}
return &innerNode{
name: name,
state: atomic.Int32{},
successors: make([]*innerNode, 0),
dependents: make([]*innerNode, 0),
mu: &sync.Mutex{},
priority: NORMAL,
joinCounter: atomic.Int32{},
}
}