2015-07-15 09:44:51 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-01-16 13:34:43 -05:00
|
|
|
declare(strict_types=1);
|
2015-07-15 09:44:51 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-07-15 09:44:51 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Lock;
|
|
|
|
|
|
|
|
|
|
use OCP\Lock\ILockingProvider;
|
|
|
|
|
|
2015-08-03 09:46:23 -04:00
|
|
|
/**
|
|
|
|
|
* Base locking provider that keeps track of locks acquired during the current request
|
2022-04-22 09:00:20 -04:00
|
|
|
* to release any leftover locks at the end of the request
|
2015-08-03 09:46:23 -04:00
|
|
|
*/
|
2015-07-15 09:44:51 -04:00
|
|
|
abstract class AbstractLockingProvider implements ILockingProvider {
|
2023-09-26 03:18:17 -04:00
|
|
|
protected array $acquiredLocks = [
|
2015-07-15 09:44:51 -04:00
|
|
|
'shared' => [],
|
|
|
|
|
'exclusive' => []
|
|
|
|
|
];
|
|
|
|
|
|
2023-09-26 03:18:17 -04:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param int $ttl how long until we clear stray locks in seconds
|
|
|
|
|
*/
|
2024-09-19 05:10:31 -04:00
|
|
|
public function __construct(
|
|
|
|
|
protected int $ttl,
|
|
|
|
|
) {
|
2023-09-26 03:18:17 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-22 09:00:20 -04:00
|
|
|
/** @inheritDoc */
|
2018-01-16 13:34:43 -05:00
|
|
|
protected function hasAcquiredLock(string $path, int $type): bool {
|
2015-10-20 08:04:50 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 09:00:20 -04:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function markAcquire(string $path, int $targetType): void {
|
|
|
|
|
if ($targetType === self::LOCK_SHARED) {
|
2015-07-15 09:44:51 -04:00
|
|
|
if (!isset($this->acquiredLocks['shared'][$path])) {
|
|
|
|
|
$this->acquiredLocks['shared'][$path] = 0;
|
|
|
|
|
}
|
|
|
|
|
$this->acquiredLocks['shared'][$path]++;
|
|
|
|
|
} else {
|
|
|
|
|
$this->acquiredLocks['exclusive'][$path] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 09:00:20 -04:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function markRelease(string $path, int $type): void {
|
2015-07-15 09:44:51 -04:00
|
|
|
if ($type === self::LOCK_SHARED) {
|
2025-09-27 16:56:38 -04:00
|
|
|
if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0) {
|
2015-07-15 09:44:51 -04:00
|
|
|
$this->acquiredLocks['shared'][$path]--;
|
2016-05-02 08:18:46 -04:00
|
|
|
if ($this->acquiredLocks['shared'][$path] === 0) {
|
|
|
|
|
unset($this->acquiredLocks['shared'][$path]);
|
|
|
|
|
}
|
2015-07-15 09:44:51 -04:00
|
|
|
}
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($type === self::LOCK_EXCLUSIVE) {
|
2015-07-15 09:44:51 -04:00
|
|
|
unset($this->acquiredLocks['exclusive'][$path]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 09:00:20 -04:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function markChange(string $path, int $targetType): void {
|
2015-07-15 09:44:51 -04:00
|
|
|
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]++;
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($targetType === self::LOCK_EXCLUSIVE) {
|
2015-07-15 09:44:51 -04:00
|
|
|
$this->acquiredLocks['exclusive'][$path] = true;
|
|
|
|
|
$this->acquiredLocks['shared'][$path]--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 09:00:20 -04:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function releaseAll(): void {
|
2015-07-15 09:44:51 -04:00
|
|
|
foreach ($this->acquiredLocks['shared'] as $path => $count) {
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
|
$this->releaseLock($path, self::LOCK_SHARED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
|
|
|
|
|
$this->releaseLock($path, self::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-29 06:54:33 -04:00
|
|
|
|
2022-04-22 09:00:20 -04:00
|
|
|
protected function getOwnSharedLockCount(string $path): int {
|
|
|
|
|
return $this->acquiredLocks['shared'][$path] ?? 0;
|
2016-04-29 06:54:33 -04:00
|
|
|
}
|
2015-07-15 09:44:51 -04:00
|
|
|
}
|