-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnode.go
110 lines (94 loc) · 1.91 KB
/
node.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
package gotaskflow
import (
"fmt"
"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{}
rw *sync.RWMutex
state atomic.Int32
joinCounter uint
g *eGraph
priority TaskPriority
}
func (n *innerNode) recyclable(lockup bool) bool {
if lockup {
n.rw.RLock()
defer n.rw.RUnlock()
}
return n.joinCounter == 0
}
func (n *innerNode) ref(lockup bool) {
if lockup {
n.rw.Lock()
defer n.rw.Unlock()
}
n.joinCounter++
}
func (n *innerNode) deref(lockup bool) {
if lockup {
n.rw.Lock()
defer n.rw.Unlock()
}
if n.joinCounter == 0 {
panic(fmt.Sprintf("node %v ref counter is zero, cannot deref", n.name))
}
n.joinCounter--
}
func (n *innerNode) setup() {
n.rw.Lock()
defer n.rw.Unlock()
n.state.Store(kNodeStateIdle)
for _, dep := range n.dependents {
if dep.Typ == nodeCondition {
continue
}
n.ref(false)
}
}
func (n *innerNode) drop() {
// release every deps
for _, node := range n.successors {
if n.Typ != nodeCondition {
node.deref(true)
}
}
}
// set dependency: V deps on N, V is input node
func (n *innerNode) precede(v *innerNode) {
n.successors = append(n.successors, v)
v.dependents = append(v.dependents, n)
}
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),
rw: &sync.RWMutex{},
priority: NORMAL,
joinCounter: 0,
}
}