3 namespace Friendica\Core\Lock;
6 use Friendica\Database\DBM;
9 * Locking driver that stores the locks in the database
11 class DatabaseLockDriver extends AbstractLockDriver
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(string $key, int $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]);
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));
57 $this->markAcquire($key);
63 * @brief Removes a lock if it was set by us
65 * @param string $key Name of the lock
69 public function releaseLock(string $key)
71 dba::delete('locks', ['locked' => false, 'pid' => 0], ['name' => $key, 'pid' => getmypid()]);
73 $this->releaseLock($key);
79 * @brief Removes all lock that were set by us
83 public function releaseAll()
85 dba::delete('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
87 $this->acquiredLocks = [];