|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Util\Timer; |
|
4
|
|
|
|
|
5
|
|
|
class TimerThread extends \Thread |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* This flag is set to false by the reaper to inform us that there |
|
9
|
|
|
* are no more live references to our Timer object. Once this flag |
|
10
|
|
|
* is true and there are no more tasks in our queue, there is no |
|
11
|
|
|
* work left for us to do, so we terminate gracefully. Note that |
|
12
|
|
|
* this field is protected by queue's monitor! |
|
13
|
|
|
*/ |
|
14
|
|
|
public $newTasksMayBeScheduled = true; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Our Timer's queue. We store this reference in preference to |
|
18
|
|
|
* a reference to the Timer so the reference graph remains acyclic. |
|
19
|
|
|
* Otherwise, the Timer would never be garbage-collected and this |
|
20
|
|
|
* thread would never go away. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $queue; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(TaskQueue $queue) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->queue = $queue; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function run(): void |
|
30
|
|
|
{ |
|
31
|
|
|
try { |
|
32
|
|
|
$this->mainLoop(); |
|
33
|
|
|
} finally { |
|
34
|
|
|
// Someone killed this Thread, behave as if Timer cancelled |
|
35
|
|
|
$this->queue->synchronized(function ($scope) { |
|
36
|
|
|
$scope->newTasksMayBeScheduled = false; |
|
37
|
|
|
$scope->queue->clear(); // Eliminate obsolete references |
|
38
|
|
|
}, $this); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The main timer loop. (See class comment.) |
|
44
|
|
|
*/ |
|
45
|
|
|
private function mainLoop(): void |
|
46
|
|
|
{ |
|
47
|
|
|
while (true) { |
|
48
|
|
|
try { |
|
49
|
|
|
$task = null; |
|
50
|
|
|
$taskFired = false; |
|
51
|
|
|
$this->queue->synchronized(function ($scope, $taskFired, $task) { |
|
|
|
|
|
|
52
|
|
|
// Wait for queue to become non-empty |
|
53
|
|
|
while ($scope->queue->isEmpty() && $scope->newTasksMayBeScheduled) { |
|
54
|
|
|
$scope->queue->wait(); |
|
55
|
|
|
} |
|
56
|
|
|
if ($scope->queue->isEmpty()) { |
|
57
|
|
|
return; // Queue is empty and will forever remain; die |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Queue nonempty; look at first evt and do the right thing |
|
61
|
|
|
$currentTime = null; |
|
62
|
|
|
$executionTime = null; |
|
63
|
|
|
$task = $scope->queue->getMin(); |
|
64
|
|
|
$task->lock->synchronized(function ($scope, $task, $currentTime, $executionTime, $taskFired) { |
|
|
|
|
|
|
65
|
|
|
if ($task->state == TimerTask::CANCELLED) { |
|
66
|
|
|
$scope->queue->removeMin(); |
|
67
|
|
|
$scope->mainLoop();// No action required, poll queue again |
|
68
|
|
|
} |
|
69
|
|
|
$currentTime = floor(microtime(true) * 1000); |
|
70
|
|
|
$executionTime = $task->nextExecutionTime; |
|
71
|
|
|
if ($taskFired = ($executionTime <= $currentTime)) { |
|
|
|
|
|
|
72
|
|
|
if ($task->period == 0) { // Non-repeating, remove |
|
73
|
|
|
$scope->queue->removeMin(); |
|
74
|
|
|
$task->state = TimerTask::EXECUTED; |
|
75
|
|
|
} else { // Repeating task, reschedule |
|
76
|
|
|
$scope->queue->rescheduleMin( |
|
77
|
|
|
$task->period < 0 ? $currentTime - $task->period |
|
78
|
|
|
: $executionTime + $task->period |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
}, $scope, $task, $currentTime, $executionTime, $taskFired); |
|
83
|
|
|
if (!$taskFired) {// Task hasn't yet fired; wait |
|
84
|
|
|
$scope->queue->wait(($executionTime - $currentTime) / 1000); //wait(time) where time is in microseconds |
|
85
|
|
|
} |
|
86
|
|
|
}, $this, $taskFired, $task); |
|
87
|
|
|
if ($taskFired) {// Task fired; run it, holding no locks |
|
88
|
|
|
$task->run(); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.