You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.0 KiB

3 years ago
  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 left over locks at the end of the request
  30. */
  31. abstract class AbstractLockingProvider implements ILockingProvider {
  32. /** @var int $ttl */
  33. protected $ttl; // how long until we clear stray locks in seconds
  34. protected $acquiredLocks = [
  35. 'shared' => [],
  36. 'exclusive' => []
  37. ];
  38. /**
  39. * Check if we've locally acquired a lock
  40. *
  41. * @param string $path
  42. * @param int $type
  43. * @return bool
  44. */
  45. protected function hasAcquiredLock(string $path, int $type): bool {
  46. if ($type === self::LOCK_SHARED) {
  47. return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0;
  48. } else {
  49. return isset($this->acquiredLocks['exclusive'][$path]) && $this->acquiredLocks['exclusive'][$path] === true;
  50. }
  51. }
  52. /**
  53. * Mark a locally acquired lock
  54. *
  55. * @param string $path
  56. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  57. */
  58. protected function markAcquire(string $path, int $type) {
  59. if ($type === self::LOCK_SHARED) {
  60. if (!isset($this->acquiredLocks['shared'][$path])) {
  61. $this->acquiredLocks['shared'][$path] = 0;
  62. }
  63. $this->acquiredLocks['shared'][$path]++;
  64. } else {
  65. $this->acquiredLocks['exclusive'][$path] = true;
  66. }
  67. }
  68. /**
  69. * Mark a release of a locally acquired lock
  70. *
  71. * @param string $path
  72. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  73. */
  74. protected function markRelease(string $path, int $type) {
  75. if ($type === self::LOCK_SHARED) {
  76. if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
  77. $this->acquiredLocks['shared'][$path]--;
  78. if ($this->acquiredLocks['shared'][$path] === 0) {
  79. unset($this->acquiredLocks['shared'][$path]);
  80. }
  81. }
  82. } elseif ($type === self::LOCK_EXCLUSIVE) {
  83. unset($this->acquiredLocks['exclusive'][$path]);
  84. }
  85. }
  86. /**
  87. * Change the type of an existing tracked lock
  88. *
  89. * @param string $path
  90. * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  91. */
  92. protected function markChange(string $path, int $targetType) {
  93. if ($targetType === self::LOCK_SHARED) {
  94. unset($this->acquiredLocks['exclusive'][$path]);
  95. if (!isset($this->acquiredLocks['shared'][$path])) {
  96. $this->acquiredLocks['shared'][$path] = 0;
  97. }
  98. $this->acquiredLocks['shared'][$path]++;
  99. } elseif ($targetType === self::LOCK_EXCLUSIVE) {
  100. $this->acquiredLocks['exclusive'][$path] = true;
  101. $this->acquiredLocks['shared'][$path]--;
  102. }
  103. }
  104. /**
  105. * release all lock acquired by this instance which were marked using the mark* methods
  106. */
  107. public function releaseAll() {
  108. foreach ($this->acquiredLocks['shared'] as $path => $count) {
  109. for ($i = 0; $i < $count; $i++) {
  110. $this->releaseLock($path, self::LOCK_SHARED);
  111. }
  112. }
  113. foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
  114. $this->releaseLock($path, self::LOCK_EXCLUSIVE);
  115. }
  116. }
  117. protected function getOwnSharedLockCount(string $path) {
  118. return isset($this->acquiredLocks['shared'][$path]) ? $this->acquiredLocks['shared'][$path] : 0;
  119. }
  120. }