PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
TaskScheduler.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
28namespace pocketmine\scheduler;
29
32
34 private bool $enabled = true;
35
38
43 protected ObjectSet $tasks;
44
45 protected int $currentTick = 0;
46
47 public function __construct(
48 private ?string $owner = null
49 ){
50 $this->queue = new ReversePriorityQueue();
51 $this->tasks = new ObjectSet();
52 }
53
60 public function scheduleTask(Task $task) : TaskHandler{
61 return $this->addTask($task, -1, -1);
62 }
63
70 public function scheduleDelayedTask(Task $task, int $delay) : TaskHandler{
71 return $this->addTask($task, $delay, -1);
72 }
73
80 public function scheduleRepeatingTask(Task $task, int $period) : TaskHandler{
81 return $this->addTask($task, -1, $period);
82 }
83
90 public function scheduleDelayedRepeatingTask(Task $task, int $delay, int $period) : TaskHandler{
91 return $this->addTask($task, $delay, $period);
92 }
93
94 public function cancelAllTasks() : void{
95 foreach($this->tasks as $id => $task){
96 $task->cancel();
97 }
98 $this->tasks->clear();
99 while(!$this->queue->isEmpty()){
100 $this->queue->extract();
101 }
102 }
103
107 public function isQueued(TaskHandler $task) : bool{
108 return $this->tasks->contains($task);
109 }
110
117 private function addTask(Task $task, int $delay, int $period) : TaskHandler{
118 if(!$this->enabled){
119 throw new \LogicException("Tried to schedule task to disabled scheduler");
120 }
121
122 if($delay <= 0){
123 $delay = -1;
124 }
125
126 if($period <= -1){
127 $period = -1;
128 }elseif($period < 1){
129 $period = 1;
130 }
131
132 return $this->handle(new TaskHandler($task, $delay, $period, $this->owner));
133 }
134
140 private function handle(TaskHandler $handler) : TaskHandler{
141 if($handler->isDelayed()){
142 $nextRun = $this->currentTick + $handler->getDelay();
143 }else{
144 $nextRun = $this->currentTick;
145 }
146
147 $handler->setNextRun($nextRun);
148 $this->tasks->add($handler);
149 $this->queue->insert($handler, $nextRun);
150
151 return $handler;
152 }
153
154 public function shutdown() : void{
155 $this->enabled = false;
156 $this->cancelAllTasks();
157 }
158
159 public function setEnabled(bool $enabled) : void{
160 $this->enabled = $enabled;
161 }
162
163 public function mainThreadHeartbeat(int $currentTick) : void{
164 if(!$this->enabled){
165 throw new \LogicException("Cannot run heartbeat on a disabled scheduler");
166 }
167 $this->currentTick = $currentTick;
168 while($this->isReady($this->currentTick)){
170 $task = $this->queue->extract();
171 if($task->isCancelled()){
172 $this->tasks->remove($task);
173 continue;
174 }
175 $task->run();
176 if(!$task->isCancelled() && $task->isRepeating()){
177 $task->setNextRun($this->currentTick + $task->getPeriod());
178 $this->queue->insert($task, $this->currentTick + $task->getPeriod());
179 }else{
180 $task->remove();
181 $this->tasks->remove($task);
182 }
183 }
184 }
185
186 private function isReady(int $currentTick) : bool{
187 return !$this->queue->isEmpty() && $this->queue->current()->getNextRun() <= $currentTick;
188 }
189}
scheduleDelayedTask(Task $task, int $delay)
scheduleDelayedRepeatingTask(Task $task, int $delay, int $period)
scheduleRepeatingTask(Task $task, int $period)