-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
152 lines (123 loc) · 3.67 KB
/
monitor.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package dmon
import (
"strconv"
"sync"
)
// Monitor is distributed monitor structure handling data, token exchange and it's Conditional variables
type Monitor struct {
mid string
env *Env
local *sync.Mutex
keepToken bool
conditionals map[string]*Conditional
data *map[string]interface{}
RN map[string]int
token *token
tokenChan chan bool
lastSignaled []string
}
func newMonitor(mid string, env *Env) *Monitor {
monitor := Monitor{
mid: mid,
env: env,
local: &sync.Mutex{},
keepToken: false,
conditionals: map[string]*Conditional{},
data: &map[string]interface{}{},
RN: map[string]int{},
token: nil,
tokenChan: make(chan bool),
lastSignaled: []string{},
}
monitor.RN[env.address] = 0
for address := range env.sockets {
monitor.RN[address] = 0
}
lowestAddress := env.getLowestAddress()
if lowestAddress == env.address {
monitor.token = newToken(&monitor)
}
return &monitor
}
// RegisterSharedData registers shared data for defined Monitor (comma-separated pointers for variables)
func (mon *Monitor) RegisterSharedData(data ...interface{}) {
for id, value := range data {
(*mon.data)[strconv.Itoa(id)] = value
}
}
// Enter requests to enter distributed critical section for defined Monitor
func (mon *Monitor) Enter() {
mon.local.Lock()
if mon.token == nil {
mon.RN[mon.env.address]++
requestMsg, _ := serializeRequestCSMessage(mon.env.address, mon.mid, mon.RN[mon.env.address])
mon.env.broadcast(requestMsg)
mon.local.Unlock()
<-mon.tokenChan
mon.local.Lock()
}
mon.keepToken = true
mon.local.Unlock()
}
// Exit leaves distributed critical section for defined Monitor
func (mon *Monitor) Exit() {
mon.local.Lock()
mon.token.LRN[mon.env.address] = mon.RN[mon.env.address]
mon.token.updateQ(mon)
address, _ := mon.token.lastSignaledOrPop()
if address != "" {
mon.sendToken(address)
}
mon.keepToken = false
mon.local.Unlock()
}
// NewConditional creates new Conditional variable for defined Monitor
func (mon *Monitor) NewConditional() *Conditional {
idx := strconv.Itoa(len(mon.conditionals))
cond := newConditional(mon, idx)
mon.conditionals[idx] = cond
return cond
}
func (mon *Monitor) sendToken(address string) {
mon.token.serializeData(mon.data)
mon.token.serializeCondWaiting(&mon.conditionals)
tokenMsg, _ := serializeTokenMessage(mon.mid, mon.token)
mon.env.send(address, tokenMsg)
mon.token = nil
}
// message handlers
func (mon *Monitor) handleRequestCSMessage(data []byte) {
requestCS, _ := deserializeRequestCSMessage(data)
mon.local.Lock()
if requestCS.SN > mon.RN[requestCS.From] {
mon.RN[requestCS.From] = requestCS.SN
}
if mon.token != nil && !mon.keepToken && mon.RN[requestCS.From] == mon.token.LRN[requestCS.From]+1 {
mon.sendToken(requestCS.From)
}
mon.local.Unlock()
}
func (mon *Monitor) handleTokenMessage(data []byte) {
token, _ := deserializeTokenMessage(data)
mon.local.Lock()
mon.token = token
mon.token.deserializeData(mon.data, mon)
mon.token.deserializeCondWaiting(&mon.conditionals)
mon.keepToken = true
mon.local.Unlock()
mon.tokenChan <- true
}
func (mon *Monitor) handleConditionalSignalMessage(data []byte) {
signalMsg, _ := deserializeConditionalSignalMessage(data)
mon.local.Lock()
mon.conditionals[signalMsg.Cid].receiveSignal()
mon.local.Unlock()
}
// Synchronized is method imitating synchronized block. It's higher order function that takes Monitor and function without argument, that's executed within distributed critical section defined by Monitor
func Synchronized(mon *Monitor) func(f func()) {
return func(run func()) {
mon.Enter()
run()
mon.Exit()
}
}