-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRWLock.hx
211 lines (161 loc) · 5.08 KB
/
RWLock.hx
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
201
202
203
204
205
206
207
208
209
210
211
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package hx.concurrent.lock;
import hx.concurrent.internal.Dates;
import hx.concurrent.internal.Ints;
import hx.concurrent.lock.Acquirable.AbstractAcquirable;
import hx.concurrent.thread.Threads;
/**
* A upgradeable re-entrant read-write lock where locks can only be released by the same thread that acquired them.
*
* A thread can acquire a write-lock (upgrade) while holding a read-lock if no other thread holds a read-lock at the same time.
*
* https://stackoverflow.com/questions/2332765/lock-mutex-semaphore-whats-the-difference
*/
@:allow(hx.concurrent.lock.ReadLock)
@:allow(hx.concurrent.lock.WriteLock)
@:nullSafety(Off)
class RWLock {
public final readLock:ReadLock;
public final writeLock:WriteLock;
final sync = new RLock();
inline //
public function new() {
readLock = new ReadLock(this);
writeLock = new WriteLock(this);
}
}
@:allow(hx.concurrent.lock.RWLock)
@:allow(hx.concurrent.lock.WriteLock)
class ReadLock extends AbstractAcquirable {
function get_availablePermits():Int {
if (rwLock.writeLock.isAcquiredByOtherThread)
return 0;
return rwLock.sync.execute(() -> Ints.MAX_VALUE - holders.length);
}
/**
* Indicates if the lock is acquired by any thread
*/
public var isAcquiredByAnyThread(get, never):Bool;
inline function get_isAcquiredByAnyThread():Bool
return rwLock.sync.execute(() -> holders.length > 0);
/**
* Indicates if the lock is acquired by the current thread
*/
public var isAcquiredByCurrentThread(get, never):Bool;
inline function get_isAcquiredByCurrentThread():Bool
return rwLock.sync.execute(() -> holders.indexOf(Threads.current) > -1);
/**
* Indicates if the lock is acquired by any other thread
*/
public var isAcquiredByOtherThread(get, never):Bool;
inline function get_isAcquiredByOtherThread():Bool {
final requestor = Threads.current;
return rwLock.sync.execute(function() {
if (holders.length == 0)
return false;
for (holder in holders)
if (holder != requestor)
return true;
return false;
});
}
final rwLock:RWLock;
final holders = new Array<Dynamic>();
inline //
function new(rwLock:RWLock) {
this.rwLock = rwLock;
}
public function acquire():Void {
while (true) {
if (tryAcquire(Ints.MAX_VALUE))
return;
}
}
public function tryAcquire(timeoutMS:Int = 0):Bool {
final requestor = Threads.current;
final startAt = Dates.now();
while (true) {
if (rwLock.sync.execute(function() {
if (rwLock.writeLock.isAcquiredByOtherThread)
return false;
holders.push(requestor);
return true;
}))
return true;
final elapsedMS = Dates.now() - startAt;
if (elapsedMS >= timeoutMS)
return false;
}
}
public function release():Void
rwLock.sync.execute(function() {
if (!holders.remove(Threads.current))
throw "This lock was not acquired by the current thread!";
});
}
@:allow(hx.concurrent.lock.RWLock)
@:allow(hx.concurrent.lock.ReadLock)
class WriteLock extends RLock {
final rwLock:RWLock;
inline //
function new(rwLock:RWLock) {
super();
this.rwLock = rwLock;
}
override //
function get_availablePermits():Int {
if (isAcquiredByAnyThread)
return 0;
return rwLock.sync.execute(function() {
final readLockHolders = rwLock.readLock.holders;
// no read locks?
if (readLockHolders.length == 0)
return 1;
// read locks held by other threads?
final requestor = Threads.current;
for (holder in readLockHolders)
if (holder != requestor)
return 0;
return 1;
});
}
override //
public function acquire():Void {
while (true) {
if (tryAcquire(Ints.MAX_VALUE))
return;
}
}
override //
public function tryAcquire(timeoutMS = 0):Bool {
final requestor = Threads.current;
final readLockHolders = rwLock.readLock.holders;
#if (flash || sys)
return Threads.await(function() {
return rwLock.sync.execute(function() {
#end
if (readLockHolders.length > 0) {
// read locks held by other threads?
for (holder in readLockHolders) {
if (holder != requestor)
return false;
}
}
return super_tryAcquire(50);
#if (flash || sys)
});
}, timeoutMS);
#end
}
override //
public function release():Void
rwLock.sync.execute(() -> super_release());
function super_tryAcquire(timeoutMS = 0):Bool
return super.tryAcquire(timeoutMS);
function super_release():Void
return super.release();
}