memcachelockingprovider.php 4.1 KB

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