Last active
August 30, 2015 09:49
-
-
Save ConConovaloff/6a4df0abe1f40a8461ca to your computer and use it in GitHub Desktop.
php Lock on flock with blocked acquire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Lock | |
{ | |
/** | |
* @var string | |
*/ | |
private $name; | |
/** | |
* @var string | |
*/ | |
private $directory; | |
private $fp; | |
private $timeout_check; | |
private $time_expired; | |
/** | |
* @param string $name | |
* @param string $directory | |
* @param int|float $timeout_check | |
*/ | |
public function __construct($name, $directory = '/tmp', $timeout_check = 0.5) | |
{ | |
$this->name = $name; | |
$this->directory = rtrim($directory, '/'); | |
$this->timeout_check = $timeout_check; | |
} | |
/** | |
* @param bool $blocked | |
* @param int|float|null $timeout seconds | |
* @return bool | |
*/ | |
public function acquire($blocked = true, $timeout = null) { | |
$this->fp = fopen(sprintf("%s/%s.lock", $this->directory, $this->name), "w+"); | |
$is_locked = flock($this->fp, LOCK_EX | LOCK_NB); | |
if (!$blocked) { | |
return $is_locked; | |
} | |
$this->setTimeout($timeout); | |
while (!$is_locked and !$this->isTimeExpired()) { | |
sleep($this->timeout_check); | |
$is_locked = $this->acquire(false); | |
} | |
return $is_locked; | |
} | |
public function release() { | |
flock($this->fp, LOCK_UN); | |
fclose($this->fp); | |
} | |
private function setTimeout($timeout) { | |
$this->time_expired = $timeout ? time() + $timeout : $timeout; | |
} | |
private function isTimeExpired() { | |
return $this->time_expired ? $this->time_expired < time() : False; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use:
With timeout:
without blocking