|
|
@ -52,6 +52,18 @@ func (lt *LockTable[T]) NewActiveLock(intention string, lockType LockType) *Acti |
|
|
return l |
|
|
return l |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func isNotFirstWaiter(lock *ActiveLock, entry *LockEntry) bool { |
|
|
|
|
|
return len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func shouldWaitForExclusiveLock(lock *ActiveLock, entry *LockEntry) bool { |
|
|
|
|
|
return !lock.isDeleted && (isNotFirstWaiter(lock, entry) || entry.activeExclusiveLockOwnerCount > 0 || entry.activeSharedLockOwnerCount > 0) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func shouldWaitForSharedLock(lock *ActiveLock, entry *LockEntry) bool { |
|
|
|
|
|
return !lock.isDeleted && (isNotFirstWaiter(lock, entry) || entry.activeExclusiveLockOwnerCount > 0) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType) (lock *ActiveLock) { |
|
|
func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType) (lock *ActiveLock) { |
|
|
lt.mu.Lock() |
|
|
lt.mu.Lock() |
|
|
// Get or create the lock entry for the key
|
|
|
// Get or create the lock entry for the key
|
|
|
@ -81,11 +93,11 @@ func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType) |
|
|
} |
|
|
} |
|
|
entry.waiters = append(entry.waiters, lock) |
|
|
entry.waiters = append(entry.waiters, lock) |
|
|
if lockType == ExclusiveLock { |
|
|
if lockType == ExclusiveLock { |
|
|
for !lock.isDeleted && ((len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID) || entry.activeExclusiveLockOwnerCount > 0 || entry.activeSharedLockOwnerCount > 0) { |
|
|
|
|
|
|
|
|
for shouldWaitForExclusiveLock(lock, entry) { |
|
|
entry.cond.Wait() |
|
|
entry.cond.Wait() |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
for !lock.isDeleted && (len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID) || entry.activeExclusiveLockOwnerCount > 0 { |
|
|
|
|
|
|
|
|
for shouldWaitForSharedLock(lock, entry) { |
|
|
entry.cond.Wait() |
|
|
entry.cond.Wait() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|