MadelineProto/src/Threaded.php

187 lines
3.9 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 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);
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
public function offsetGet($offset)
{
return $this->__get($offset);
}
public function offsetUnset($offset)
{
$this->__unset($offset);
}
public function offsetExists($offset)
{
return $this->__isset($offset);
}
public function count()
{
2017-05-17 23:51:39 +02:00
return count((array) $this);
2017-05-16 22:19:54 +02:00
}
public function getIterator()
{
2017-05-17 23:51:13 +02:00
return new ArrayIterator($this);
2017-05-16 22:19:54 +02:00
}
public function __set($offset, $value)
{
if ($offset === null) {
2017-05-17 23:51:13 +02:00
$offset = count($this);
2017-05-16 22:19:54 +02:00
}
if (!$this instanceof Volatile) {
2017-05-17 23:51:13 +02:00
if (isset($this->{$offset}) &&
$this->{$offset} instanceof self) {
2017-05-16 22:19:54 +02:00
throw new \RuntimeException();
}
}
2017-05-17 23:51:13 +02:00
return $this->{$offset} = $value;
2017-05-16 22:19:54 +02:00
}
public function __get($offset)
{
2017-05-17 23:51:13 +02:00
return $this->{$offset};
2017-05-16 22:19:54 +02:00
}
public function __isset($offset)
{
2017-05-17 23:51:13 +02:00
return isset($this->{$offset});
2017-05-16 22:19:54 +02:00
}
public function __unset($offset)
{
if (!$this instanceof Volatile) {
2017-05-17 23:51:13 +02:00
if (isset($this->{$offset}) && $this->{$offset} instanceof self) {
2017-05-16 22:19:54 +02:00
throw new \RuntimeException();
}
}
2017-05-17 23:51:13 +02:00
unset($this->{$offset});
2017-05-16 22:19:54 +02:00
}
public function shift()
{
}
public function chunk($size)
{
$chunk = [];
while (count($chunk) < $size) {
$chunk[] = $this->shift();
}
return $chunk;
}
public function pop()
{
}
public function merge($merge)
{
foreach ($merge as $k => $v) {
2017-05-17 23:51:13 +02:00
$this->{$k} = $v;
2017-05-16 22:19:54 +02:00
}
}
public function wait($timeout = 0)
{
return true;
}
public function notify()
{
return true;
}
public function synchronized(Closure $closure, ...$args)
{
return $closure(...$args);
}
public function isRunning()
{
return $this->state & THREAD::RUNNING;
}
public function isTerminated()
{
return $this->state & THREAD::ERROR;
}
public static function extend($class)
{
return true;
}
public function addRef()
{
}
public function delRef()
{
}
public function getRefCount()
{
}
public function lock()
{
return true;
}
public function unlock()
{
return true;
}
public function isWaiting()
{
return false;
}
public function run()
{
}
public function isGarbage()
{
return true;
}
2017-05-16 22:33:18 +02:00
public function convertToVolatile($value)
2017-05-16 22:19:54 +02:00
{
/*
if (is_array($value)) {
foreach ($value as $k => $v) {
if (is_array($v)) {
$value[$k] =
new Volatile();
$value[$k]->merge(
$this->convertToVolatile($v));
}
}
}
2017-05-16 22:19:42 +02:00
*/
2017-05-16 22:19:54 +02:00
return $value;
}
}
2017-05-16 22:19:42 +02:00
}