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
21 * @brief Check for memcache and open a connection if configured
23 * @return object|boolean The memcache object - or "false" if not successful
25 private static function connectMemcache() {
26 if (!function_exists('memcache_connect')) {
30 if (!Config::get('system', 'memcache')) {
34 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
35 $memcache_port = Config::get('system', 'memcache_port', 11211);
37 $memcache = new Memcache;
39 if (!$memcache->connect($memcache_host, $memcache_port)) {
47 * @brief Sets a lock for a given name
49 * @param string $fn_name Name of the lock
50 * @param integer $timeout Seconds until we give up
52 * @return boolean Was the lock successful?
54 public static function set($fn_name, $timeout = 120) {
58 $memcache = self::connectMemcache();
59 if (is_object($memcache)) {
61 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
64 $lock = $memcache->get($cachekey);
66 if (!is_bool($lock)) {
69 // When the process id isn't used anymore, we can safely claim the lock for us.
70 // Or we do want to lock something that was already locked by us.
71 if (!posix_kill($pid, 0) OR ($pid == getmypid())) {
76 $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
79 if (!$got_lock AND ($timeout > 0)) {
80 usleep($wait_sec * 1000000);
82 } while (!$got_lock AND ((time() - $start) < $timeout));
91 $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
93 if (dbm::is_result($lock)) {
94 if ($lock['locked']) {
95 // When the process id isn't used anymore, we can safely claim the lock for us.
96 if (!posix_kill($lock['pid'], 0)) {
97 $lock['locked'] = false;
99 // We want to lock something that was already locked by us? So we got the lock.
100 if ($lock['pid'] == getmypid()) {
104 if (!$lock['locked']) {
105 dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
108 } elseif (!dbm::is_result($lock)) {
109 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
115 if (!$got_lock AND ($timeout > 0)) {
118 } while (!$got_lock AND ((time() - $start) < $timeout));
124 * @brief Removes a lock if it was set by us
126 * @param string $fn_name Name of the lock
128 public static function remove($fn_name) {
129 $memcache = self::connectMemcache();
130 if (is_object($memcache)) {
131 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
132 $lock = $memcache->get($cachekey);
134 if (!is_bool($lock)) {
135 if ((int)$lock == getmypid()) {
136 $memcache->delete($cachekey);
142 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
147 * @brief Removes all lock that were set by us
149 public static function removeAll() {
150 $memcache = self::connectMemcache();
151 if (is_object($memcache)) {
152 // We cannot delete all cache entries, but this doesn't matter with memcache
156 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));