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 ($self, ...$args) { $self->assertEquals([1, 2, 3, 4, 5], $args); }, $this, 1, 2, 3, 4, 5); }
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); }); }
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 static function microSleep(int $microseconds) { Server::$sleeper->synchronized(function (int $ms) { Server::$sleeper->wait($ms); }, $microseconds); }