‪TYPO3CMS  10.4
ResourceMutex.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
27 {
31  private ‪$lockFactory;
32 
38  private ‪$accessLocks = [];
39 
45  private ‪$workerLocks = [];
46 
48  {
49  $this->lockFactory = ‪$lockFactory;
50  }
51 
61  public function ‪acquireLock(string $scope, string $key): void
62  {
63  $this->accessLocks[$scope] = $this->lockFactory->createLocker($scope);
64 
65  $this->workerLocks[$scope] = $this->lockFactory->createLocker(
66  $key,
68  );
69 
70  do {
71  if (!$this->accessLocks[$scope]->acquire()) {
72  throw new \RuntimeException('Could not acquire access lock for "' . $scope . '"".', 1601923209);
73  }
74 
75  try {
76  $locked = $this->workerLocks[$scope]->acquire(
78  );
80  // somebody else has the lock, we keep waiting
81 
82  // first release the access lock
83  $this->accessLocks[$scope]->release();
84  // now lets make a short break (100ms) until we try again, since
85  // the page generation by the lock owner will take a while anyways
86  usleep(100000);
87  continue;
88  }
89  $this->accessLocks[$scope]->release();
90  if ($locked) {
91  break;
92  }
93  throw new \RuntimeException('Could not acquire image process lock for ' . $key . '.', 1601923215);
94  } while (true);
95  }
96 
104  public function ‪releaseLock(string $scope): void
105  {
106  if ($this->accessLocks[$scope] ?? null) {
107  if (!$this->accessLocks[$scope]->acquire()) {
108  throw new \RuntimeException('Could not acquire access lock for "' . $scope . '"".', 1601923319);
109  }
110 
111  $this->workerLocks[$scope]->release();
112  $this->workerLocks[$scope]->destroy();
113  $this->workerLocks[$scope] = null;
114 
115  $this->accessLocks[$scope]->release();
116  $this->accessLocks[$scope] = null;
117  }
118  }
119 }
‪TYPO3\CMS\Core\Locking\ResourceMutex
Definition: ResourceMutex.php:27
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_NOBLOCK
‪const LOCK_CAPABILITY_NOBLOCK
Definition: LockingStrategyInterface.php:40
‪TYPO3\CMS\Core\Locking
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:26
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_EXCLUSIVE
‪const LOCK_CAPABILITY_EXCLUSIVE
Definition: LockingStrategyInterface.php:30
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
Definition: LockAcquireWouldBlockException.php:22
‪TYPO3\CMS\Core\Locking\ResourceMutex\$lockFactory
‪LockFactory $lockFactory
Definition: ResourceMutex.php:30
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireException
Definition: LockAcquireException.php:24
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:24
‪TYPO3\CMS\Core\Locking\ResourceMutex\releaseLock
‪releaseLock(string $scope)
Definition: ResourceMutex.php:101
‪TYPO3\CMS\Core\Locking\ResourceMutex\$accessLocks
‪LockingStrategyInterface[] $accessLocks
Definition: ResourceMutex.php:36
‪TYPO3\CMS\Core\Locking\LockFactory
Definition: LockFactory.php:25
‪TYPO3\CMS\Core\Locking\ResourceMutex\acquireLock
‪acquireLock(string $scope, string $key)
Definition: ResourceMutex.php:58
‪TYPO3\CMS\Core\Locking\ResourceMutex\__construct
‪__construct(LockFactory $lockFactory)
Definition: ResourceMutex.php:44
‪TYPO3\CMS\Core\Locking\ResourceMutex\$workerLocks
‪LockingStrategyInterface[] $workerLocks
Definition: ResourceMutex.php:42