forked from amit-davidson/Chronos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.go
59 lines (51 loc) · 1.44 KB
/
Context.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
package domain
import (
"github.com/pdufour/Chronos/utils/stacks"
)
// Flow context
type Context struct {
GoroutineID int
Clock VectorClock
StackTrace *stacks.IntStackWithMap
}
func NewEmptyContext() *Context {
return &Context{
Clock: VectorClock{},
GoroutineID: GoroutineCounter.GetNext(),
StackTrace: stacks.NewEmptyIntStackWithMap(),
}
}
func NewGoroutineExecutionState(state *Context) *Context {
state.Increment()
return &Context{
Clock: state.Clock.Copy(),
GoroutineID: GoroutineCounter.GetNext(),
StackTrace: state.StackTrace.Copy(),
}
}
func (gs *Context) Increment() {
gs.Clock[gs.GoroutineID] += 1
}
func (gs *Context) MayConcurrent(state *Context) bool {
timestampAidA := gs.Clock.Get(gs.GoroutineID)
timestampAidB := state.Clock.Get(gs.GoroutineID)
timestampBidA := gs.Clock.Get(state.GoroutineID)
timestampBidB := state.Clock.Get(state.GoroutineID)
isBefore := timestampAidA <= timestampAidB && timestampBidA < timestampBidB
isAfter := timestampBidB <= timestampBidA && timestampAidB < timestampAidA
return !(isBefore || isAfter)
}
func (gs *Context) Copy() *Context {
return &Context{
GoroutineID: gs.GoroutineID,
Clock: gs.Clock.Copy(),
StackTrace: gs.StackTrace.Copy(),
}
}
func (gs *Context) CopyWithoutMap() *Context {
return &Context{
GoroutineID: gs.GoroutineID,
Clock: gs.Clock.Copy(),
StackTrace: stacks.NewIntStackWithMap(*gs.StackTrace.GetItems().Copy(), nil),
}
}