‪TYPO3CMS  9.5
SemaphoreLockStrategy.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 
27 {
29 
30  const ‪FILE_LOCK_FOLDER = 'lock/';
31 
35  protected ‪$id;
36 
40  protected ‪$resource;
41 
45  protected ‪$filePath = '';
46 
50  protected ‪$isAcquired = false;
51 
56  public function ‪__construct($subject)
57  {
59  if (!is_dir($path)) {
60  // Not using mkdir_deep on purpose here, if typo3temp/var itself
61  // does not exist, this issue should be solved on a different
62  // level of the application.
63  if (!GeneralUtility::mkdir($path)) {
64  throw new ‪LockCreateException('Cannot create directory ' . $path, 1460976250);
65  }
66  }
67  if (!is_writable($path)) {
68  throw new ‪LockCreateException('Cannot write to directory ' . $path, 1460976320);
69  }
70  $this->filePath = $path . 'sem_' . md5((string)$subject);
71  touch($this->filePath);
72  $this->id = ftok($this->filePath, 'A');
73  if ($this->id === false) {
74  throw new ‪LockCreateException('Cannot create key for semaphore using path ' . $this->filePath, 1396278734);
75  }
76  }
77 
81  public function ‪__destruct()
82  {
83  $this->‪release();
84  // We do not call sem_remove() since this would remove the resource for other processes,
85  // we leave that to the system. This is not clean, but there's no other way to determine when
86  // a semaphore is no longer needed as a website is generally running endlessly
87  // and we have no way to detect if there is a process currently waiting on that lock
88  // or if the server is shutdown
89  }
90 
96  public function ‪release()
97  {
98  if (!$this->‪isAcquired) {
99  return true;
100  }
101  $this->‪isAcquired = false;
102  return (bool)@sem_release($this->resource);
103  }
104 
110  public function ‪isAcquired()
111  {
112  return ‪$this->isAcquired;
113  }
114 
118  public static function ‪getCapabilities()
119  {
120  if (function_exists('sem_get')) {
122  }
123  return 0;
124  }
125 
133  public function ‪acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
134  {
135  if ($this->‪isAcquired) {
136  return true;
137  }
138 
139  $this->resource = sem_get($this->id, 1);
140  if ($this->resource === false) {
141  throw new LockAcquireException('Unable to get semaphore with id ' . $this->id, 1313828196);
142  }
143 
144  $this->‪isAcquired = (bool)sem_acquire($this->resource);
145  return ‪$this->isAcquired;
146  }
147 
151  public static function ‪getPriority()
152  {
153  return 25;
154  }
155 
159  public function ‪destroy()
160  {
161  if ($this->resource) {
162  sem_remove($this->resource);
163  @unlink($this->filePath);
164  }
165  }
166 }
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$resource
‪resource $resource
Definition: SemaphoreLockStrategy.php:38
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\getPriority
‪static int getPriority()
Definition: SemaphoreLockStrategy.php:147
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$isAcquired
‪bool $isAcquired
Definition: SemaphoreLockStrategy.php:46
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\__construct
‪__construct($subject)
Definition: SemaphoreLockStrategy.php:52
‪TYPO3\CMS\Core\Locking
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:25
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_EXCLUSIVE
‪const LOCK_CAPABILITY_EXCLUSIVE
Definition: LockingStrategyInterface.php:29
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy
Definition: SemaphoreLockStrategy.php:27
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$id
‪mixed $id
Definition: SemaphoreLockStrategy.php:34
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\getCapabilities
‪static int getCapabilities()
Definition: SemaphoreLockStrategy.php:114
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\destroy
‪destroy()
Definition: SemaphoreLockStrategy.php:155
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$filePath
‪string $filePath
Definition: SemaphoreLockStrategy.php:42
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\__destruct
‪__destruct()
Definition: SemaphoreLockStrategy.php:77
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\release
‪bool release()
Definition: SemaphoreLockStrategy.php:92
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireException
Definition: LockAcquireException.php:21
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:21
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\isAcquired
‪bool isAcquired()
Definition: SemaphoreLockStrategy.php:106
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\acquire
‪bool acquire($mode=self::LOCK_CAPABILITY_EXCLUSIVE)
Definition: SemaphoreLockStrategy.php:129
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: SemaphoreLockStrategy.php:30
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165