I am trying to write a general purpose locking algorithm which allows me to lock a range of contiguous values in a sorted array. To start with, these could be exclusive locks - so two concurrent locks must be non-overlapping. What could be a good, scalable algorithm to do this? (Scalable ~ concurrent)
A specific example could be locking a range of contiguous bytes in a file for writing.
Concretely:
Let's say {a_i}
is a valid sequence, for i
= 0 through N-1
, such that:
a_j < a_k
for all j, k such that0 <= j < k < N
.lock(a_j, a_k)
locks all elements froma_j
througha_k
.- While the above lock is held, another request
lock(a_x, a_y)
will succeed only if eithera_x > a_k
, or elsea_y < a_j
.