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 public static function memcache() {
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
51 * @param integer $wait_sec Time between to lock attempts
53 * @return boolean Was the lock successful?
55 public static function set($fn_name, $timeout = 30, $wait_sec = 2) {
63 $memcache = self::memcache();
64 if (is_object($memcache)) {
65 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
68 $lock = $memcache->get($cachekey);
70 if (!is_bool($lock)) {
73 // When the process id isn't used anymore, we can safely claim the lock for us.
74 // Or we do want to lock something that was already locked by us.
75 if (!posix_kill($pid, 0) OR ($pid == getmypid())) {
80 $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
86 } while (!$got_lock AND ((time() - $start) < $timeout));
92 $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
94 if (dbm::is_result($lock)) {
95 if ($lock['locked']) {
96 // When the process id isn't used anymore, we can safely claim the lock for us.
97 if (!posix_kill($lock['pid'], 0)) {
98 $lock['locked'] = false;
100 // We want to lock something that was already locked by us? So we got the lock.
101 if ($lock['pid'] == getmypid()) {
105 if (!$lock['locked']) {
106 dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
109 } elseif (!dbm::is_result($lock)) {
110 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
119 } while (!$got_lock AND ((time() - $start) < $timeout));
125 * @brief Removes a lock if it was set by us
127 * @param string $fn_name Name of the lock
129 public static function remove($fn_name) {
130 $memcache = self::memcache();
131 if (is_object($memcache)) {
132 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
133 $lock = $memcache->get($cachekey);
135 if (!is_bool($lock)) {
136 if ((int)$lock == getmypid()) {
137 $memcache->delete($cachekey);
143 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
148 * @brief Removes all lock that were set by us
150 public static function removeAll() {
151 $memcache = self::memcache();
152 if (is_object($memcache)) {
153 // We cannot delete all cache entries, but this doesn't matter with memcache
157 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));