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.
Inheritance: implements Traversable, implements Collectable
Ejemplo n.º 1
0
 /**
  * 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;
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
 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);
         }
     }
 }
Ejemplo n.º 4
0
 public function testThreadedSynchronized()
 {
     $threaded = new Threaded();
     $threaded->synchronized(function (...$args) {
         $this->assertEquals($args, [1, 2, 3, 4, 5]);
     }, 1, 2, 3, 4, 5);
 }
Ejemplo n.º 5
0
function _sleep($microseconds)
{
    $monitor = new Threaded();
    $monitor->synchronized(function () use($microseconds, $monitor) {
        $monitor->wait($microseconds);
    });
}
Ejemplo n.º 6
0
 /**
  * @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);
 }
Ejemplo n.º 7
0
 /**
  * @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));
     }
 }
Ejemplo n.º 8
0
 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);
     }
 }
Ejemplo n.º 9
0
 public function readThreadToMainPacket()
 {
     return $this->externalQueue->shift();
 }
Ejemplo n.º 10
0
 public static function microSleep(int $microseconds)
 {
     Server::$sleeper->synchronized(function (int $ms) {
         Server::$sleeper->wait($ms);
     }, $microseconds);
 }