-
Notifications
You must be signed in to change notification settings - Fork 0
/
routine_internal.go
87 lines (72 loc) · 1.67 KB
/
routine_internal.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
package routines
import (
"fmt"
"runtime"
"time"
)
func (r *Routine) caller() uintptr {
if runtime.Callers(3, r.pc[:]) != 1 {
panic(fmt.Errorf("failed to get caller"))
}
return r.pc[0]
}
func (r *Routine) isExecuted(callers string) (executed bool) {
_, executed = r.executed[callers]
return executed
}
func (r *Routine) isPrevExecuted(myCaller string) bool {
for _, caller := range r.executionSequence {
if caller == myCaller {
break
}
if !r.isExecuted(caller) {
return false
}
}
return true
}
func (r *Routine) isPrevExecutedTo(index int) bool {
for i, caller := range r.executionSequence {
if i < index {
break
}
if !r.isExecuted(caller) {
return false
}
}
return true
}
func (r *Routine) executionSequenceIndex(caller string) int {
i, ok := r.executionSeqIndex[caller]
if !ok {
i = len(r.executionSequence)
r.executionSeqIndex[caller] = i
}
return i
}
func (r *Routine) addExecution(caller string) {
for i := len(r.executionSequence) - 1; i >= 0; i-- {
if r.executionSequence[i] == caller {
return
}
}
r.executionSequence = append(r.executionSequence, caller)
}
func (r *Routine) markAsExecuted(caller string) {
r.executed[caller] = struct{}{}
}
func (r *Routine) executionTimer(caller string, duration time.Duration) <-chan time.Time {
if timer, found := r.timers[caller]; found {
return timer
}
timer := time.After(duration)
r.timers[caller] = timer
r.addExecution(caller)
return timer
}
func (r *Routine) pushToStack(caller uintptr) (string, func()) {
r.executionStack = append(r.executionStack, caller)
return encodeCaller(r.executionStack), func() {
r.executionStack = r.executionStack[:len(r.executionStack)-1]
}
}