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
if (!extension_loaded("pthreads")) {
interface Collectable {
if (!extension_loaded('pthreads')) {
interface Collectable
{
public function isGarbage();
}
}

View File

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

View File

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

View File

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

View File

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

View File

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