Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2017-05-16 20:19:54 +00:00 committed by StyleCI Bot
parent ab1bd5330d
commit 1f35f9fdb7
6 changed files with 374 additions and 269 deletions

View File

@ -1,7 +1,8 @@
<?php <?php
if (!extension_loaded("pthreads")) {
interface Collectable { if (!extension_loaded('pthreads')) {
interface Collectable
{
public function isGarbage(); public function isGarbage();
} }
} }

View File

@ -1,15 +1,17 @@
<?php <?php
if (!extension_loaded("pthreads")) {
class Pool { if (!extension_loaded('pthreads')) {
class Pool
public function __construct($size, $class = \Worker::class, $ctor = []) { {
public function __construct($size, $class = \Worker::class, $ctor = [])
{
$this->size = $size; $this->size = $size;
$this->clazz = $class; $this->clazz = $class;
$this->ctor = $ctor; $this->ctor = $ctor;
} }
public function submit(Threaded $collectable) { public function submit(Threaded $collectable)
{
if ($this->last > $this->size) { if ($this->last > $this->size) {
$this->last = 0; $this->last = 0;
} }
@ -23,31 +25,38 @@ if (!extension_loaded("pthreads")) {
$this->workers[$this->last++]->stack($collectable); $this->workers[$this->last++]->stack($collectable);
} }
public function submitTo($worker, Threaded $collectable) { public function submitTo($worker, Threaded $collectable)
{
if (isset($this->workers[$worker])) { if (isset($this->workers[$worker])) {
$this->workers[$worker]->stack($collectable); $this->workers[$worker]->stack($collectable);
} }
} }
public function collect(Closure $collector = null) { public function collect(Closure $collector = null)
{
$total = 0; $total = 0;
foreach ($this->workers as $worker) foreach ($this->workers as $worker) {
$total += $worker->collect($collector); $total += $worker->collect($collector);
}
return $total; return $total;
} }
public function resize($size) { public function resize($size)
{
if ($size < $this->size) { if ($size < $this->size) {
while ($this->size > $size) { while ($this->size > $size) {
if (isset($this->workers[$this->size-1])) if (isset($this->workers[$this->size - 1])) {
$this->workers[$this->size-1]->shutdown(); $this->workers[$this->size - 1]->shutdown();
unset($this->workers[$this->size-1]); }
unset($this->workers[$this->size - 1]);
$this->size--; $this->size--;
} }
} }
} }
public function shutdown() { public function shutdown()
{
$this->workers = null; $this->workers = null;
} }
@ -58,5 +67,3 @@ if (!extension_loaded("pthreads")) {
protected $ctor; protected $ctor;
} }
} }

View File

@ -1,41 +1,63 @@
<?php <?php
if (!extension_loaded("pthreads")) {
class Thread extends Threaded { if (!extension_loaded('pthreads')) {
public function isStarted() { return (bool) ($this->state & THREAD::STARTED); } class Thread extends Threaded
public function isJoined() { return (bool) ($this->state & THREAD::JOINED); } {
public function kill() { public function isStarted()
$this->state |= THREAD::ERROR; {
return (bool) ($this->state & self::STARTED);
}
public function isJoined()
{
return (bool) ($this->state & self::JOINED);
}
public function kill()
{
$this->state |= self::ERROR;
return true; return true;
} }
public static function getCurrentThreadId() { return 1; } public static function getCurrentThreadId()
public function getThreadId() { return 1; } {
return 1;
}
public function start() { public function getThreadId()
if ($this->state & THREAD::STARTED) { {
return 1;
}
public function start()
{
if ($this->state & self::STARTED) {
throw new \RuntimeException(); throw new \RuntimeException();
} }
$this->state |= THREAD::STARTED; $this->state |= self::STARTED;
$this->state |= THREAD::RUNNING; $this->state |= self::RUNNING;
try { try {
$this->run(); $this->run();
} catch(Exception $t) { } catch (Exception $t) {
$this->state |= THREAD::ERROR; $this->state |= self::ERROR;
} }
$this->state &= ~THREAD::RUNNING; $this->state &= ~self::RUNNING;
return true; return true;
} }
public function join() { public function join()
if ($this->state & THREAD::JOINED) { {
if ($this->state & self::JOINED) {
throw new \RuntimeException(); throw new \RuntimeException();
} }
$this->state |= THREAD::JOINED; $this->state |= self::JOINED;
return true; return true;
} }
} }

View File

@ -1,45 +1,53 @@
<?php <?php
if (!extension_loaded("pthreads")) {
class Threaded implements ArrayAccess, Countable, IteratorAggregate, Collectable { if (!extension_loaded('pthreads')) {
class Threaded implements ArrayAccess, Countable, IteratorAggregate, Collectable
{
const NOTHING = (0); const NOTHING = (0);
const STARTED = (1<<0); const STARTED = (1 << 0);
const RUNNING = (1<<1); const RUNNING = (1 << 1);
const JOINED = (1<<2); const JOINED = (1 << 2);
const ERROR = (1<<3); const ERROR = (1 << 3);
public function offsetSet($offset, $value) { public function offsetSet($offset, $value)
{
$this->__set($offset, $value); $this->__set($offset, $value);
} }
public function offsetGet($offset) { public function offsetGet($offset)
{
return $this->__get($offset); return $this->__get($offset);
} }
public function offsetUnset($offset) { public function offsetUnset($offset)
{
$this->__unset($offset); $this->__unset($offset);
} }
public function offsetExists($offset) { public function offsetExists($offset)
{
return $this->__isset($offset); return $this->__isset($offset);
} }
public function count() { public function count()
{
return count($this->data); return count($this->data);
} }
public function getIterator() { public function getIterator()
{
return new ArrayIterator($this->data); return new ArrayIterator($this->data);
} }
public function __set($offset, $value) { public function __set($offset, $value)
{
if ($offset === null) { if ($offset === null) {
$offset = count($this->data); $offset = count($this->data);
} }
if (!$this instanceof Volatile) { if (!$this instanceof Volatile) {
if (isset($this->data[$offset]) && if (isset($this->data[$offset]) &&
$this->data[$offset] instanceof Threaded) { $this->data[$offset] instanceof self) {
throw new \RuntimeException(); throw new \RuntimeException();
} }
} }
@ -55,80 +63,121 @@ if (!extension_loaded("pthreads")) {
return $this->data[$offset] = $value; return $this->data[$offset] = $value;
} }
public function __get($offset) { public function __get($offset)
{
return $this->data[$offset]; return $this->data[$offset];
} }
public function __isset($offset) { public function __isset($offset)
{
return isset($this->data[$offset]); return isset($this->data[$offset]);
} }
public function __unset($offset) { public function __unset($offset)
{
if (!$this instanceof Volatile) { if (!$this instanceof Volatile) {
if (isset($this->data[$offset]) && $this->data[$offset] instanceof Threaded) { if (isset($this->data[$offset]) && $this->data[$offset] instanceof self) {
throw new \RuntimeException(); throw new \RuntimeException();
} }
} }
unset($this->data[$offset]); unset($this->data[$offset]);
} }
public function shift() { public function shift()
{
return array_shift($this->data); return array_shift($this->data);
} }
public function chunk($size) { public function chunk($size)
{
$chunk = []; $chunk = [];
while (count($chunk) < $size) { while (count($chunk) < $size) {
$chunk[] = $this->shift(); $chunk[] = $this->shift();
} }
return $chunk; return $chunk;
} }
public function pop() { public function pop()
{
return array_pop($this->data); return array_pop($this->data);
} }
public function merge($merge) { public function merge($merge)
{
foreach ($merge as $k => $v) { foreach ($merge as $k => $v) {
$this->data[$k] = $v; $this->data[$k] = $v;
} }
} }
public function wait($timeout = 0) { public function wait($timeout = 0)
{
return true; return true;
} }
public function notify() { public function notify()
{
return true; return true;
} }
public function synchronized(Closure $closure, ... $args) { public function synchronized(Closure $closure, ...$args)
{
return $closure(...$args); return $closure(...$args);
} }
public function isRunning() { public function isRunning()
{
return $this->state & THREAD::RUNNING; return $this->state & THREAD::RUNNING;
} }
public function isTerminated() { public function isTerminated()
{
return $this->state & THREAD::ERROR; return $this->state & THREAD::ERROR;
} }
public static function extend($class) { return true; } public static function extend($class)
{
return true;
}
public function addRef() {} public function addRef()
public function delRef() {} {
public function getRefCount() {} }
public function lock() { return true; } public function delRef()
public function unlock() { return true; } {
public function isWaiting() { return false; } }
public function run() {} public function getRefCount()
{
}
public function isGarbage() { return true; } public function lock()
{
return true;
}
private function convertToVolatile($value) { public function unlock()
{
return true;
}
public function isWaiting()
{
return false;
}
public function run()
{
}
public function isGarbage()
{
return true;
}
private function convertToVolatile($value)
{
/* /*
if (is_array($value)) { if (is_array($value)) {
foreach ($value as $k => $v) { foreach ($value as $k => $v) {

View File

@ -1,15 +1,17 @@
<?php <?php
if (!extension_loaded("pthreads")) {
class Volatile extends Threaded { if (!extension_loaded('pthreads')) {
public function __set($offset, $value) { class Volatile extends Threaded
{
public function __set($offset, $value)
{
if ($offset === null) { if ($offset === null) {
$offset = count($this->data); $offset = count($this->data);
} }
if (is_array($value)) { if (is_array($value)) {
$safety = $safety =
new Volatile(); new self();
$safety->merge( $safety->merge(
$this->convertToVolatile($value)); $this->convertToVolatile($value));
$value = $safety; $value = $safety;

View File

@ -1,8 +1,10 @@
<?php <?php
if (!extension_loaded("pthreads")) {
class Worker extends Thread { if (!extension_loaded('pthreads')) {
public function collect(Closure $collector = null) { class Worker extends Thread
{
public function collect(Closure $collector = null)
{
foreach ($this->gc as $idx => $collectable) { foreach ($this->gc as $idx => $collectable) {
if ($collector) { if ($collector) {
if ($collector($collectable)) { if ($collector($collectable)) {
@ -17,26 +19,50 @@ if (!extension_loaded("pthreads")) {
return count($this->gc) + count($this->stack); return count($this->gc) + count($this->stack);
} }
public function collector(Collectable $collectable) { return $collectable->isGarbage(); }
public function shutdown() { return $this->join(); } public function collector(Collectable $collectable)
public function isShutdown() { return $this->isJoined(); } {
public function getStacked() { return count($this->stack); } return $collectable->isGarbage();
public function unstack() { return array_shift($this->stack); } }
public function stack(Threaded $collectable) {
public function shutdown()
{
return $this->join();
}
public function isShutdown()
{
return $this->isJoined();
}
public function getStacked()
{
return count($this->stack);
}
public function unstack()
{
return array_shift($this->stack);
}
public function stack(Threaded $collectable)
{
$this->stack[] = $collectable; $this->stack[] = $collectable;
if ($this->isStarted()) { if ($this->isStarted()) {
$this->runCollectable(count($this->stack)-1, $collectable); $this->runCollectable(count($this->stack) - 1, $collectable);
} }
} }
public function run() { public function run()
{
foreach ($this->stack as $idx => $collectable) { foreach ($this->stack as $idx => $collectable) {
$this $this
->runCollectable($idx, $collectable); ->runCollectable($idx, $collectable);
} }
} }
private function runCollectable($idx, Collectable $collectable) { private function runCollectable($idx, Collectable $collectable)
{
$collectable->worker = $this; $collectable->worker = $this;
$collectable->state |= THREAD::RUNNING; $collectable->state |= THREAD::RUNNING;
$collectable->run(); $collectable->run();
@ -49,5 +75,3 @@ if (!extension_loaded("pthreads")) {
private $gc = []; private $gc = [];
} }
} }