fileName = tempnam(sys_get_temp_dir(), 'mutex-') . '.lock'; } /** * {@inheritdoc} */ public function acquire(): \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 from Coroutine\sleep(self::LATENCY_TIMEOUT); } // Return a lock object that can be used to release the lock on the mutex. $lock = new Lock(function (Lock $lock) { $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.'); } } }