2017-06-04 09:26:21 +02:00
|
|
|
<?php
|
2017-11-19 23:04:40 +01:00
|
|
|
/**
|
|
|
|
* @file src/Util/Lock.php
|
|
|
|
*/
|
2017-06-04 09:26:21 +02:00
|
|
|
namespace Friendica\Util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file src/Util/Lock.php
|
|
|
|
* @brief Functions for preventing parallel execution of functions
|
|
|
|
*/
|
|
|
|
|
2017-06-04 17:59:20 +02:00
|
|
|
use Friendica\Core\Config;
|
2017-11-08 04:57:46 +01:00
|
|
|
use Friendica\Database\DBM;
|
2017-06-04 17:59:20 +02:00
|
|
|
use Memcache;
|
2017-06-04 09:26:21 +02:00
|
|
|
use dba;
|
|
|
|
|
2017-12-17 21:24:57 +01:00
|
|
|
require_once 'include/dba.php';
|
|
|
|
|
2017-06-04 09:26:21 +02:00
|
|
|
/**
|
|
|
|
* @brief This class contain Functions for preventing parallel execution of functions
|
|
|
|
*/
|
2017-11-19 23:04:40 +01:00
|
|
|
class Lock
|
|
|
|
{
|
2018-01-15 14:05:12 +01:00
|
|
|
private static $semaphore = [];
|
2017-06-28 06:53:11 +02:00
|
|
|
|
2017-08-02 13:09:42 +02:00
|
|
|
/**
|
2017-06-06 19:25:28 +02:00
|
|
|
* @brief Check for memcache and open a connection if configured
|
|
|
|
*
|
|
|
|
* @return object|boolean The memcache object - or "false" if not successful
|
|
|
|
*/
|
2017-11-19 23:04:40 +01:00
|
|
|
private static function connectMemcache()
|
|
|
|
{
|
2017-06-06 19:25:28 +02:00
|
|
|
if (!function_exists('memcache_connect')) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-04 17:59:20 +02:00
|
|
|
|
2017-06-06 19:25:28 +02:00
|
|
|
if (!Config::get('system', 'memcache')) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-04 17:59:20 +02:00
|
|
|
|
2017-06-06 19:25:28 +02:00
|
|
|
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
|
|
|
|
$memcache_port = Config::get('system', 'memcache_port', 11211);
|
2017-06-04 17:59:20 +02:00
|
|
|
|
2017-06-06 19:25:28 +02:00
|
|
|
$memcache = new Memcache;
|
2017-06-04 17:59:20 +02:00
|
|
|
|
2017-06-06 19:25:28 +02:00
|
|
|
if (!$memcache->connect($memcache_host, $memcache_port)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-04 17:59:20 +02:00
|
|
|
|
2017-06-06 19:25:28 +02:00
|
|
|
return $memcache;
|
|
|
|
}
|
2017-06-04 09:26:21 +02:00
|
|
|
|
2017-06-28 06:53:11 +02:00
|
|
|
/**
|
|
|
|
* @brief Creates a semaphore key
|
|
|
|
*
|
|
|
|
* @param string $fn_name Name of the lock
|
|
|
|
*
|
|
|
|
* @return ressource the semaphore key
|
|
|
|
*/
|
2017-11-19 23:04:40 +01:00
|
|
|
private static function semaphoreKey($fn_name)
|
|
|
|
{
|
2017-06-28 06:53:11 +02:00
|
|
|
$temp = get_temppath();
|
|
|
|
|
|
|
|
$file = $temp.'/'.$fn_name.'.sem';
|
|
|
|
|
|
|
|
if (!file_exists($file)) {
|
2017-12-17 21:31:37 +01:00
|
|
|
file_put_contents($file, $fn_name);
|
2017-06-28 06:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ftok($file, 'f');
|
|
|
|
}
|
|
|
|
|
2017-06-04 10:36:08 +02:00
|
|
|
/**
|
|
|
|
* @brief Sets a lock for a given name
|
|
|
|
*
|
2017-11-19 23:04:40 +01:00
|
|
|
* @param string $fn_name Name of the lock
|
2017-06-04 10:36:08 +02:00
|
|
|
* @param integer $timeout Seconds until we give up
|
|
|
|
*
|
|
|
|
* @return boolean Was the lock successful?
|
|
|
|
*/
|
2017-11-19 23:04:40 +01:00
|
|
|
public static function set($fn_name, $timeout = 120)
|
|
|
|
{
|
2017-06-04 09:26:21 +02:00
|
|
|
$got_lock = false;
|
|
|
|
$start = time();
|
|
|
|
|
2017-09-30 09:51:09 +02:00
|
|
|
// 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', '>=')) {
|
2017-08-02 13:09:42 +02:00
|
|
|
self::$semaphore[$fn_name] = sem_get(self::semaphoreKey($fn_name));
|
2017-06-28 06:53:11 +02:00
|
|
|
if (self::$semaphore[$fn_name]) {
|
|
|
|
return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 19:42:47 +02:00
|
|
|
$memcache = self::connectMemcache();
|
2017-06-04 17:59:20 +02:00
|
|
|
if (is_object($memcache)) {
|
|
|
|
$cachekey = get_app()->get_hostname().";lock:".$fn_name;
|
|
|
|
|
|
|
|
do {
|
2017-06-12 17:42:54 +02:00
|
|
|
// We only lock to be sure that nothing happens at exactly the same time
|
|
|
|
dba::lock('locks');
|
2017-06-04 17:59:20 +02:00
|
|
|
$lock = $memcache->get($cachekey);
|
|
|
|
|
|
|
|
if (!is_bool($lock)) {
|
|
|
|
$pid = (int)$lock;
|
|
|
|
|
|
|
|
// When the process id isn't used anymore, we can safely claim the lock for us.
|
|
|
|
// Or we do want to lock something that was already locked by us.
|
2017-06-08 04:00:59 +02:00
|
|
|
if (!posix_kill($pid, 0) || ($pid == getmypid())) {
|
2017-06-04 17:59:20 +02:00
|
|
|
$lock = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_bool($lock)) {
|
|
|
|
$memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
|
|
|
|
$got_lock = true;
|
|
|
|
}
|
2017-06-12 17:42:54 +02:00
|
|
|
|
|
|
|
dba::unlock();
|
|
|
|
|
2017-06-08 04:00:59 +02:00
|
|
|
if (!$got_lock && ($timeout > 0)) {
|
2017-06-12 21:20:50 +02:00
|
|
|
usleep(rand(10000, 200000));
|
2017-06-04 17:59:20 +02:00
|
|
|
}
|
2017-06-08 04:00:59 +02:00
|
|
|
} while (!$got_lock && ((time() - $start) < $timeout));
|
2017-06-04 17:59:20 +02:00
|
|
|
|
|
|
|
return $got_lock;
|
|
|
|
}
|
2017-06-04 20:59:50 +02:00
|
|
|
|
2017-06-04 09:26:21 +02:00
|
|
|
do {
|
2017-06-04 14:59:29 +02:00
|
|
|
dba::lock('locks');
|
2018-01-10 14:36:02 +01:00
|
|
|
$lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $fn_name]);
|
2017-06-04 10:36:08 +02:00
|
|
|
|
2017-11-08 04:57:46 +01:00
|
|
|
if (DBM::is_result($lock)) {
|
2017-06-04 10:36:08 +02:00
|
|
|
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)) {
|
|
|
|
$lock['locked'] = false;
|
|
|
|
}
|
|
|
|
// We want to lock something that was already locked by us? So we got the lock.
|
|
|
|
if ($lock['pid'] == getmypid()) {
|
|
|
|
$got_lock = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$lock['locked']) {
|
2018-01-15 14:05:12 +01:00
|
|
|
dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $fn_name]);
|
2017-06-04 10:36:08 +02:00
|
|
|
$got_lock = true;
|
|
|
|
}
|
2017-11-08 04:57:46 +01:00
|
|
|
} elseif (!DBM::is_result($lock)) {
|
2018-01-15 14:05:12 +01:00
|
|
|
dba::insert('locks', ['name' => $fn_name, 'locked' => true, 'pid' => getmypid()]);
|
2017-06-04 09:26:21 +02:00
|
|
|
$got_lock = true;
|
|
|
|
}
|
|
|
|
|
2017-06-04 14:59:29 +02:00
|
|
|
dba::unlock();
|
2017-06-04 09:26:21 +02:00
|
|
|
|
2017-06-08 04:00:59 +02:00
|
|
|
if (!$got_lock && ($timeout > 0)) {
|
2017-06-12 21:20:50 +02:00
|
|
|
usleep(rand(100000, 2000000));
|
2017-06-04 09:26:21 +02:00
|
|
|
}
|
2017-06-08 04:00:59 +02:00
|
|
|
} while (!$got_lock && ((time() - $start) < $timeout));
|
2017-06-04 09:26:21 +02:00
|
|
|
|
|
|
|
return $got_lock;
|
|
|
|
}
|
|
|
|
|
2017-06-04 10:36:08 +02:00
|
|
|
/**
|
|
|
|
* @brief Removes a lock if it was set by us
|
|
|
|
*
|
|
|
|
* @param string $fn_name Name of the lock
|
2017-11-19 23:04:40 +01:00
|
|
|
* @return mixed
|
2017-06-04 10:36:08 +02:00
|
|
|
*/
|
2017-11-19 23:04:40 +01:00
|
|
|
public static function remove($fn_name)
|
|
|
|
{
|
2017-09-30 09:51:09 +02:00
|
|
|
if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
|
2017-06-28 22:38:22 +02:00
|
|
|
if (empty(self::$semaphore[$fn_name])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
2017-07-07 18:26:43 +02:00
|
|
|
$success = @sem_release(self::$semaphore[$fn_name]);
|
2017-07-08 11:03:41 +02:00
|
|
|
unset(self::$semaphore[$fn_name]);
|
2017-07-07 18:26:43 +02:00
|
|
|
return $success;
|
2017-06-28 22:38:22 +02:00
|
|
|
}
|
2017-06-28 06:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 19:42:47 +02:00
|
|
|
$memcache = self::connectMemcache();
|
2017-06-04 17:59:20 +02:00
|
|
|
if (is_object($memcache)) {
|
|
|
|
$cachekey = get_app()->get_hostname().";lock:".$fn_name;
|
|
|
|
$lock = $memcache->get($cachekey);
|
|
|
|
|
|
|
|
if (!is_bool($lock)) {
|
|
|
|
if ((int)$lock == getmypid()) {
|
|
|
|
$memcache->delete($cachekey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $fn_name, 'pid' => getmypid()]);
|
2017-06-04 09:26:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-04 17:59:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Removes all lock that were set by us
|
2017-11-19 23:04:40 +01:00
|
|
|
* @return void
|
2017-06-04 17:59:20 +02:00
|
|
|
*/
|
2017-11-19 23:04:40 +01:00
|
|
|
public static function removeAll()
|
|
|
|
{
|
2017-06-06 19:42:47 +02:00
|
|
|
$memcache = self::connectMemcache();
|
2017-06-04 17:59:20 +02:00
|
|
|
if (is_object($memcache)) {
|
|
|
|
// We cannot delete all cache entries, but this doesn't matter with memcache
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
|
2017-06-04 17:59:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-04 09:26:21 +02:00
|
|
|
}
|