Strict Two-Phase Locking
Lock implementation
Unlocking
unlock(transaction, object)
{
entry = lock_entry(object)
if queue.empty() {
entry.lock_mode = UNLOCKED
lock_count = 0
} else {
// Grant lock to first waiter in queue
lock_request = queue.next() // (transaction, lock_mode)
entry.transaction = lock_request.transaction
entry.lock_mode = lock_request.lock_mode
entry.lock_count -= 1
unblock entry.transaction.process
// If we just granted a SHARED lock, we can also unblock
// all waiters who also want a SHARED lock. Stop when the queue
// is empty or we encounter a waiter who wants an EXCLUSIVE lock.
if entry.lock_mode is SHARED {
while not queue.empty() or queue.peek().lock_mode is SHARED {
lock_request = queue.next()
entry.transaction = lock_request.transaction
entry.lock_mode = lock_request.lock_mode
entry.lock_count -= 1
unblock entry.transaction.process
}
}
}
}
|