AbstractLockingProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /** how long until we clear stray locks in seconds */
  33. protected int $ttl;
  34. protected $acquiredLocks = [
  35. 'shared' => [],
  36. 'exclusive' => []
  37. ];
  38. /** @inheritDoc */
  39. protected function hasAcquiredLock(string $path, int $type): bool {
  40. if ($type === self::LOCK_SHARED) {
  41. return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0;
  42. } else {
  43. return isset($this->acquiredLocks['exclusive'][$path]) && $this->acquiredLocks['exclusive'][$path] === true;
  44. }
  45. }
  46. /** @inheritDoc */
  47. protected function markAcquire(string $path, int $targetType): void {
  48. if ($targetType === self::LOCK_SHARED) {
  49. if (!isset($this->acquiredLocks['shared'][$path])) {
  50. $this->acquiredLocks['shared'][$path] = 0;
  51. }
  52. $this->acquiredLocks['shared'][$path]++;
  53. } else {
  54. $this->acquiredLocks['exclusive'][$path] = true;
  55. }
  56. }
  57. /** @inheritDoc */
  58. protected function markRelease(string $path, int $type): void {
  59. if ($type === self::LOCK_SHARED) {
  60. if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
  61. $this->acquiredLocks['shared'][$path]--;
  62. if ($this->acquiredLocks['shared'][$path] === 0) {
  63. unset($this->acquiredLocks['shared'][$path]);
  64. }
  65. }
  66. } elseif ($type === self::LOCK_EXCLUSIVE) {
  67. unset($this->acquiredLocks['exclusive'][$path]);
  68. }
  69. }
  70. /** @inheritDoc */
  71. protected function markChange(string $path, int $targetType): void {
  72. if ($targetType === self::LOCK_SHARED) {
  73. unset($this->acquiredLocks['exclusive'][$path]);
  74. if (!isset($this->acquiredLocks['shared'][$path])) {
  75. $this->acquiredLocks['shared'][$path] = 0;
  76. }
  77. $this->acquiredLocks['shared'][$path]++;
  78. } elseif ($targetType === self::LOCK_EXCLUSIVE) {
  79. $this->acquiredLocks['exclusive'][$path] = true;
  80. $this->acquiredLocks['shared'][$path]--;
  81. }
  82. }
  83. /** @inheritDoc */
  84. public function releaseAll(): void {
  85. foreach ($this->acquiredLocks['shared'] as $path => $count) {
  86. for ($i = 0; $i < $count; $i++) {
  87. $this->releaseLock($path, self::LOCK_SHARED);
  88. }
  89. }
  90. foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
  91. $this->releaseLock($path, self::LOCK_EXCLUSIVE);
  92. }
  93. }
  94. protected function getOwnSharedLockCount(string $path): int {
  95. return $this->acquiredLocks['shared'][$path] ?? 0;
  96. }
  97. }