Skip to content
Open
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
75 changes: 45 additions & 30 deletions lib/private/Lock/AbstractLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,85 @@
* to release any leftover locks at the end of the request
*/
abstract class AbstractLockingProvider implements ILockingProvider {
/**
* @var array{
* shared: array<string, int>,
* exclusive: array<string, true>,
* }
*/
protected array $acquiredLocks = [
'shared' => [],
'exclusive' => []
'exclusive' => [],
];

/**
*
* @param int $ttl how long until we clear stray locks in seconds
* @param int $ttl How long until stray locks are cleared, in seconds.
*/
public function __construct(
protected int $ttl,
) {
}

/** @inheritDoc */
protected function assertValidLockType(int $type): void {
if ($type !== self::LOCK_SHARED && $type !== self::LOCK_EXCLUSIVE) {
throw new \InvalidArgumentException('Invalid lock type: ' . $type);
}
}

protected function hasAcquiredLock(string $path, int $type): bool {
if ($type === self::LOCK_SHARED) {
return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0;
} else {
return isset($this->acquiredLocks['exclusive'][$path]) && $this->acquiredLocks['exclusive'][$path] === true;
return isset($this->acquiredLocks['shared'][$path])
&& $this->acquiredLocks['shared'][$path] > 0;
}

return $type === self::LOCK_EXCLUSIVE
&& isset($this->acquiredLocks['exclusive'][$path]);
}

/** @inheritDoc */
protected function markAcquire(string $path, int $targetType): void {
if ($targetType === self::LOCK_EXCLUSIVE) {
$this->acquiredLocks['exclusive'][$path] = true;
return;
}

if ($targetType === self::LOCK_SHARED) {
if (!isset($this->acquiredLocks['shared'][$path])) {
$this->acquiredLocks['shared'][$path] = 0;
}
$this->acquiredLocks['shared'][$path] ??= 0;
$this->acquiredLocks['shared'][$path]++;
} else {
$this->acquiredLocks['exclusive'][$path] = true;
}
}

/** @inheritDoc */
protected function markRelease(string $path, int $type): void {
if ($type === self::LOCK_EXCLUSIVE) {
unset($this->acquiredLocks['exclusive'][$path]);
return;
}

if ($type === self::LOCK_SHARED) {
if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0) {
$this->acquiredLocks['shared'][$path]--;
if ($this->acquiredLocks['shared'][$path] === 0) {
unset($this->acquiredLocks['shared'][$path]);
}
if (!isset($this->acquiredLocks['shared'][$path])) {
return;
}

$this->acquiredLocks['shared'][$path]--;
if ($this->acquiredLocks['shared'][$path] === 0) {
unset($this->acquiredLocks['shared'][$path]);
}
} elseif ($type === self::LOCK_EXCLUSIVE) {
unset($this->acquiredLocks['exclusive'][$path]);
}
}

/** @inheritDoc */
protected function markChange(string $path, int $targetType): void {
if ($targetType === self::LOCK_EXCLUSIVE) {
$this->acquiredLocks['exclusive'][$path] = true;
unset($this->acquiredLocks['shared'][$path]);
return;
}

if ($targetType === self::LOCK_SHARED) {
unset($this->acquiredLocks['exclusive'][$path]);
if (!isset($this->acquiredLocks['shared'][$path])) {
$this->acquiredLocks['shared'][$path] = 0;
}
$this->acquiredLocks['shared'][$path] ??= 0;
$this->acquiredLocks['shared'][$path]++;
} elseif ($targetType === self::LOCK_EXCLUSIVE) {
$this->acquiredLocks['exclusive'][$path] = true;
$this->acquiredLocks['shared'][$path]--;
}
}

/** @inheritDoc */
#[\Override]
public function releaseAll(): void {
foreach ($this->acquiredLocks['shared'] as $path => $count) {
Expand All @@ -88,7 +103,7 @@ public function releaseAll(): void {
}
}

foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
foreach ($this->acquiredLocks['exclusive'] as $path => $_) {
$this->releaseLock($path, self::LOCK_EXCLUSIVE);
}
}
Expand Down
Loading