forked from amit-davidson/Chronos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuardedAccess.go
110 lines (95 loc) · 2.28 KB
/
GuardedAccess.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 domain
import (
"github.com/pdufour/Chronos/ssaPureUtils"
"go/token"
"golang.org/x/tools/go/ssa"
)
type OpKind int
const (
GuardAccessRead OpKind = iota
GuardAccessWrite
)
func (op OpKind) String() string {
switch op {
case GuardAccessRead:
return "Read"
case GuardAccessWrite:
return "Write"
default:
return "Unknown op type"
}
}
type GuardedAccess struct {
*PosData
*FlowData
}
type PosData struct {
PosID int // guarded accesses of the same function share the same PosID. It's used to mark the same guarded access in different flows.
Pos token.Pos
OpKind OpKind
Value ssa.Value
}
type FlowData struct {
PosToRemove int
ID int // ID depends on the flow, which means it's unique.
State *Context
Lockset *Lockset
}
func (ga *FlowData) Copy() *FlowData {
return &FlowData{
ID: ga.ID,
PosToRemove: ga.PosToRemove,
Lockset: ga.Lockset.Copy(),
State: ga.State.CopyWithoutMap(),
}
}
func (ga *GuardedAccess) Copy() *GuardedAccess {
return &GuardedAccess{
PosData: ga.PosData,
FlowData: ga.FlowData.Copy(),
}
}
func (ga *GuardedAccess) ShallowCopy() *GuardedAccess {
return &GuardedAccess{
PosData: ga.PosData,
FlowData: ga.FlowData.Copy(),
}
}
func (ga *GuardedAccess) Intersects(gaToCompare *GuardedAccess) bool {
if ga.ID == gaToCompare.ID || ga.State.GoroutineID == gaToCompare.State.GoroutineID {
return true
}
if ga.OpKind == GuardAccessRead && gaToCompare.OpKind == GuardAccessRead {
return true
}
if ssaPureUtils.FilterStructs(ga.Value, gaToCompare.Value) {
return true
}
for lockA := range ga.Lockset.Locks {
for lockB := range gaToCompare.Lockset.Locks {
if lockA == lockB {
return true
}
}
}
return false
}
func (ga *GuardedAccess) IsConflicting(gaToCompare *GuardedAccess) bool {
return !ga.Intersects(gaToCompare) && ga.State.MayConcurrent(gaToCompare.State)
}
func AddGuardedAccess(pos token.Pos, value ssa.Value, kind OpKind, lockset *Lockset, context *Context) *GuardedAccess {
context.Increment()
return &GuardedAccess{
PosData: &PosData{
PosID: PosIDCounter.GetNext(),
Pos: pos,
OpKind: kind,
Value: value,
},
FlowData: &FlowData{
ID: GuardedAccessCounter.GetNext(),
Lockset: lockset.Copy(),
State: context.CopyWithoutMap(),
},
}
}