|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Util\Concurrent; |
|
4
|
|
|
|
|
5
|
|
|
class ProcessQueue extends AbstractQueue implements ProcessQueueInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** The queued items */ |
|
8
|
|
|
public $items = []; |
|
9
|
|
|
|
|
10
|
|
|
/** items index for next take, poll, peek or remove */ |
|
11
|
|
|
public $takeIndex = 0; |
|
12
|
|
|
|
|
13
|
|
|
/** items index for next put, offer, or add */ |
|
14
|
|
|
public $putIndex = 0; |
|
15
|
|
|
|
|
16
|
|
|
/** Number of elements in the queue */ |
|
17
|
|
|
public $count = 0; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Circularly increment i. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function inc(int $i): int |
|
23
|
|
|
{ |
|
24
|
|
|
$i += 1; |
|
25
|
|
|
return ($i === count($this->items)) ? 0 : $i; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Circularly decrement i. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function dec(int $i): int |
|
32
|
|
|
{ |
|
33
|
|
|
return (($i === 0) ? count($this->items) : $i) - 1; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns item at index i. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function itemAt(int $i = null) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($i !== null && $i >= 0 && $i < count($this->items)) { |
|
42
|
|
|
return $this->items[$i]; |
|
43
|
|
|
} |
|
44
|
|
|
return null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Throws NullPointerException if argument is null. |
|
49
|
|
|
* |
|
50
|
|
|
* @param v the element |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
|
|
private static function checkNotNull($v = null): void |
|
53
|
|
|
{ |
|
54
|
|
|
if ($v === null) { |
|
55
|
|
|
throw new \Exception("Object is null"); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Inserts element at current put position, advances |
|
61
|
|
|
*/ |
|
62
|
|
|
private function insert($x, InterruptibleProcess $receiver = null): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->items[$this->putIndex] = $x; |
|
65
|
|
|
$this->putIndex = $this->inc($this->putIndex); |
|
66
|
|
|
$this->count += 1; |
|
67
|
|
|
if ($receiver !== null) { |
|
68
|
|
|
$receiver->push(serialize($x)); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function __construct(int $capacity, bool $fair = false, $c = null) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
if ($capacity < 0) { |
|
75
|
|
|
throw new \Exception("Illegal capacity"); |
|
76
|
|
|
} |
|
77
|
|
|
for ($i = 0; $i < $capacity; $i += 1) { |
|
78
|
|
|
$this->items[] = null; |
|
79
|
|
|
} |
|
80
|
|
|
$i = 0; |
|
81
|
|
|
try { |
|
82
|
|
|
if (is_array($c)) { |
|
83
|
|
|
foreach ($c as $e) { |
|
84
|
|
|
self::checkNotNull($e); |
|
85
|
|
|
$i += 1; |
|
86
|
|
|
$this->items[$i] = $e; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} catch (\Exception $ex) { |
|
90
|
|
|
throw $ex; |
|
91
|
|
|
} |
|
92
|
|
|
$this->count = $i; |
|
93
|
|
|
$this->putIndex = ($i === $capacity) ? 0 : $i; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function offer($e, InterruptibleProcess $receiver = null): bool |
|
97
|
|
|
{ |
|
98
|
|
|
self::checkNotNull($e); |
|
99
|
|
|
if ($this->count === count($this->items)) { |
|
100
|
|
|
return false; |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->insert($e, $receiver); |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function poll(int $timeout, string $unit, InterruptibleProcess $process) |
|
108
|
|
|
{ |
|
109
|
|
|
$nanos = TimeUnit::toNanos($timeout, $unit); |
|
110
|
|
|
time_nanosleep(0, $nanos); |
|
111
|
|
|
return $process->pop(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function take(InterruptibleProcess $process) |
|
115
|
|
|
{ |
|
116
|
|
|
return $process->pop(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function peek() |
|
120
|
|
|
{ |
|
121
|
|
|
return ($this->count === 0) ? null : $this->itemAt($this->takeIndex); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns the number of elements in this queue. |
|
126
|
|
|
* |
|
127
|
|
|
* @return the number of elements in this queue |
|
128
|
|
|
*/ |
|
129
|
|
|
public function size(): int |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->count; |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Removes a single instance of the specified element from this queue, |
|
136
|
|
|
* if it is present. |
|
137
|
|
|
* |
|
138
|
|
|
* @param o element to be removed from this queue, if present |
|
|
|
|
|
|
139
|
|
|
* @return {@code true} if this queue changed as a result of the call |
|
|
|
|
|
|
140
|
|
|
*/ |
|
141
|
|
|
public function remove($o = null) |
|
142
|
|
|
{ |
|
143
|
|
|
if ($o === null) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
for ($i = $this->takeIndex, $k = $this->count; $k > 0; $i = $this->inc($i), $k -= 1) { |
|
147
|
|
|
if ($o === $this->items[$i]) { |
|
148
|
|
|
$this->removeAt($i); |
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function removeAt(int $i): void |
|
156
|
|
|
{ |
|
157
|
|
|
if ($i == $this->takeIndex) { |
|
158
|
|
|
$this->items[$this->takeIndex] = null; |
|
159
|
|
|
$this->takeIndex = $this->inc($this->takeIndex); |
|
160
|
|
|
} else { |
|
161
|
|
|
// slide over all others up through putIndex. |
|
162
|
|
|
for (;;) { |
|
163
|
|
|
$nexti = $this->inc($i); |
|
164
|
|
|
if ($nexti != $this->putIndex) { |
|
165
|
|
|
$this->items[$i] = $this->items[$nexti]; |
|
166
|
|
|
$i = $nexti; |
|
167
|
|
|
} else { |
|
168
|
|
|
$this->items[$i] = null; |
|
169
|
|
|
$this->putIndex = $i; |
|
170
|
|
|
break; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
$this->count -= 1; |
|
175
|
|
|
//notFull.signal(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Returns {@code true} if this queue contains the specified element. |
|
180
|
|
|
* |
|
181
|
|
|
* @param o object to be checked for containment in this queue |
|
182
|
|
|
* @return {@code true} if this queue contains the specified element |
|
|
|
|
|
|
183
|
|
|
*/ |
|
184
|
|
|
public function contains($o): bool |
|
185
|
|
|
{ |
|
186
|
|
|
if ($o === null) { |
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
for ($i = $this->takeIndex, $k = $this->count; $k > 0; $i = $this->inc($i), $k -= 1) { |
|
190
|
|
|
if ($o === $this->items[$i]) { |
|
191
|
|
|
return true; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns an array containing all of the elements in this queue, in |
|
199
|
|
|
* proper sequence. |
|
200
|
|
|
* |
|
201
|
|
|
* @return an array containing all of the elements in this queue |
|
|
|
|
|
|
202
|
|
|
*/ |
|
203
|
|
|
public function toArray(array &$c = null): array |
|
204
|
|
|
{ |
|
205
|
|
|
if ($c == null) { |
|
206
|
|
|
$a = []; |
|
207
|
|
|
for ($i = $this->takeIndex, $k = 0; $k < $this->count; $i = $this->inc($i), $k += 1) { |
|
208
|
|
|
$a[$k] = $this->items[$i]; |
|
209
|
|
|
} |
|
210
|
|
|
return $a; |
|
|
|
|
|
|
211
|
|
|
} elseif (is_array($c)) { |
|
|
|
|
|
|
212
|
|
|
for ($i = $this->takeIndex, $k = 0; $k < $this->count; $i = $this->inc($i), $k += 1) { |
|
213
|
|
|
$c[$k] = $this->items[$i]; |
|
214
|
|
|
} |
|
215
|
|
|
return $c; |
|
|
|
|
|
|
216
|
|
|
} |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Atomically removes all of the elements from this queue. |
|
221
|
|
|
* The queue will be empty after this call returns. |
|
222
|
|
|
*/ |
|
223
|
|
|
public function clear(): void |
|
224
|
|
|
{ |
|
225
|
|
|
for ($i = $this->takeIndex, $k = $this->count; $k > 0; $i = $this->inc($i), $k -= 1) { |
|
226
|
|
|
$this->items[$i] = null; |
|
227
|
|
|
} |
|
228
|
|
|
$this->count = 0; |
|
229
|
|
|
$this->putIndex = 0; |
|
230
|
|
|
$this->takeIndex = 0; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function drainTo(&$c, int $maxElements = null): int |
|
234
|
|
|
{ |
|
235
|
|
|
self::checkNotNull($c); |
|
236
|
|
|
if ($c === $this) { |
|
237
|
|
|
throw new \Exception("Argument must be non-null"); |
|
238
|
|
|
} |
|
239
|
|
|
$i = $this->takeIndex; |
|
240
|
|
|
$n = 0; |
|
241
|
|
|
$max = $maxElements ?? $this->count; |
|
242
|
|
|
while ($n < $max) { |
|
243
|
|
|
$c[] = $this->items[$i]; |
|
244
|
|
|
$this->items[$i] = null; |
|
245
|
|
|
$i = $this->inc($i); |
|
246
|
|
|
$n += 1; |
|
247
|
|
|
} |
|
248
|
|
|
if ($n > 0 && $maxElements === null) { |
|
249
|
|
|
$this->count = 0; |
|
250
|
|
|
$this->putIndex = 0; |
|
251
|
|
|
$this->takeIndex = 0; |
|
252
|
|
|
} elseif ($n > 0) { |
|
253
|
|
|
$this->count -= $n; |
|
254
|
|
|
$this->takeIndex = $i; |
|
255
|
|
|
} |
|
256
|
|
|
return $n; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function iterator() |
|
260
|
|
|
{ |
|
261
|
|
|
return new ProcessIterator($this); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths