-
Notifications
You must be signed in to change notification settings - Fork 31
/
queue_lib.gd
263 lines (222 loc) · 4.89 KB
/
queue_lib.gd
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# This file is part of Unidot Importer. See LICENSE.txt for full MIT license.
# Copyright (c) 2021-present Lyuma <[email protected]> and contributors
# SPDX-License-Identifier: MIT
@tool
class Deque:
extends RefCounted
var _arr: Array = []
var _start: int = 0
var _end: int = 0
var _mutex: RefCounted = null
class DummyMutex:
extends RefCounted
var orig_mutex: RefCounted = null
func lock():
pass
func unlock():
pass
func try_lock():
return OK
func _init():
_arr = [].duplicate()
_mutex = DummyMutex.new()
func _grow():
if len(_arr) == 0:
_arr = [null].duplicate()
_start = 0
_end = 0
return 0
elif _start == _end:
var sz: int = len(_arr)
# print("Grow array " + str(_start) + "/" + str(sz))
var arr_slice: Array = _arr.slice(_start, sz)
_arr.resize(_start)
_arr.resize(sz + _start)
_arr.append_array(arr_slice)
_start += sz
# print("Grow array done " + str(_start) + "/" + str(sz))
func push_back(el: Variant):
_mutex.lock()
_grow()
_arr[_end] = el
_end = (_end + 1) % len(_arr)
_mutex.unlock()
func push_front(el: Variant):
_mutex.lock()
_grow()
_start = (_start + len(_arr) - 1) % len(_arr)
_arr[_start] = el
_mutex.unlock()
func back() -> Variant:
_mutex.lock()
if len(_arr) == 0:
_mutex.unlock()
return null
var idx: int = (_end + len(_arr) - 1) % len(_arr)
var ret: Variant = _arr[idx]
_mutex.unlock()
return ret
func front() -> Variant:
_mutex.lock()
if len(_arr) == 0:
_mutex.unlock()
return null
_mutex.unlock()
return _arr[_start]
func pop_back() -> Variant:
_mutex.lock()
if len(_arr) == 0:
_mutex.unlock()
return null
_end = (_end + len(_arr) - 1) % len(_arr)
var el: Variant = _arr[_end]
_arr[_end] = null
if _start == _end:
_arr = [].duplicate()
_mutex.unlock()
return el
func pop_front() -> Variant:
_mutex.lock()
if len(_arr) == 0:
_mutex.unlock()
return null
var el: Variant = _arr[_start]
_arr[_start] = null
_start = (_start + 1) % len(_arr)
if _start == _end:
_arr = [].duplicate()
_mutex.unlock()
return el
func clear():
_mutex.lock()
_arr = [].duplicate()
_mutex.unlock()
func size():
_mutex.lock()
var s: int = _start
var e: int = _end
var sz: int = len(_arr)
_mutex.unlock()
if sz == 0:
return 0
if e > s:
return e - s
return e + sz - s
func lock():
_mutex.lock()
func try_lock():
return _mutex.try_lock()
func unlock():
_mutex.unlock()
# Iteration functions are not locked. Please surround loops with explicit lock()/unlock()
func _iter_init(arg):
arg[0] = _start
return len(_arr) != 0
func _iter_next(arg):
arg[0] = (arg[0] + 1) % len(_arr)
return arg[0] != _end
func _iter_get(arg):
return _arr[arg[0]]
class Queue:
extends Deque
func _init():
_arr = [].duplicate()
_mutex = DummyMutex.new()
func push(v: Variant):
push_back(v)
func pop() -> Variant:
return pop_front()
func peek() -> Variant:
return front()
class LockedDeque:
extends Deque
func _init():
_arr = [].duplicate()
_mutex = Mutex.new()
class LockedQueue:
extends Queue
func _init():
_arr = [].duplicate()
_mutex = Mutex.new()
class BlockingQueue:
extends LockedQueue
var _semaphore: Semaphore = null
func _init():
_arr = [].duplicate()
_mutex = Mutex.new()
_semaphore = Semaphore.new()
func push(v: Variant):
push_back(v)
_semaphore.post()
# Old Godot 3.2 api?
#while _semaphore.post() != OK:
# printerr("Failed to post to BlockingQueue semaphore")
# lock()
# unlock()
func pop() -> Variant:
_semaphore.wait()
# Old Godot 3.2 api?
#while _semaphore.wait() != OK:
# printerr("Failed to wait for BlockingQueue semaphore")
# lock()
# unlock()
return pop_front()
func _thread_function(bq: BlockingQueue, some_int: int):
assert(bq.pop() == 1)
assert(bq.pop() == 2)
assert(bq.pop() == 3)
print("Thread Finished")
return "Success"
func run_test():
var q: Queue = Queue.new()
q.push(1)
q.push(2)
q.push(3)
assert(q.pop() == 1)
q.push(4)
assert(q.pop() == 2)
assert(q.pop() == 3)
assert(q.pop() == 4)
assert(q.pop() == null)
q.push(5)
q.push(6)
assert(q.pop() == 5)
assert(q.pop() == 6)
assert(q.pop() == null)
q.push(7)
q.push(8)
q.push(9)
q.push(10)
assert(q.pop() == 7)
assert(q.pop() == 8)
assert(q.pop() == 9)
assert(q.pop() == 10)
assert(q.pop() == null)
q.push(11)
q.push(12)
q.push(13)
q.push(14)
q.push(15)
q.push(16)
q.push(17)
q.push(18)
assert(q.pop() == 11)
assert(q.pop() == 12)
assert(q.pop() == 13)
assert(q.pop() == 14)
assert(q.pop() == 15)
assert(q.pop() == 16)
assert(q.pop() == 17)
assert(q.pop() == 18)
assert(q.pop() == null)
print("Test 1 Finished")
var bq: BlockingQueue = BlockingQueue.new()
var t: Thread = Thread.new()
bq.push(1)
t.start(self._thread_function.bind(bq))
OS.delay_msec(100)
bq.push(2)
bq.push(3)
assert(t.wait_to_finish() == "Success")
assert(null == bq.pop_back())
print("Test 2 Finished")