1
0

AbstractLockingProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Lock;
  26. use OCP\Lock\ILockingProvider;
  27. /**
  28. * Base locking provider that keeps track of locks acquired during the current request
  29. * to release any leftover locks at the end of the request
  30. */
  31. abstract class AbstractLockingProvider implements ILockingProvider {
  32. protected array $acquiredLocks = [
  33. 'shared' => [],
  34. 'exclusive' => []
  35. ];
  36. /**
  37. *
  38. * @param int $ttl how long until we clear stray locks in seconds
  39. */
  40. public function __construct(protected int $ttl) {
  41. }
  42. /** @inheritDoc */
  43. protected function hasAcquiredLock(string $path, int $type): bool {
  44. if ($type === self::LOCK_SHARED) {
  45. return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0;
  46. } else {
  47. return isset($this->acquiredLocks['exclusive'][$path]) && $this->acquiredLocks['exclusive'][$path] === true;
  48. }
  49. }
  50. /** @inheritDoc */
  51. protected function markAcquire(string $path, int $targetType): void {
  52. if ($targetType === self::LOCK_SHARED) {
  53. if (!isset($this->acquiredLocks['shared'][$path])) {
  54. $this->acquiredLocks['shared'][$path] = 0;
  55. }
  56. $this->acquiredLocks['shared'][$path]++;
  57. } else {
  58. $this->acquiredLocks['exclusive'][$path] = true;
  59. }
  60. }
  61. /** @inheritDoc */
  62. protected function markRelease(string $path, int $type): void {
  63. if ($type === self::LOCK_SHARED) {
  64. if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
  65. $this->acquiredLocks['shared'][$path]--;
  66. if ($this->acquiredLocks['shared'][$path] === 0) {
  67. unset($this->acquiredLocks['shared'][$path]);
  68. }
  69. }
  70. } elseif ($type === self::LOCK_EXCLUSIVE) {
  71. unset($this->acquiredLocks['exclusive'][$path]);
  72. }
  73. }
  74. /** @inheritDoc */
  75. protected function markChange(string $path, int $targetType): void {
  76. if ($targetType === self::LOCK_SHARED) {
  77. unset($this->acquiredLocks['exclusive'][$path]);
  78. if (!isset($this->acquiredLocks['shared'][$path])) {
  79. $this->acquiredLocks['shared'][$path] = 0;
  80. }
  81. $this->acquiredLocks['shared'][$path]++;
  82. } elseif ($targetType === self::LOCK_EXCLUSIVE) {
  83. $this->acquiredLocks['exclusive'][$path] = true;
  84. $this->acquiredLocks['shared'][$path]--;
  85. }
  86. }
  87. /** @inheritDoc */
  88. public function releaseAll(): void {
  89. foreach ($this->acquiredLocks['shared'] as $path => $count) {
  90. for ($i = 0; $i < $count; $i++) {
  91. $this->releaseLock($path, self::LOCK_SHARED);
  92. }
  93. }
  94. foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
  95. $this->releaseLock($path, self::LOCK_EXCLUSIVE);
  96. }
  97. }
  98. protected function getOwnSharedLockCount(string $path): int {
  99. return $this->acquiredLocks['shared'][$path] ?? 0;
  100. }
  101. }