2 namespace TYPO3\CMS\Core\Locking;
100 $this->loops = (int)
$loops;
103 $this->step = (int)
$step;
105 if (
$method ===
'' && isset(
$GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'lockingMode'])) {
110 case self::LOCKING_METHOD_SIMPLE:
112 case self::LOCKING_METHOD_FLOCK:
113 $this->
id = md5(
$id);
116 case self::LOCKING_METHOD_SEMAPHORE:
117 $this->
id = abs(crc32(
$id));
119 case self::LOCKING_METHOD_DISABLED:
122 throw new \InvalidArgumentException(
'No such locking method "' .
$method .
'"', 1294586097);
134 switch ($this->method) {
135 case self::LOCKING_METHOD_FLOCK:
140 @unlink($this->resource);
143 case self::LOCKING_METHOD_SEMAPHORE:
144 @sem_remove($this->resource);
159 $this->resource = sem_get($this->
id, 1);
160 if ($this->resource ===
false) {
161 throw new \RuntimeException(
'Unable to get semaphore with id ' . $this->
id, 1313828196);
184 switch ($this->method) {
185 case self::LOCKING_METHOD_SIMPLE:
186 if (file_exists($this->resource)) {
187 $this->
sysLog(
'Waiting for a different process to release the lock');
188 $maxExecutionTime = (int)ini_get(
'max_execution_time');
189 $maxAge = time() - ($maxExecutionTime ?: 120);
190 if (@filectime($this->resource) < $maxAge) {
191 @unlink($this->resource);
200 $this->
sysLog(
'Lock acquired');
205 usleep($this->step * 1000);
208 throw new \RuntimeException(
'Lock file could not be created', 1294586098);
211 case self::LOCKING_METHOD_FLOCK:
212 $this->filePointer = fopen($this->resource,
'c');
213 if ($this->filePointer ===
false) {
214 throw new \RuntimeException(
'Lock file could not be opened', 1294586099);
217 if (flock($this->filePointer, LOCK_EX | LOCK_NB)) {
219 }
elseif (flock($this->filePointer, LOCK_EX)) {
223 throw new \RuntimeException(
'Could not lock file "' . $this->resource .
'"', 1294586100);
227 case self::LOCKING_METHOD_SEMAPHORE:
230 if (@sem_acquire($this->resource)) {
238 case self::LOCKING_METHOD_DISABLED:
255 if ($this->isAcquired) {
258 $this->isAcquired =
false;
259 switch ($this->method) {
260 case self::LOCKING_METHOD_SIMPLE:
261 if (file_exists($this->resource)) {
262 $this->
sysLog(
'Waiting for a different process to release the lock');
263 $maxExecutionTime = (int)ini_get(
'max_execution_time');
264 $maxAge = time() - ($maxExecutionTime ?: 120);
265 if (@filectime($this->resource) < $maxAge) {
266 @unlink($this->resource);
275 $this->
sysLog(
'Lock acquired');
276 $this->isAcquired =
true;
279 usleep($this->step * 1000);
282 case self::LOCKING_METHOD_FLOCK:
283 $this->filePointer = fopen($this->resource,
'c');
284 if ($this->filePointer ===
false) {
285 throw new \RuntimeException(
'Lock file could not be opened', 1294586099);
287 if (flock($this->filePointer, LOCK_EX)) {
288 $this->isAcquired =
true;
291 case self::LOCKING_METHOD_SEMAPHORE:
293 if (@sem_acquire($this->resource)) {
294 $this->isAcquired =
true;
297 case self::LOCKING_METHOD_DISABLED:
315 if ($this->isAcquired) {
318 if ($this->method === self::LOCKING_METHOD_FLOCK) {
319 $this->filePointer = fopen($this->resource,
'c');
320 if ($this->filePointer ===
false) {
321 throw new \RuntimeException(
'Lock file could not be opened', 1294586099);
323 if (flock($this->filePointer, LOCK_SH)) {
324 $this->isAcquired =
true;
337 if (!$this->isAcquired) {
341 switch ($this->method) {
342 case self::LOCKING_METHOD_SIMPLE:
347 if (@unlink($this->resource) ===
false) {
352 case self::LOCKING_METHOD_FLOCK:
353 if (is_resource($this->filePointer)) {
354 if (flock($this->filePointer, LOCK_UN) ===
false) {
357 fclose($this->filePointer);
360 case self::LOCKING_METHOD_SEMAPHORE:
361 if (!@sem_release($this->resource)) {
365 case self::LOCKING_METHOD_DISABLED:
370 $this->isAcquired =
false;
423 switch ($this->method) {
424 case self::LOCKING_METHOD_SIMPLE:
425 if (file_exists($this->resource)) {
426 $maxExecutionTime = (int)ini_get(
'max_execution_time');
427 $maxAge = time() - ($maxExecutionTime ?: 120);
428 if (@filectime($this->resource) < $maxAge) {
429 @unlink($this->resource);
436 case self::LOCKING_METHOD_FLOCK:
439 case self::LOCKING_METHOD_SEMAPHORE:
442 case self::LOCKING_METHOD_DISABLED:
478 public function sysLog($message, $severity = 0)
480 if ($this->isLoggingEnabled) {
481 GeneralUtility::sysLog(
'Locking [' . $this->method .
'::' . $this->
id .
']: ' . trim($message), $this->syslogFacility, $severity);
495 $path = PATH_site . self::FILE_LOCK_FOLDER;
496 if (!is_dir($path)) {
501 throw new \RuntimeException(
'Cannot create directory ' . $path, 1395140007);
504 if (!is_writable($path)) {
505 throw new \RuntimeException(
'Cannot write to directory ' . $path, 1396278700);