/** * Release a lock that was previously obtained with @lock. * @param lock $lock - a lock obtained from this factory. * @return boolean - true if the lock is no longer held (including if it was never held). */ public function release_lock(lock $lock) { $params = array('locktype' => $this->dblockid, 'token' => $lock->get_key()); $result = $this->db->get_record_sql('SELECT pg_advisory_unlock(:locktype, :token) AS unlocked', $params); $result = $result->unlocked === 't'; if ($result) { unset($this->openlocks[$lock->get_key()]); } return $result; }
/** * Extend a lock that was previously obtained with @lock. * @param lock $lock - a lock obtained from this factory. * @param int $maxlifetime - the new lifetime for the lock (in seconds). * @return boolean - true if the lock was extended. */ public function extend_lock(lock $lock, $maxlifetime = 86400) { $now = time(); $expires = $now + $maxlifetime; $params = array('expires' => $expires, 'token' => $lock->get_key()); $sql = 'UPDATE {lock_db} SET expires = :expires, WHERE owner = :token'; $this->db->execute($sql, $params); $countparams = array('owner' => $lock->get_key()); $result = $this->count_records('lock_db', $countparams); return $result === 0; }
/** * Release a lock that was previously obtained with @lock. * @param lock $lock - A lock obtained from this factory. * @return boolean - true if the lock is no longer held (including if it was never held). */ public function release_lock(lock $lock) { $handle = $lock->get_key(); if (!$handle) { // We didn't have a lock. return false; } $result = flock($handle, LOCK_UN); fclose($handle); return $result; }