3 namespace Friendica\Util;
6 * @file src/Util/Lock.php
7 * @brief Functions for preventing parallel execution of functions
11 use Friendica\Core\Config;
12 use Friendica\Database\DBM;
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 semaphoreKey($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 // The second parameter for "sem_acquire" doesn't exist before 5.6.1
80 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
81 self::$semaphore[$fn_name] = sem_get(self::semaphoreKey($fn_name));
82 if (self::$semaphore[$fn_name]) {
83 return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
87 $memcache = self::connectMemcache();
88 if (is_object($memcache)) {
89 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
92 // We only lock to be sure that nothing happens at exactly the same time
94 $lock = $memcache->get($cachekey);
96 if (!is_bool($lock)) {
99 // When the process id isn't used anymore, we can safely claim the lock for us.
100 // Or we do want to lock something that was already locked by us.
101 if (!posix_kill($pid, 0) || ($pid == getmypid())) {
105 if (is_bool($lock)) {
106 $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
112 if (!$got_lock && ($timeout > 0)) {
113 usleep(rand(10000, 200000));
115 } while (!$got_lock && ((time() - $start) < $timeout));
122 $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
124 if (DBM::is_result($lock)) {
125 if ($lock['locked']) {
126 // When the process id isn't used anymore, we can safely claim the lock for us.
127 if (!posix_kill($lock['pid'], 0)) {
128 $lock['locked'] = false;
130 // We want to lock something that was already locked by us? So we got the lock.
131 if ($lock['pid'] == getmypid()) {
135 if (!$lock['locked']) {
136 dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
139 } elseif (!DBM::is_result($lock)) {
140 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
146 if (!$got_lock && ($timeout > 0)) {
147 usleep(rand(100000, 2000000));
149 } while (!$got_lock && ((time() - $start) < $timeout));
155 * @brief Removes a lock if it was set by us
157 * @param string $fn_name Name of the lock
159 public static function remove($fn_name) {
160 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
161 if (empty(self::$semaphore[$fn_name])) {
164 $success = @sem_release(self::$semaphore[$fn_name]);
165 unset(self::$semaphore[$fn_name]);
170 $memcache = self::connectMemcache();
171 if (is_object($memcache)) {
172 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
173 $lock = $memcache->get($cachekey);
175 if (!is_bool($lock)) {
176 if ((int)$lock == getmypid()) {
177 $memcache->delete($cachekey);
183 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
188 * @brief Removes all lock that were set by us
190 public static function removeAll() {
191 $memcache = self::connectMemcache();
192 if (is_object($memcache)) {
193 // We cannot delete all cache entries, but this doesn't matter with memcache
197 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));