3 * @file src/Util/Lock.php
5 namespace Friendica\Util;
8 * @file src/Util/Lock.php
9 * @brief Functions for preventing parallel execution of functions
12 use Friendica\Core\Config;
13 use Friendica\Database\DBM;
17 require_once 'include/dba.php';
20 * @brief This class contain Functions for preventing parallel execution of functions
24 private static $semaphore = [];
27 * @brief Check for memcache and open a connection if configured
29 * @return object|boolean The memcache object - or "false" if not successful
31 private static function connectMemcache()
33 if (!function_exists('memcache_connect')) {
37 if (!Config::get('system', 'memcache')) {
41 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
42 $memcache_port = Config::get('system', 'memcache_port', 11211);
44 $memcache = new Memcache;
46 if (!$memcache->connect($memcache_host, $memcache_port)) {
54 * @brief Creates a semaphore key
56 * @param string $fn_name Name of the lock
58 * @return ressource the semaphore key
60 private static function semaphoreKey($fn_name)
62 $temp = get_temppath();
64 $file = $temp.'/'.$fn_name.'.sem';
66 if (!file_exists($file)) {
67 file_put_contents($file, $fn_name);
70 return ftok($file, 'f');
74 * @brief Sets a lock for a given name
76 * @param string $fn_name Name of the lock
77 * @param integer $timeout Seconds until we give up
79 * @return boolean Was the lock successful?
81 public static function set($fn_name, $timeout = 120)
86 // The second parameter for "sem_acquire" doesn't exist before 5.6.1
87 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
88 self::$semaphore[$fn_name] = sem_get(self::semaphoreKey($fn_name));
89 if (self::$semaphore[$fn_name]) {
90 return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
94 $memcache = self::connectMemcache();
95 if (is_object($memcache)) {
96 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
99 // We only lock to be sure that nothing happens at exactly the same time
101 $lock = $memcache->get($cachekey);
103 if (!is_bool($lock)) {
106 // When the process id isn't used anymore, we can safely claim the lock for us.
107 // Or we do want to lock something that was already locked by us.
108 if (!posix_kill($pid, 0) || ($pid == getmypid())) {
112 if (is_bool($lock)) {
113 $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
119 if (!$got_lock && ($timeout > 0)) {
120 usleep(rand(10000, 200000));
122 } while (!$got_lock && ((time() - $start) < $timeout));
129 $lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $fn_name]);
131 if (DBM::is_result($lock)) {
132 if ($lock['locked']) {
133 // When the process id isn't used anymore, we can safely claim the lock for us.
134 if (!posix_kill($lock['pid'], 0)) {
135 $lock['locked'] = false;
137 // We want to lock something that was already locked by us? So we got the lock.
138 if ($lock['pid'] == getmypid()) {
142 if (!$lock['locked']) {
143 dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $fn_name]);
146 } elseif (!DBM::is_result($lock)) {
147 dba::insert('locks', ['name' => $fn_name, 'locked' => true, 'pid' => getmypid()]);
153 if (!$got_lock && ($timeout > 0)) {
154 usleep(rand(100000, 2000000));
156 } while (!$got_lock && ((time() - $start) < $timeout));
162 * @brief Removes a lock if it was set by us
164 * @param string $fn_name Name of the lock
167 public static function remove($fn_name)
169 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
170 if (empty(self::$semaphore[$fn_name])) {
173 $success = @sem_release(self::$semaphore[$fn_name]);
174 unset(self::$semaphore[$fn_name]);
179 $memcache = self::connectMemcache();
180 if (is_object($memcache)) {
181 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
182 $lock = $memcache->get($cachekey);
184 if (!is_bool($lock)) {
185 if ((int)$lock == getmypid()) {
186 $memcache->delete($cachekey);
192 dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $fn_name, 'pid' => getmypid()]);
197 * @brief Removes all lock that were set by us
200 public static function removeAll()
202 $memcache = self::connectMemcache();
203 if (is_object($memcache)) {
204 // We cannot delete all cache entries, but this doesn't matter with memcache
208 dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);