2018-06-28 16:57:17 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Lock;
|
|
|
|
|
|
|
|
class SemaphoreLockDriver extends AbstractLockDriver
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (!function_exists('sem_get')) {
|
|
|
|
throw new \Exception('Semaphore lock not supported');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Creates a semaphore key
|
|
|
|
*
|
|
|
|
* @param string $key Name of the lock
|
|
|
|
*
|
|
|
|
* @return integer the semaphore key
|
|
|
|
*/
|
2018-06-28 17:02:00 -04:00
|
|
|
private static function semaphoreKey(string $key)
|
2018-06-28 16:57:17 -04:00
|
|
|
{
|
|
|
|
$temp = get_temppath();
|
|
|
|
|
|
|
|
$file = $temp.'/'.$key.'.sem';
|
|
|
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
file_put_contents($file, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ftok($file, 'f');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Sets a lock for a given name
|
|
|
|
*
|
|
|
|
* @param string $key The Name of the lock
|
|
|
|
* @param integer $timeout Seconds until we give up
|
|
|
|
*
|
|
|
|
* @return boolean Was the lock successful?
|
|
|
|
*/
|
|
|
|
public function acquireLock(string $key, int $timeout = 120)
|
|
|
|
{
|
|
|
|
$this->acquiredLocks[$key] = sem_get(self::semaphoreKey($key));
|
|
|
|
if ($this->acquiredLocks[$key]) {
|
|
|
|
return sem_acquire($this->acquiredLocks[$key], ($timeout == 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Removes a lock if it was set by us
|
|
|
|
*
|
|
|
|
* @param string $key Name of the lock
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function releaseLock(string $key)
|
|
|
|
{
|
|
|
|
if (empty($this->acquiredLocks[$key])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$success = @sem_release($this->acquiredLocks[$key]);
|
|
|
|
unset($this->acquiredLocks[$key]);
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|