fileName = \tempnam(\sys_get_temp_dir(), 'mutex-') . '.lock'; } /** * {@inheritdoc} */ public function acquire(): Promise { return new Coroutine($this->doAcquire()); } /** * @coroutine * * @return \Generator */ private function doAcquire(): \Generator { // Try to create the lock file. If the file already exists, someone else // has the lock, so set an asynchronous timer and try again. while (($handle = @\fopen($this->fileName, 'x')) === false) { yield new Pause(self::LATENCY_TIMEOUT); } // Return a lock object that can be used to release the lock on the mutex. $lock = new Lock(function () { $this->release(); }); \fclose($handle); return $lock; } /** * Releases the lock on the mutex. * * @throws MutexException If the unlock operation failed. */ protected function release() { $success = @\unlink($this->fileName); if (!$success) { throw new MutexException('Failed to unlock the mutex file.'); } } }