Threaded objects form the basis of pthreads ability to execute user code asynchronously;
they expose and include synchronization methods and various useful interfaces.
Threaded objects, most importantly, provide implicit safety for the programmer;
all operations on the object scope are safe.
/** * Reads a line from console, if available. Returns null if not available * * @return string|null */ public function getLine() { if ($this->buffer->count() !== 0) { return $this->buffer->shift(); } return null; }
public function testThreadedSynchronized() { $threaded = new Threaded(); $threaded->synchronized(function ($self, ...$args) { $self->assertEquals([1, 2, 3, 4, 5], $args); }, $this, 1, 2, 3, 4, 5); }
public function run() { $this->buffer = new \Threaded(); $opts = getopt("", ["disable-readline"]); if (extension_loaded("readline") and $this->stream === "php://stdin" and !isset($opts["disable-readline"])) { $this->readline = true; } else { $this->readline = false; $this->fp = fopen($this->stream, "r"); stream_set_blocking($this->fp, 1); //Non-blocking STDIN won't work on Windows } $lastLine = microtime(true); while (true) { if (($line = $this->readLine()) !== "") { $this->buffer->synchronized(function (\Threaded $buffer, $line) { $buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line); }, $this->buffer, $line); $lastLine = microtime(true); } elseif (microtime(true) - $lastLine <= 0.1) { //Non blocking! Sleep to save CPU usleep(40000); } } }
public function testThreadedSynchronized() { $threaded = new Threaded(); $threaded->synchronized(function (...$args) { $this->assertEquals($args, [1, 2, 3, 4, 5]); }, 1, 2, 3, 4, 5); }
function _sleep($microseconds) { $monitor = new Threaded(); $monitor->synchronized(function () use($microseconds, $monitor) { $monitor->wait($microseconds); }); }
/** * @param Work $threaded Submit the work to the pool and will be dispatched among Pool's Workers * @return int|void */ public function submit(\Threaded $threaded) { if (!$threaded instanceof VolatileAware) { throw new \InvalidArgumentException("Threadeds submitted to OnDemandePool should implement both PipeAware and VolatileAware"); } ++$this->submitted; $threaded->setVolatile($this->getResultBag()); parent::submit($threaded); }
/** * @internal Only call from AsyncPool.php on the main thread * * @param Server $server */ public function checkProgressUpdates(Server $server) { while ($this->progressUpdates->count() !== 0) { $progress = $this->progressUpdates->shift(); $this->onProgressUpdate($server, unserialize($progress)); } }
public function run() { $opts = getopt("", ["disable-readline"]); if (extension_loaded("readline") and !isset($opts["disable-readline"])) { $this->readline = true; } else { $this->readline = false; } $lastLine = microtime(true); while (true) { if (($line = $this->readLine()) !== "") { $this->buffer->synchronized(function (\Threaded $buffer, $line) { $buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line); }, $this->buffer, $line); } elseif (microtime(true) - $lastLine <= 0.1) { //Non blocking! Sleep to save CPU usleep(40000); } $lastLine = microtime(true); } }
public function readThreadToMainPacket() { return $this->externalQueue->shift(); }
public static function microSleep(int $microseconds) { Server::$sleeper->synchronized(function (int $ms) { Server::$sleeper->wait($ms); }, $microseconds); }