memcachelockingprovider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Lock;
  22. use OCP\Lock\ILockingProvider;
  23. use OCP\Lock\LockedException;
  24. use OCP\IMemcache;
  25. class MemcacheLockingProvider implements ILockingProvider {
  26. /**
  27. * @var \OCP\IMemcache
  28. */
  29. private $memcache;
  30. private $acquiredLocks = [
  31. 'shared' => [],
  32. 'exclusive' => []
  33. ];
  34. /**
  35. * @param \OCP\IMemcache $memcache
  36. */
  37. public function __construct(IMemcache $memcache) {
  38. $this->memcache = $memcache;
  39. }
  40. /**
  41. * @param string $path
  42. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  43. * @return bool
  44. */
  45. public function isLocked($path, $type) {
  46. $lockValue = $this->memcache->get($path);
  47. if ($type === self::LOCK_SHARED) {
  48. return $lockValue > 0;
  49. } else if ($type === self::LOCK_EXCLUSIVE) {
  50. return $lockValue === 'exclusive';
  51. } else {
  52. return false;
  53. }
  54. }
  55. /**
  56. * @param string $path
  57. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  58. * @throws \OCP\Lock\LockedException
  59. */
  60. public function acquireLock($path, $type) {
  61. if ($type === self::LOCK_SHARED) {
  62. if (!$this->memcache->inc($path)) {
  63. throw new LockedException($path);
  64. }
  65. if (!isset($this->acquiredLocks['shared'][$path])) {
  66. $this->acquiredLocks['shared'][$path] = 0;
  67. }
  68. $this->acquiredLocks['shared'][$path]++;
  69. } else {
  70. $this->memcache->add($path, 0);
  71. if (!$this->memcache->cas($path, 0, 'exclusive')) {
  72. throw new LockedException($path);
  73. }
  74. $this->acquiredLocks['exclusive'][$path] = true;
  75. }
  76. }
  77. /**
  78. * @param string $path
  79. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  80. */
  81. public function releaseLock($path, $type) {
  82. if ($type === self::LOCK_SHARED) {
  83. if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
  84. $this->memcache->dec($path);
  85. $this->acquiredLocks['shared'][$path]--;
  86. }
  87. } else if ($type === self::LOCK_EXCLUSIVE) {
  88. $this->memcache->cas($path, 'exclusive', 0);
  89. unset($this->acquiredLocks['exclusive'][$path]);
  90. }
  91. }
  92. /**
  93. * Change the type of an existing lock
  94. *
  95. * @param string $path
  96. * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  97. * @throws \OCP\Lock\LockedException
  98. */
  99. public function changeLock($path, $targetType) {
  100. if ($targetType === self::LOCK_SHARED) {
  101. if (!$this->memcache->cas($path, 'exclusive', 1)) {
  102. throw new LockedException($path);
  103. }
  104. unset($this->acquiredLocks['exclusive'][$path]);
  105. if (!isset($this->acquiredLocks['shared'][$path])) {
  106. $this->acquiredLocks['shared'][$path] = 0;
  107. }
  108. $this->acquiredLocks['shared'][$path]++;
  109. } else if ($targetType === self::LOCK_EXCLUSIVE) {
  110. // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
  111. if (!$this->memcache->cas($path, 1, 'exclusive')) {
  112. throw new LockedException($path);
  113. }
  114. $this->acquiredLocks['exclusive'][$path] = true;
  115. $this->acquiredLocks['shared'][$path]--;
  116. }
  117. }
  118. /**
  119. * release all lock acquired by this instance
  120. */
  121. public function releaseAll() {
  122. foreach ($this->acquiredLocks['shared'] as $path => $count) {
  123. for ($i = 0; $i < $count; $i++) {
  124. $this->releaseLock($path, self::LOCK_SHARED);
  125. }
  126. }
  127. foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
  128. $this->releaseLock($path, self::LOCK_EXCLUSIVE);
  129. }
  130. }
  131. }