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)) {
60 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
63 // We only lock to be sure that nothing happens at exactly the same time
65 $lock = $memcache->get($cachekey);
67 if (!is_bool($lock)) {
70 // When the process id isn't used anymore, we can safely claim the lock for us.
71 // Or we do want to lock something that was already locked by us.
72 if (!posix_kill($pid, 0) || ($pid == getmypid())) {
77 $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
83 if (!$got_lock && ($timeout > 0)) {
84 usleep(rand(10000, 200000));
86 } while (!$got_lock && ((time() - $start) < $timeout));
93 $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
95 if (dbm::is_result($lock)) {
96 if ($lock['locked']) {
97 // When the process id isn't used anymore, we can safely claim the lock for us.
98 if (!posix_kill($lock['pid'], 0)) {
99 $lock['locked'] = false;
101 // We want to lock something that was already locked by us? So we got the lock.
102 if ($lock['pid'] == getmypid()) {
106 if (!$lock['locked']) {
107 dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
110 } elseif (!dbm::is_result($lock)) {
111 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
117 if (!$got_lock && ($timeout > 0)) {
118 usleep(rand(100000, 2000000));
120 } while (!$got_lock && ((time() - $start) < $timeout));
126 * @brief Removes a lock if it was set by us
128 * @param string $fn_name Name of the lock
130 public static function remove($fn_name) {
131 $memcache = self::connectMemcache();
132 if (is_object($memcache)) {
133 $cachekey = get_app()->get_hostname().";lock:".$fn_name;
134 $lock = $memcache->get($cachekey);
136 if (!is_bool($lock)) {
137 if ((int)$lock == getmypid()) {
138 $memcache->delete($cachekey);
144 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
149 * @brief Removes all lock that were set by us
151 public static function removeAll() {
152 $memcache = self::connectMemcache();
153 if (is_object($memcache)) {
154 // We cannot delete all cache entries, but this doesn't matter with memcache
158 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));