]> git.mxchange.org Git - friendica.git/blob - src/Util/Lock.php
Hide semaphone warning
[friendica.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         private static $semaphore = array();
21
22        /**
23          * @brief Check for memcache and open a connection if configured
24          *
25          * @return object|boolean The memcache object - or "false" if not successful
26          */
27         private static function connectMemcache() {
28                 if (!function_exists('memcache_connect')) {
29                         return false;
30                 }
31
32                 if (!Config::get('system', 'memcache')) {
33                         return false;
34                 }
35
36                 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
37                 $memcache_port = Config::get('system', 'memcache_port', 11211);
38
39                 $memcache = new Memcache;
40
41                 if (!$memcache->connect($memcache_host, $memcache_port)) {
42                         return false;
43                 }
44
45                 return $memcache;
46         }
47
48         /**
49          * @brief Creates a semaphore key
50          *
51          * @param string $fn_name Name of the lock
52          *
53          * @return ressource the semaphore key
54          */
55         private static function semaphore_key($fn_name) {
56                 $temp = get_temppath();
57
58                 $file = $temp.'/'.$fn_name.'.sem';
59
60                 if (!file_exists($file)) {
61                         file_put_contents($file, $function);
62                 }
63
64                 return ftok($file, 'f');
65         }
66
67         /**
68          * @brief Sets a lock for a given name
69          *
70          * @param string $fn_name Name of the lock
71          * @param integer $timeout Seconds until we give up
72          *
73          * @return boolean Was the lock successful?
74          */
75         public static function set($fn_name, $timeout = 120) {
76                 $got_lock = false;
77                 $start = time();
78
79                 if (function_exists('sem_get')) {
80                         self::$semaphore[$fn_name] = sem_get(self::semaphore_key($fn_name));
81                         if (self::$semaphore[$fn_name]) {
82                                 return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
83                         }
84                 }
85
86                 $memcache = self::connectMemcache();
87                 if (is_object($memcache)) {
88                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
89
90                         do {
91                                 // We only lock to be sure that nothing happens at exactly the same time
92                                 dba::lock('locks');
93                                 $lock = $memcache->get($cachekey);
94
95                                 if (!is_bool($lock)) {
96                                         $pid = (int)$lock;
97
98                                         // When the process id isn't used anymore, we can safely claim the lock for us.
99                                         // Or we do want to lock something that was already locked by us.
100                                         if (!posix_kill($pid, 0) || ($pid == getmypid())) {
101                                                 $lock = false;
102                                         }
103                                 }
104                                 if (is_bool($lock)) {
105                                         $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
106                                         $got_lock = true;
107                                 }
108
109                                 dba::unlock();
110
111                                 if (!$got_lock && ($timeout > 0)) {
112                                         usleep(rand(10000, 200000));
113                                 }
114                         } while (!$got_lock && ((time() - $start) < $timeout));
115
116                         return $got_lock;
117                 }
118
119                 do {
120                         dba::lock('locks');
121                         $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
122
123                         if (dbm::is_result($lock)) {
124                                 if ($lock['locked']) {
125                                         // When the process id isn't used anymore, we can safely claim the lock for us.
126                                         if (!posix_kill($lock['pid'], 0)) {
127                                                 $lock['locked'] = false;
128                                         }
129                                         // We want to lock something that was already locked by us? So we got the lock.
130                                         if ($lock['pid'] == getmypid()) {
131                                                 $got_lock = true;
132                                         }
133                                 }
134                                 if (!$lock['locked']) {
135                                         dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
136                                         $got_lock = true;
137                                 }
138                         } elseif (!dbm::is_result($lock)) {
139                                 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
140                                 $got_lock = true;
141                         }
142
143                         dba::unlock();
144
145                         if (!$got_lock && ($timeout > 0)) {
146                                 usleep(rand(100000, 2000000));
147                         }
148                 } while (!$got_lock && ((time() - $start) < $timeout));
149
150                 return $got_lock;
151         }
152
153         /**
154          * @brief Removes a lock if it was set by us
155          *
156          * @param string $fn_name Name of the lock
157          */
158         public static function remove($fn_name) {
159                 if (function_exists('sem_get') && self::$semaphore[$fn_name]) {
160                         return @sem_release(self::$semaphore[$fn_name]);
161                 }
162
163                 $memcache = self::connectMemcache();
164                 if (is_object($memcache)) {
165                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
166                         $lock = $memcache->get($cachekey);
167
168                         if (!is_bool($lock)) {
169                                 if ((int)$lock == getmypid()) {
170                                         $memcache->delete($cachekey);
171                                 }
172                         }
173                         return;
174                 }
175
176                 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
177                 return;
178         }
179
180         /**
181          * @brief Removes all lock that were set by us
182          */
183         public static function removeAll() {
184                 $memcache = self::connectMemcache();
185                 if (is_object($memcache)) {
186                         // We cannot delete all cache entries, but this doesn't matter with memcache
187                         return;
188                 }
189
190                 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));
191                 return;
192         }
193 }