/** * Acqiure mutex with given name. * If $timeout is less than zero (which is default) method waits for * mutex release forever. * If $timeout is greater than zero, method will wait given amount * of microseconds for mutex release. * If $timeout is zero, method will return false immediatly if mutex * is not free. * * @param string $name * @param int $timeout * @return bool */ public function acquire($name, $timeout = -1) { if (isset($this->acqiured[$name])) { return true; } if ($timeout > 0) { $endTime = microtime(true) + $timeout / 1000000; } $lockName = $this->getLockName($name); $info = $this->getLockInfo(); while ($timeout <= 0 || microtime(true) < $endTime) { if ($this->cache->add($lockName, $info, $this->expireTime)) { $this->acqiured[$name] = true; return true; } if ($timeout === 0) { return false; } usleep($this->sleepTime); } return false; }