synchronized() public method

Executes the block while retaining the synchronization lock for the current context.
public synchronized ( Closure $function, mixed $args = null ) : mixed
$function Closure The block of code to execute
$args mixed ... Variable length list of arguments to use as function arguments to the block
return mixed The return value from the block
示例#1
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);
         }
     }
 }
 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);
 }
示例#3
0
 public function testThreadedSynchronized()
 {
     $threaded = new Threaded();
     $threaded->synchronized(function (...$args) {
         $this->assertEquals($args, [1, 2, 3, 4, 5]);
     }, 1, 2, 3, 4, 5);
 }
示例#4
0
function _sleep($microseconds)
{
    $monitor = new Threaded();
    $monitor->synchronized(function () use($microseconds, $monitor) {
        $monitor->wait($microseconds);
    });
}
示例#5
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);
     }
 }
示例#6
0
 public static function microSleep(int $microseconds)
 {
     Server::$sleeper->synchronized(function (int $ms) {
         Server::$sleeper->wait($ms);
     }, $microseconds);
 }