friendica/src/Core/Lock/SemaphoreLock.php

125 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace Friendica\Core\Lock;
use Friendica\Core\Cache;
class SemaphoreLock extends Lock
{
private static $semaphore = [];
public function __construct()
{
if (!function_exists('sem_get')) {
throw new \Exception('Semaphore lock not supported');
}
}
/**
2018-07-05 07:59:56 +02:00
* (@inheritdoc)
*/
private static function semaphoreKey($key)
{
$success = true;
$temp = get_temppath();
$file = $temp . '/' . $key . '.sem';
if (!file_exists($file)) {
$success = !empty(file_put_contents($file, $key));
}
2019-08-13 21:20:41 +02:00
return $success ? ftok($file, 'f') : false;
2019-08-13 21:20:41 +02:00
}
/**
2018-07-05 07:59:56 +02:00
* (@inheritdoc)
*/
2019-08-13 21:20:41 +02:00
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
{
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
if (self::$semaphore[$key]) {
if (sem_acquire(self::$semaphore[$key], ($timeout == 0))) {
$this->markAcquire($key);
return true;
}
}
return false;
}
/**
2018-07-05 07:59:56 +02:00
* (@inheritdoc)
*
* @param bool $override not necessary parameter for semaphore locks since the lock lives as long as the execution
* of the using function
*/
public function releaseLock($key, $override = false)
{
2019-08-13 21:20:41 +02:00
$success = false;
if (!empty(self::$semaphore[$key])) {
try {
$success = @sem_release(self::$semaphore[$key]);
2019-08-13 21:20:41 +02:00
unset(self::$semaphore[$key]);
$this->markRelease($key);
} catch (\Exception $exception) {
$success = false;
}
}
2019-08-13 21:20:41 +02:00
return $success;
}
/**
2018-07-05 07:59:56 +02:00
* (@inheritdoc)
*/
public function isLocked($key)
{
return isset(self::$semaphore[$key]);
}
2019-08-13 21:20:41 +02:00
/**
* {@inheritDoc}
*/
public function getName()
{
return self::TYPE_SEMAPHORE;
}
/**
* {@inheritDoc}
*/
public function getLocks(string $prefix = '')
{
// We can just return our own semaphore keys, since we don't know
// the state of other semaphores, even if the .sem files exists
$keys = array_keys(self::$semaphore);
if (empty($prefix)) {
return $keys;
} else {
$result = [];
foreach ($keys as $key) {
if (strpos($key, $prefix) === 0) {
array_push($result, $key);
}
2019-08-13 21:20:41 +02:00
}
return $result;
}
2019-08-13 21:20:41 +02:00
}
/**
* {@inheritDoc}
*/
public function releaseAll($override = false)
{
// Semaphores are just alive during a run, so there is no need to release
// You can just release your own locks
return parent::releaseAll($override);
2019-08-13 21:20:41 +02:00
}
}