3 namespace Friendica\Util;
6 * @file src/Util/Lock.php
7 * @brief Functions for preventing parallel execution of functions
11 use Friendica\Core\Config;
17 * @brief This class contain Functions for preventing parallel execution of functions
20 private static $semaphore = array();
23 * @brief Check for memcache and open a connection if configured
25 * @return object|boolean The memcache object - or "false" if not successful
27 private static function connectMemcache() {
28 if (!function_exists('memcache_connect')) {
32 if (!Config::get('system', 'memcache')) {
36 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
37 $memcache_port = Config::get('system', 'memcache_port', 11211);
39 $memcache = new Memcache;
41 if (!$memcache->connect($memcache_host, $memcache_port)) {
49 * @brief Creates a semaphore key
51 * @param string $fn_name Name of the lock
53 * @return ressource the semaphore key
55 private static function semaphore_key($fn_name) {
56 $temp = get_temppath();
58 $file = $temp.'/'.$fn_name.'.sem';
60 if (!file_exists($file)) {
61 file_put_contents($file, $function);
64 return ftok($file, 'f');
68 * @brief Sets a lock for a given name
70 * @param string $fn_name Name of the lock
71 * @param integer $timeout Seconds until we give up
73 * @return boolean Was the lock successful?
75 public static function set($fn_name, $timeout = 120) {
79 if (function_exists('sem_get')) {
80 self::$semaphore[$fn_name] = sem_get(self::semaphore_key($fn_name));
81 if (self::$semaphore[$fn_name]) {
82 return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
86 $memcache = self::connectMemcache();
87 if (is_object($memcache)) {
88 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
91 // We only lock to be sure that nothing happens at exactly the same time
93 $lock = $memcache->get($cachekey);
95 if (!is_bool($lock)) {
98 // When the process id isn't used anymore, we can safely claim the lock for us.
99 // Or we do want to lock something that was already locked by us.
100 if (!posix_kill($pid, 0) || ($pid == getmypid())) {
104 if (is_bool($lock)) {
105 $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
111 if (!$got_lock && ($timeout > 0)) {
112 usleep(rand(10000, 200000));
114 } while (!$got_lock && ((time() - $start) < $timeout));
121 $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
123 if (dbm::is_result($lock)) {
124 if ($lock['locked']) {
125 // When the process id isn't used anymore, we can safely claim the lock for us.
126 if (!posix_kill($lock['pid'], 0)) {
127 $lock['locked'] = false;
129 // We want to lock something that was already locked by us? So we got the lock.
130 if ($lock['pid'] == getmypid()) {
134 if (!$lock['locked']) {
135 dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
138 } elseif (!dbm::is_result($lock)) {
139 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
145 if (!$got_lock && ($timeout > 0)) {
146 usleep(rand(100000, 2000000));
148 } while (!$got_lock && ((time() - $start) < $timeout));
154 * @brief Removes a lock if it was set by us
156 * @param string $fn_name Name of the lock
158 public static function remove($fn_name) {
159 if (function_exists('sem_get') && self::$semaphore[$fn_name]) {
160 sem_release(self::$semaphore[$fn_name]);
164 $memcache = self::connectMemcache();
165 if (is_object($memcache)) {
166 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
167 $lock = $memcache->get($cachekey);
169 if (!is_bool($lock)) {
170 if ((int)$lock == getmypid()) {
171 $memcache->delete($cachekey);
177 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
182 * @brief Removes all lock that were set by us
184 public static function removeAll() {
185 $memcache = self::connectMemcache();
186 if (is_object($memcache)) {
187 // We cannot delete all cache entries, but this doesn't matter with memcache
191 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));