<?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 Memcache;
use dba;
-use dbm;
/**
* @brief This class contain Functions for preventing parallel execution of functions
*/
-class Lock {
+class Lock
+{
private static $semaphore = array();
- /**
+ /**
* @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;
}
*
* @return ressource the semaphore key
*/
- private static function semaphore_key($fn_name) {
+ private static function semaphoreKey($fn_name)
+ {
$temp = get_temppath();
$file = $temp.'/'.$fn_name.'.sem';
/**
* @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();
- if (function_exists('sem_get')) {
- self::$semaphore[$fn_name] = sem_get(self::semaphore_key($fn_name));
+ // The second parameter for "sem_acquire" doesn't exist before 5.6.1
+ if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
+ self::$semaphore[$fn_name] = sem_get(self::semaphoreKey($fn_name));
if (self::$semaphore[$fn_name]) {
return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
}
dba::lock('locks');
$lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
- 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)) {
dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
$got_lock = true;
}
- } elseif (!dbm::is_result($lock)) {
+ } elseif (!DBM::is_result($lock)) {
dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
$got_lock = true;
}
* @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) {
- if (function_exists('sem_get')) {
+ 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;
} else {
- $rid = self::$semaphore[$fn_name];
- self::$semaphore[$fn_name] = 0;
- return @sem_release($rid);
+ $success = @sem_release(self::$semaphore[$fn_name]);
+ unset(self::$semaphore[$fn_name]);
+ return $success;
}
}
/**
* @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