]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Lock.php
Merge pull request #5294 from annando/use-gravity
[friendica.git] / src / Util / Lock.php
index 9bf67d7043b8682e9af46078fe793795d1507fe2..eba264ad932d6ba6357c5fd566cafbc8c7ff8a30 100644 (file)
@@ -1,30 +1,35 @@
 <?php
-
+/**
+ * @file src/Util/Lock.php
+ */
 namespace Friendica\Util;
 
 /**
  * @file src/Util/Lock.php
  * @brief Functions for preventing parallel execution of functions
- *
  */
 
 use Friendica\Core\Config;
-use Friendica\Database\Dbm;
+use Friendica\Database\DBM;
 use Memcache;
 use dba;
 
+require_once 'include/dba.php';
+
 /**
  * @brief This class contain Functions for preventing parallel execution of functions
  */
-class Lock {
-       private static $semaphore = array();
+class Lock
+{
+       private static $semaphore = [];
 
        /**
         * @brief Check for memcache and open a connection if configured
         *
         * @return object|boolean The memcache object - or "false" if not successful
         */
-       private static function connectMemcache() {
+       private static function connectMemcache()
+       {
                if (!function_exists('memcache_connect')) {
                        return false;
                }
@@ -52,13 +57,14 @@ class Lock {
         *
         * @return ressource the semaphore key
         */
-       private static function semaphoreKey($fn_name) {
+       private static function semaphoreKey($fn_name)
+       {
                $temp = get_temppath();
 
                $file = $temp.'/'.$fn_name.'.sem';
 
                if (!file_exists($file)) {
-                       file_put_contents($file, $function);
+                       file_put_contents($file, $fn_name);
                }
 
                return ftok($file, 'f');
@@ -67,12 +73,13 @@ class Lock {
        /**
         * @brief Sets a lock for a given name
         *
-        * @param string $fn_name Name of the lock
+        * @param string  $fn_name Name of the lock
         * @param integer $timeout Seconds until we give up
         *
         * @return boolean Was the lock successful?
         */
-       public static function set($fn_name, $timeout = 120) {
+       public static function set($fn_name, $timeout = 120)
+       {
                $got_lock = false;
                $start = time();
 
@@ -119,9 +126,9 @@ class Lock {
 
                do {
                        dba::lock('locks');
-                       $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
+                       $lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $fn_name]);
 
-                       if (Dbm::is_result($lock)) {
+                       if (DBM::is_result($lock)) {
                                if ($lock['locked']) {
                                        // When the process id isn't used anymore, we can safely claim the lock for us.
                                        if (!posix_kill($lock['pid'], 0)) {
@@ -133,11 +140,11 @@ class Lock {
                                        }
                                }
                                if (!$lock['locked']) {
-                                       dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
+                                       dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $fn_name]);
                                        $got_lock = true;
                                }
-                       } elseif (!Dbm::is_result($lock)) {
-                               dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
+                       } elseif (!DBM::is_result($lock)) {
+                               dba::insert('locks', ['name' => $fn_name, 'locked' => true, 'pid' => getmypid()]);
                                $got_lock = true;
                        }
 
@@ -155,8 +162,10 @@ class Lock {
         * @brief Removes a lock if it was set by us
         *
         * @param string $fn_name Name of the lock
+        * @return mixed
         */
-       public static function remove($fn_name) {
+       public static function remove($fn_name)
+       {
                if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
                        if (empty(self::$semaphore[$fn_name])) {
                                return false;
@@ -180,21 +189,23 @@ class Lock {
                        return;
                }
 
-               dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
+               dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $fn_name, 'pid' => getmypid()]);
                return;
        }
 
        /**
         * @brief Removes all lock that were set by us
+        * @return void
         */
-       public static function removeAll() {
+       public static function removeAll()
+       {
                $memcache = self::connectMemcache();
                if (is_object($memcache)) {
                        // We cannot delete all cache entries, but this doesn't matter with memcache
                        return;
                }
 
-               dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));
+               dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
                return;
        }
 }