3 namespace Friendica\Util\Lock;
5 class SemaphoreLockDriver implements ILockDriver
8 * @var array stored semaphores
10 private static $semaphore = [];
13 * @brief Creates a semaphore key
15 * @param string $key Name of the lock
17 * @return integer the semaphore key
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');
34 * @brief Sets a lock for a given name
36 * @param string $key The Name of the lock
37 * @param integer $timeout Seconds until we give up
39 * @return boolean Was the lock successful?
41 public function acquireLock($key, $timeout = 120)
43 // The second parameter for "sem_acquire" doesn't exist before 5.6.1
44 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
45 self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
46 if (self::$semaphore[$key]) {
47 return sem_acquire(self::$semaphore[$key], ($timeout == 0));
53 * @brief Removes a lock if it was set by us
55 * @param string $key Name of the lock
59 public function releaseLock($key)
61 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
62 if (empty(self::$semaphore[$key])) {
65 $success = @sem_release(self::$semaphore[$key]);
66 unset(self::$semaphore[$key]);
73 * @brief Removes all lock that were set by us
77 public function releaseAll()
79 // not needed/supported