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);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2015-07-15 09:44:51 -04:00
|
|
|
/**
|
2016-07-21 11:07:57 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2020-04-29 05:57:22 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 12:13:36 -04:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 13:57:53 -05:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-07-15 09:44:51 -04:00
|
|
|
*
|
|
|
|
|
* @license AGPL-3.0
|
|
|
|
|
*
|
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(protected int $ttl) {
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
|
|
|
|
|
$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
|
|
|
}
|