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;
18 * @brief This class contain Functions for preventing parallel execution of functions
22 private static $semaphore = array();
25 * @brief Check for memcache and open a connection if configured
27 * @return object|boolean The memcache object - or "false" if not successful
29 private static function connectMemcache()
31 if (!function_exists('memcache_connect')) {
35 if (!Config::get('system', 'memcache')) {
39 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
40 $memcache_port = Config::get('system', 'memcache_port', 11211);
42 $memcache = new Memcache;
44 if (!$memcache->connect($memcache_host, $memcache_port)) {
52 * @brief Creates a semaphore key
54 * @param string $fn_name Name of the lock
56 * @return ressource the semaphore key
58 private static function semaphoreKey($fn_name)
60 $temp = get_temppath();
62 $file = $temp.'/'.$fn_name.'.sem';
64 if (!file_exists($file)) {
65 file_put_contents($file, $function);
68 return ftok($file, 'f');
72 * @brief Sets a lock for a given name
74 * @param string $fn_name Name of the lock
75 * @param integer $timeout Seconds until we give up
77 * @return boolean Was the lock successful?
79 public static function set($fn_name, $timeout = 120)
84 // The second parameter for "sem_acquire" doesn't exist before 5.6.1
85 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
86 self::$semaphore[$fn_name] = sem_get(self::semaphoreKey($fn_name));
87 if (self::$semaphore[$fn_name]) {
88 return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
92 $memcache = self::connectMemcache();
93 if (is_object($memcache)) {
94 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
97 // We only lock to be sure that nothing happens at exactly the same time
99 $lock = $memcache->get($cachekey);
101 if (!is_bool($lock)) {
104 // When the process id isn't used anymore, we can safely claim the lock for us.
105 // Or we do want to lock something that was already locked by us.
106 if (!posix_kill($pid, 0) || ($pid == getmypid())) {
110 if (is_bool($lock)) {
111 $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
117 if (!$got_lock && ($timeout > 0)) {
118 usleep(rand(10000, 200000));
120 } while (!$got_lock && ((time() - $start) < $timeout));
127 $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
129 if (DBM::is_result($lock)) {
130 if ($lock['locked']) {
131 // When the process id isn't used anymore, we can safely claim the lock for us.
132 if (!posix_kill($lock['pid'], 0)) {
133 $lock['locked'] = false;
135 // We want to lock something that was already locked by us? So we got the lock.
136 if ($lock['pid'] == getmypid()) {
140 if (!$lock['locked']) {
141 dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
144 } elseif (!DBM::is_result($lock)) {
145 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
151 if (!$got_lock && ($timeout > 0)) {
152 usleep(rand(100000, 2000000));
154 } while (!$got_lock && ((time() - $start) < $timeout));
160 * @brief Removes a lock if it was set by us
162 * @param string $fn_name Name of the lock
165 public static function remove($fn_name)
167 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
168 if (empty(self::$semaphore[$fn_name])) {
171 $success = @sem_release(self::$semaphore[$fn_name]);
172 unset(self::$semaphore[$fn_name]);
177 $memcache = self::connectMemcache();
178 if (is_object($memcache)) {
179 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
180 $lock = $memcache->get($cachekey);
182 if (!is_bool($lock)) {
183 if ((int)$lock == getmypid()) {
184 $memcache->delete($cachekey);
190 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
195 * @brief Removes all lock that were set by us
198 public static function removeAll()
200 $memcache = self::connectMemcache();
201 if (is_object($memcache)) {
202 // We cannot delete all cache entries, but this doesn't matter with memcache
206 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));