3 namespace Friendica\Util\Lock;
6 use Friendica\Database\DBM;
9 * Locking driver that stores the locks in the database
11 class DatabaseLockDriver implements ILockDriver
14 * @brief Sets a lock for a given name
16 * @param string $key The Name of the lock
17 * @param integer $timeout Seconds until we give up
19 * @return boolean Was the lock successful?
21 public function acquireLock($key, $timeout = 120)
28 $lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $key]);
30 if (DBM::is_result($lock)) {
31 if ($lock['locked']) {
32 // When the process id isn't used anymore, we can safely claim the lock for us.
33 if (!posix_kill($lock['pid'], 0)) {
34 $lock['locked'] = false;
36 // We want to lock something that was already locked by us? So we got the lock.
37 if ($lock['pid'] == getmypid()) {
41 if (!$lock['locked']) {
42 dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $key]);
45 } elseif (!DBM::is_result($lock)) {
46 dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid()]);
52 if (!$got_lock && ($timeout > 0)) {
53 usleep(rand(100000, 2000000));
55 } while (!$got_lock && ((time() - $start) < $timeout));
61 * @brief Removes a lock if it was set by us
63 * @param string $key Name of the lock
67 public function releaseLock($key)
69 dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $key, 'pid' => getmypid()]);
75 * @brief Removes all lock that were set by us
79 public function releaseAll()
81 dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);