Skip to content

Commit 8c0f5d1

Browse files
committed
Merge pull request #27 from JanVoracek/directory-lock
Added lock implementor that uses mkdir
2 parents 102d2ec + 7bc8d88 commit 8c0f5d1

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/NinjaMutex/Lock/DirectoryLock.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* This file is part of ninja-mutex.
4+
*
5+
* (C) Kamil Dziedzic <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace NinjaMutex\Lock;
11+
12+
/**
13+
* Lock implementor using mkdir
14+
*
15+
* @author Jan Voracek <[email protected]>
16+
*/
17+
class DirectoryLock extends LockAbstract
18+
{
19+
protected $dirname;
20+
21+
/**
22+
* @param string $dirname
23+
*/
24+
public function __construct($dirname)
25+
{
26+
parent::__construct();
27+
28+
$this->dirname = $dirname;
29+
}
30+
31+
/**
32+
* @param string $name
33+
* @param bool $blocking
34+
* @return bool
35+
*/
36+
protected function getLock($name, $blocking)
37+
{
38+
while (!@mkdir($this->getDirectoryPath($name))) {
39+
if (!$blocking) {
40+
return false;
41+
}
42+
43+
usleep(rand(5000, 20000));
44+
}
45+
46+
return true;
47+
}
48+
49+
/**
50+
* Release lock
51+
*
52+
* @param string $name name of lock
53+
* @return bool
54+
*/
55+
public function releaseLock($name)
56+
{
57+
if (isset($this->locks[$name])) {
58+
rmdir($this->getDirectoryPath($name));
59+
unset($this->locks[$name]);
60+
61+
return true;
62+
}
63+
64+
return false;
65+
}
66+
67+
/**
68+
* @param string $name
69+
* @return string
70+
*/
71+
protected function getDirectoryPath($name)
72+
{
73+
return $this->dirname . DIRECTORY_SEPARATOR . $name . '.lock';
74+
}
75+
76+
/**
77+
* Check if lock is locked
78+
*
79+
* @param string $name name of lock
80+
* @return bool
81+
*/
82+
public function isLocked($name)
83+
{
84+
if ($this->acquireLock($name, false)) {
85+
return !$this->releaseLock($name);
86+
}
87+
88+
return true;
89+
}
90+
}

tests/NinjaMutex/AbstractTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace NinjaMutex;
1111

12+
use NinjaMutex\Lock\DirectoryLock;
1213
use NinjaMutex\Lock\FlockLock;
1314
use NinjaMutex\Lock\MemcacheLock;
1415
use NinjaMutex\Lock\MemcachedLock;
@@ -62,13 +63,15 @@ public function lockImplementorProvider()
6263
$data = array(
6364
// Just mocks
6465
$this->provideFlockMockLock(),
66+
$this->provideDirectoryMockLock(),
6567
$this->provideMemcacheMockLock(),
6668
$this->provideMemcachedMockLock(),
6769
$this->provideMysqlMockLock(),
6870
$this->providePredisRedisMockLock(),
6971
$this->providePhpRedisMockLock(),
7072
// Real locks
7173
$this->provideFlockLock(),
74+
$this->provideDirectoryLock(),
7275
array($memcacheLockFabric->create()),
7376
array($memcachedLockFabric->create()),
7477
$this->provideMysqlLock(),
@@ -139,6 +142,14 @@ protected function provideFlockMockLock()
139142
return array(new FlockLock(vfs\vfsStream::url('nfs/')));
140143
}
141144

145+
/**
146+
* @return array
147+
*/
148+
protected function provideDirectoryMockLock()
149+
{
150+
return array(new DirectoryLock(vfs\vfsStream::url('nfs/')));
151+
}
152+
142153
/**
143154
* @return array
144155
*/
@@ -175,6 +186,14 @@ protected function provideFlockLock()
175186
return array(new FlockLock('/tmp/mutex/'));
176187
}
177188

189+
/**
190+
* @return array
191+
*/
192+
protected function provideDirectoryLock()
193+
{
194+
return array(new DirectoryLock('/tmp/mutex/'));
195+
}
196+
178197
/**
179198
* @return array
180199
*/

0 commit comments

Comments
 (0)