3 namespace Friendica\Util;
6 * @file src/Util/Lock.php
7 * @brief Functions for preventing parallel execution of functions
15 * @brief This class contain Functions for preventing parallel execution of functions
20 // Provide some ability to lock a PHP function so that multiple processes
21 // can't run the function concurrently
23 public static function set($fn_name, $wait_sec = 2, $timeout = 30) {
32 dba:p("LOCK TABLE `locks` WRITE");
33 $lock = dba::select('locks', array('locked'), array('name' => $fn_name), array('limit' => 1));
35 if ((dbm::is_result($lock)) AND !$lock['locked']) {
36 dba::update('locks', array('locked' => true), array('name' => $fn_name));
38 } elseif (!dbm::is_result($lock)) {
39 dbm::insert('locks', array('name' => $fn_name, 'locked' => true));
43 dbm::p("UNLOCK TABLES");
48 } while (!$got_lock AND ((time() - $start) < $timeout));
50 logger('lock_function: function ' . $fn_name . ' with blocking = ' . $block . ' got_lock = ' . $got_lock . ' time = ' . (time() - $start), LOGGER_DEBUG);
55 public static function remove($fn_name) {
56 dba::update('locks', array('locked' => false), array('name' => $fn_name));
58 logger('unlock_function: released lock for function ' . $fn_name, LOGGER_DEBUG);