3 namespace Friendica\Core\Lock;
5 class SemaphoreLockDriver extends AbstractLockDriver
7 private static $semaphore = [];
9 public function __construct()
11 if (!function_exists('sem_get')) {
12 throw new \Exception('Semaphore lock not supported');
19 private static function semaphoreKey($key)
21 $temp = get_temppath();
23 $file = $temp . '/' . $key . '.sem';
25 if (!file_exists($file)) {
26 file_put_contents($file, $key);
29 return ftok($file, 'f');
36 public function acquireLock($key, $timeout = 120)
38 self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
39 if (self::$semaphore[$key]) {
40 if (sem_acquire(self::$semaphore[$key], ($timeout == 0))) {
41 $this->markAcquire($key);
52 public function releaseLock($key)
54 if (empty(self::$semaphore[$key])) {
57 $success = @sem_release(self::$semaphore[$key]);
58 unset(self::$semaphore[$key]);
59 $this->markRelease($key);
67 public function isLocked($key)
69 return isset(self::$semaphore[$key]);