-
Notifications
You must be signed in to change notification settings - Fork 407
/
lock.php
46 lines (39 loc) · 847 Bytes
/
lock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
require_once( 'util.php' );
class rLock
{
protected $file;
protected $lockTime;
protected $locked = false;
const MAX_LOCK_TIME = 1800;
protected function __construct( $name, $maxLockTime )
{
$this->lockTime = $maxLockTime;
$this->file = FileUtil::getSettingsPath().'/'.$name.".lock";
}
function __destruct()
{
$this->release();
}
public function lock()
{
clearstatcache();
$this->locked = (!file_exists($this->file) || (time()-filemtime($this->file)>$this->lockTime)) && touch($this->file);
return($this->locked);
}
public function release()
{
if($this->locked)
{
$this->locked = false;
unlink($this->file);
}
}
static public function obtain( $name = '', $maxLockTime = self::MAX_LOCK_TIME )
{
$lck = new rLock($name,$maxLockTime);
if(!$lck->lock())
$lck = false;
return($lck);
}
}