Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/private/Lock/MemcacheLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ public function acquireLock($path, $type) {
*/
public function releaseLock($path, $type) {
if ($type === self::LOCK_SHARED) {
$newValue = 0;
if ($this->getOwnSharedLockCount($path) === 1) {
$removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
if (!$removed) { //someone else also has a shared lock, decrease only
$this->memcache->dec($path);
$newValue = $this->memcache->dec($path);
}
} else {
// if we own more than one lock ourselves just decrease
$this->memcache->dec($path);
$newValue = $this->memcache->dec($path);
}

// if we somehow release more locks then exists, reset the lock
if ($newValue < 0) {
$this->memcache->cad($path, $newValue);
}
} else if ($type === self::LOCK_EXCLUSIVE) {
$this->memcache->cad($path, 'exclusive');
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/Lock/LockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,16 @@ public function testChangeLockToSharedFromShared() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->changeLock('foo', ILockingProvider::LOCK_SHARED);
}

public function testReleaseNonExistingShared() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);

// releasing a lock once to many should not result in a locked state
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);

$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
$this->instance->releaseLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
}
}