-
Notifications
You must be signed in to change notification settings - Fork 21
/
rwmutex.go
200 lines (166 loc) · 4.49 KB
/
rwmutex.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package core
// +build !race
import (
"fmt"
"github.com/MG-RAST/AWE/lib/core/uuid"
"github.com/MG-RAST/AWE/lib/logger"
"strings"
"sync"
"time"
)
// This RWLock keeps track of the owener and of each reader
// Because channels are used for the locking mechanism, you can use locks with timeouts.
// Each reader gets a ReadLock object that has to be used to unlock after reading is done.
type ReadLock struct {
id string
}
type RWMutex struct {
//sync.RWMutex `bson:"-" json:"-"` // Locks only members that can change. Current_work has its own lock.
writeLock chan int
lockOwner StringLocked `bson:"-" json:"-"`
Name string `bson:"-" json:"-"`
readers map[string]string // map[Id]Name (named readers)
anonCounter int // (anonymous readers, use only when necessary)
readLock sync.Mutex
}
type StringLocked struct {
sync.Mutex
value string
}
func (s *StringLocked) Set(value string) {
s.Lock()
defer s.Unlock()
s.value = value
}
func (s *StringLocked) Get() string {
s.Lock()
defer s.Unlock()
return s.value
}
func (m *RWMutex) Init(name string) {
m.Name = name
m.writeLock = make(chan int, 1)
m.readers = make(map[string]string)
m.anonCounter = 0
m.lockOwner.Set("nobody_init")
if name == "" {
panic("name not defined")
}
m.writeLock <- 1 // Put the initial value into the channel
return
}
func (r *ReadLock) Get_Id() string {
return r.id
}
func (m *RWMutex) Lock() (err error) {
panic("Lock() was called")
err = m.LockNamed("unknown")
return
}
func (m *RWMutex) LockNamed(name string) (err error) {
//logger.Debug(3, "Lock")
//reader_list := strings.Join(m.RList(), ",")
//logger.Debug(3, "(%s) %s requests Lock. current owner: %s (reader list :%s)", m.Name, name, m.lockOwner.Get(), reader_list) // reading m.lockOwner induces data race !
if m.Name == "" {
panic("LockNamed: object has no name")
}
//m.RWMutex.Lock()
initial_owner := m.lockOwner.Get()
//timer := time.NewTimer(time.Second * 100)
select {
case <-m.writeLock: // Grab the ticket
logger.Debug(3, "(RWMutex/LockNamed %s) got lock", name)
case <-time.After(time.Second * 20): //1800
//elapsed_time := time.Since(start_time)
reader_list := strings.Join(m.RList(), ",")
message := fmt.Sprintf("(%s) %s requests Lock. TIMEOUT!!! current owner: %s (reader list :%s), initial owner: %s", m.Name, name, m.lockOwner.Get(), reader_list, initial_owner)
logger.Error(message)
err = fmt.Errorf(message)
return
}
//if !timer.Stop() {
// <-timer.C
//}
//<-m.writeLock // Grab the ticket
m.lockOwner.Set(name)
// wait for Readers to leave....
for m.RCount() > 0 {
time.Sleep(100)
}
logger.Debug(3, "(%s) LOCKED by %s", m.Name, name)
return
}
func (m *RWMutex) Unlock() {
//logger.Debug(3, "Unlock")
old_owner := m.lockOwner.Get()
m.lockOwner.Set("nobody_anymore")
//m.RWMutex.Unlock()
m.writeLock <- 1 // Give it back
logger.Debug(3, "(%s) UNLOCKED by %s **********************", m.Name, old_owner)
}
func (m *RWMutex) RLockNamed(name string) (rl ReadLock, err error) {
logger.Debug(3, "(%s, %s) request RLock and Lock.", m.Name, name)
if m.Name == "" {
panic("xzy name empty")
}
err = m.LockNamed(m.Name + "/RLock " + name)
if err != nil {
return
}
logger.Debug(3, "(%s) RLock/%s got Lock.", m.Name, name)
m.readLock.Lock()
new_uuid := uuid.New()
m.readers[new_uuid] = name
//m.readCounter += 1
m.readLock.Unlock()
m.Unlock()
logger.Debug(3, "(%s) got RLock.", m.Name)
rl = ReadLock{id: new_uuid}
return
}
func (m *RWMutex) RLock() {
panic("RLock() was called")
}
func (m *RWMutex) RUnlock() {
panic("RUnlock() was called")
}
func (m *RWMutex) RLockAnon() {
m.readLock.Lock()
defer m.readLock.Unlock()
m.anonCounter += 1
}
func (m *RWMutex) RUnlockAnon() {
m.readLock.Lock()
defer m.readLock.Unlock()
m.anonCounter -= 1
}
func (m *RWMutex) RUnlockNamed(rl ReadLock) {
lock_uuid := rl.Get_Id()
logger.Debug(3, "(%s) request RUnlock.", m.Name)
m.readLock.Lock()
//m.readCounter -= 1
name, ok := m.readers[lock_uuid]
if ok {
delete(m.readers, lock_uuid)
logger.Debug(3, "(%s) %s did RUnlock.", m.Name, name)
} else {
logger.Debug(3, "(%s) ERROR: %s did not have RLock !?!??!?!?.", m.Name, name)
}
m.readLock.Unlock()
logger.Debug(3, "(%s) did RUnlock.", m.Name)
}
func (m *RWMutex) RCount() (c int) {
m.readLock.Lock()
c = len(m.readers)
c += m.anonCounter
m.readLock.Unlock()
return
}
func (m *RWMutex) RList() (list []string) {
m.readLock.Lock()
for _, v := range m.readers {
list = append(list, v)
}
m.readLock.Unlock()
return
}