]> git.mxchange.org Git - friendica.git/blob - src/Util/Lock.php
Class file relocations
[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 Friendica\Database\DBM;
13 use Memcache;
14 use dba;
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 semaphoreKey($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                 // The second parameter for "sem_acquire" doesn't exist before 5.6.1
80                 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
81                         self::$semaphore[$fn_name] = sem_get(self::semaphoreKey($fn_name));
82                         if (self::$semaphore[$fn_name]) {
83                                 return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
84                         }
85                 }
86
87                 $memcache = self::connectMemcache();
88                 if (is_object($memcache)) {
89                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
90
91                         do {
92                                 // We only lock to be sure that nothing happens at exactly the same time
93                                 dba::lock('locks');
94                                 $lock = $memcache->get($cachekey);
95
96                                 if (!is_bool($lock)) {
97                                         $pid = (int)$lock;
98
99                                         // When the process id isn't used anymore, we can safely claim the lock for us.
100                                         // Or we do want to lock something that was already locked by us.
101                                         if (!posix_kill($pid, 0) || ($pid == getmypid())) {
102                                                 $lock = false;
103                                         }
104                                 }
105                                 if (is_bool($lock)) {
106                                         $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
107                                         $got_lock = true;
108                                 }
109
110                                 dba::unlock();
111
112                                 if (!$got_lock && ($timeout > 0)) {
113                                         usleep(rand(10000, 200000));
114                                 }
115                         } while (!$got_lock && ((time() - $start) < $timeout));
116
117                         return $got_lock;
118                 }
119
120                 do {
121                         dba::lock('locks');
122                         $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
123
124                         if (DBM::is_result($lock)) {
125                                 if ($lock['locked']) {
126                                         // When the process id isn't used anymore, we can safely claim the lock for us.
127                                         if (!posix_kill($lock['pid'], 0)) {
128                                                 $lock['locked'] = false;
129                                         }
130                                         // We want to lock something that was already locked by us? So we got the lock.
131                                         if ($lock['pid'] == getmypid()) {
132                                                 $got_lock = true;
133                                         }
134                                 }
135                                 if (!$lock['locked']) {
136                                         dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
137                                         $got_lock = true;
138                                 }
139                         } elseif (!DBM::is_result($lock)) {
140                                 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
141                                 $got_lock = true;
142                         }
143
144                         dba::unlock();
145
146                         if (!$got_lock && ($timeout > 0)) {
147                                 usleep(rand(100000, 2000000));
148                         }
149                 } while (!$got_lock && ((time() - $start) < $timeout));
150
151                 return $got_lock;
152         }
153
154         /**
155          * @brief Removes a lock if it was set by us
156          *
157          * @param string $fn_name Name of the lock
158          */
159         public static function remove($fn_name) {
160                 if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
161                         if (empty(self::$semaphore[$fn_name])) {
162                                 return false;
163                         } else {
164                                 $success = @sem_release(self::$semaphore[$fn_name]);
165                                 unset(self::$semaphore[$fn_name]);
166                                 return $success;
167                         }
168                 }
169
170                 $memcache = self::connectMemcache();
171                 if (is_object($memcache)) {
172                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
173                         $lock = $memcache->get($cachekey);
174
175                         if (!is_bool($lock)) {
176                                 if ((int)$lock == getmypid()) {
177                                         $memcache->delete($cachekey);
178                                 }
179                         }
180                         return;
181                 }
182
183                 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
184                 return;
185         }
186
187         /**
188          * @brief Removes all lock that were set by us
189          */
190         public static function removeAll() {
191                 $memcache = self::connectMemcache();
192                 if (is_object($memcache)) {
193                         // We cannot delete all cache entries, but this doesn't matter with memcache
194                         return;
195                 }
196
197                 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));
198                 return;
199         }
200 }