MadelineProto/src/Worker.php

81 lines
2.1 KiB
PHP
Raw Normal View History

2017-05-16 22:19:42 +02:00
<?php
2017-05-16 22:19:54 +02:00
if (!extension_loaded('pthreads')) {
class Worker extends Thread
{
public function collect(Closure $collector = null)
{
foreach ($this->gc as $idx => $collectable) {
if ($collector) {
if ($collector($collectable)) {
unset($this->gc[$idx]);
}
} else {
if ($this->collector($collectable)) {
unset($this->gc[$idx]);
}
}
}
return count($this->gc) + count($this->stack);
}
public function collector(Collectable $collectable)
{
return $collectable->isGarbage();
}
public function shutdown()
{
return $this->join();
}
public function isShutdown()
{
return $this->isJoined();
}
public function getStacked()
{
return count($this->stack);
}
2017-05-16 22:19:42 +02:00
2017-05-16 22:19:54 +02:00
public function unstack()
{
return array_shift($this->stack);
}
public function stack(Threaded $collectable)
{
$this->stack[] = $collectable;
if ($this->isStarted()) {
$this->runCollectable(count($this->stack) - 1, $collectable);
}
}
public function run()
{
foreach ($this->stack as $idx => $collectable) {
$this
->runCollectable($idx, $collectable);
}
}
private function runCollectable($idx, Collectable $collectable)
{
$collectable->worker = $this;
2017-05-19 21:34:33 +02:00
if (!isset($collectable->state)) {
$collectable->state = 0;
}
2017-05-16 22:19:54 +02:00
$collectable->state |= THREAD::RUNNING;
$collectable->run();
$collectable->state &= ~THREAD::RUNNING;
$this->gc[] = $collectable;
unset($this->stack[$idx]);
}
private $stack = [];
private $gc = [];
}
}