Merge pull request #3522 from annando/1706-lock
[friendica.git/.git] / src / Util / Lock.php
1 <?php
2
3 namespace Friendica\Util;
4
5 /**
6  * @file src/Util/Lock.php
7  * @brief Functions for preventing parallel execution of functions
8  *
9  */
10
11 use Friendica\Core\Config;
12 use Memcache;
13 use dba;
14 use dbm;
15
16 /**
17  * @brief This class contain Functions for preventing parallel execution of functions
18  */
19 class Lock {
20        /**
21          * @brief Check for memcache and open a connection if configured
22          *
23          * @return object|boolean The memcache object - or "false" if not successful
24          */
25         private static function connectMemcache() {
26                 if (!function_exists('memcache_connect')) {
27                         return false;
28                 }
29
30                 if (!Config::get('system', 'memcache')) {
31                         return false;
32                 }
33
34                 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
35                 $memcache_port = Config::get('system', 'memcache_port', 11211);
36
37                 $memcache = new Memcache;
38
39                 if (!$memcache->connect($memcache_host, $memcache_port)) {
40                         return false;
41                 }
42
43                 return $memcache;
44         }
45
46         /**
47          * @brief Sets a lock for a given name
48          *
49          * @param string $fn_name Name of the lock
50          * @param integer $timeout Seconds until we give up
51          *
52          * @return boolean Was the lock successful?
53          */
54         public static function set($fn_name, $timeout = 120) {
55                 $got_lock = false;
56                 $start = time();
57
58                 $memcache = self::connectMemcache();
59                 if (is_object($memcache)) {
60                         $wait_sec = 0.2;
61                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
62
63                         do {
64                                 $lock = $memcache->get($cachekey);
65
66                                 if (!is_bool($lock)) {
67                                         $pid = (int)$lock;
68
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())) {
72                                                 $lock = false;
73                                         }
74                                 }
75                                 if (is_bool($lock)) {
76                                         $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
77                                         $got_lock = true;
78                                 }
79                                 if (!$got_lock AND ($timeout > 0)) {
80                                         usleep($wait_sec * 1000000);
81                                 }
82                         } while (!$got_lock AND ((time() - $start) < $timeout));
83
84                         return $got_lock;
85                 }
86
87                 $wait_sec = 2;
88
89                 do {
90                         dba::lock('locks');
91                         $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
92
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;
98                                         }
99                                         // We want to lock something that was already locked by us? So we got the lock.
100                                         if ($lock['pid'] == getmypid()) {
101                                                 $got_lock = true;
102                                         }
103                                 }
104                                 if (!$lock['locked']) {
105                                         dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
106                                         $got_lock = true;
107                                 }
108                         } elseif (!dbm::is_result($lock)) {
109                                 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
110                                 $got_lock = true;
111                         }
112
113                         dba::unlock();
114
115                         if (!$got_lock AND ($timeout > 0)) {
116                                 sleep($wait_sec);
117                         }
118                 } while (!$got_lock AND ((time() - $start) < $timeout));
119
120                 return $got_lock;
121         }
122
123         /**
124          * @brief Removes a lock if it was set by us
125          *
126          * @param string $fn_name Name of the lock
127          */
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);
133
134                         if (!is_bool($lock)) {
135                                 if ((int)$lock == getmypid()) {
136                                         $memcache->delete($cachekey);
137                                 }
138                         }
139                         return;
140                 }
141
142                 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
143                 return;
144         }
145
146         /**
147          * @brief Removes all lock that were set by us
148          */
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
153                         return;
154                 }
155
156                 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));
157                 return;
158         }
159 }