2018-06-28 20:57:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Lock;
|
|
|
|
|
2018-07-07 17:46:16 +00:00
|
|
|
use Friendica\Core\Cache;
|
|
|
|
|
2019-08-04 13:51:49 +00:00
|
|
|
class SemaphoreLock extends Lock
|
2018-06-28 20:57:17 +00:00
|
|
|
{
|
2018-07-04 21:37:22 +00:00
|
|
|
private static $semaphore = [];
|
|
|
|
|
2018-06-28 20:57:17 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (!function_exists('sem_get')) {
|
|
|
|
throw new \Exception('Semaphore lock not supported');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-05 05:59:56 +00:00
|
|
|
* (@inheritdoc)
|
2018-06-28 20:57:17 +00:00
|
|
|
*/
|
2018-06-28 21:06:14 +00:00
|
|
|
private static function semaphoreKey($key)
|
2018-06-28 20:57:17 +00:00
|
|
|
{
|
2019-08-13 19:20:41 +00:00
|
|
|
$file = self::keyToFile($key);
|
2018-06-28 20:57:17 +00:00
|
|
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
file_put_contents($file, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ftok($file, 'f');
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:20:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the full path to the semaphore file
|
|
|
|
*
|
|
|
|
* @param string $key The key of the semaphore
|
|
|
|
*
|
|
|
|
* @return string The full path
|
|
|
|
*/
|
|
|
|
private static function keyToFile($key)
|
|
|
|
{
|
|
|
|
$temp = get_temppath();
|
|
|
|
|
|
|
|
return $temp . '/' . $key . '.sem';
|
|
|
|
}
|
|
|
|
|
2018-06-28 20:57:17 +00:00
|
|
|
/**
|
2018-07-05 05:59:56 +00:00
|
|
|
* (@inheritdoc)
|
2018-06-28 20:57:17 +00:00
|
|
|
*/
|
2019-08-13 19:20:41 +00:00
|
|
|
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
|
2018-06-28 20:57:17 +00:00
|
|
|
{
|
2018-07-04 21:37:22 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-06-28 20:57:17 +00:00
|
|
|
}
|
2018-07-04 21:37:22 +00:00
|
|
|
|
|
|
|
return false;
|
2018-06-28 20:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-05 05:59:56 +00:00
|
|
|
* (@inheritdoc)
|
2018-06-28 20:57:17 +00:00
|
|
|
*/
|
2019-02-24 11:24:09 +00:00
|
|
|
public function releaseLock($key, $override = false)
|
2018-06-28 20:57:17 +00:00
|
|
|
{
|
2019-08-13 19:20:41 +00:00
|
|
|
$success = false;
|
|
|
|
|
|
|
|
if (!empty(self::$semaphore[$key])) {
|
|
|
|
try {
|
2019-08-15 15:55:17 +00:00
|
|
|
$success = @sem_release(self::$semaphore[$key]);
|
|
|
|
if (file_exists(self::keyToFile($key)) && $success) {
|
|
|
|
$success = unlink(self::keyToFile($key));
|
|
|
|
}
|
2019-08-13 19:20:41 +00:00
|
|
|
unset(self::$semaphore[$key]);
|
|
|
|
$this->markRelease($key);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
} else if ($override) {
|
|
|
|
if ($this->acquireLock($key)) {
|
|
|
|
$success = $this->releaseLock($key, true);
|
|
|
|
}
|
2018-06-28 20:57:17 +00:00
|
|
|
}
|
2019-08-13 19:20:41 +00:00
|
|
|
|
|
|
|
return $success;
|
2018-06-28 20:57:17 +00:00
|
|
|
}
|
2018-07-04 21:37:22 +00:00
|
|
|
|
|
|
|
/**
|
2018-07-05 05:59:56 +00:00
|
|
|
* (@inheritdoc)
|
2018-07-04 21:37:22 +00:00
|
|
|
*/
|
|
|
|
public function isLocked($key)
|
|
|
|
{
|
2018-07-05 18:57:31 +00:00
|
|
|
return isset(self::$semaphore[$key]);
|
2018-07-04 21:37:22 +00:00
|
|
|
}
|
2019-08-13 19:20:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return self::TYPE_SEMAPHORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getLocks(string $prefix = '')
|
|
|
|
{
|
|
|
|
$temp = get_temppath();
|
|
|
|
$locks = [];
|
|
|
|
foreach (glob(sprintf('%s/%s*.sem', $temp, $prefix)) as $lock) {
|
|
|
|
$lock = pathinfo($lock, PATHINFO_FILENAME);
|
|
|
|
if(sem_get(self::semaphoreKey($lock))) {
|
|
|
|
$locks[] = $lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $locks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function releaseAll($override = false)
|
|
|
|
{
|
|
|
|
$success = parent::releaseAll($override);
|
|
|
|
|
|
|
|
$temp = get_temppath();
|
|
|
|
foreach (glob(sprintf('%s/*.sem', $temp)) as $lock) {
|
|
|
|
$lock = pathinfo($lock, PATHINFO_FILENAME);
|
|
|
|
if (!$this->releaseLock($lock, true)) {
|
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
2018-06-28 20:57:17 +00:00
|
|
|
}
|