Next: , Previous: Indexes, Up: Internals


4.11 Locking

This library uses atomic bit-wise operations on shared memory segment as an internal implementation of locking. Low level functions were infamously stolen from Linux kernel source, thus locking should be even SMP safe. In adition to the lock scopes ( file, record ) and types ( read, write ) provided by the library interface, one ( block ) scope and a few types ( advisory read, advisory write, update, extend and again ) are used internally. Live locks in the shared memory are organized as an ALV tree of file locks containing trees of block locks containing trees of record locks. Pointers used in those trees are not relative. During a lock claiming advisory the lock(s) is/(are) made at upper level(s) e.g. if you need a “record write-lock”, the file and block where the record lives are locked advisory-write.

Example of a successfull locking sequence

  1. write lock with scope record needed in file A
  2. an item corresponding to the file A exists in a file locking tree, locked as “advisory write”, nothing to be done here, proceed .
  3. item corresponding to the block containing record doesn't exist in the file A's block tree
  4. update lock wasn't set at the file A node at upper level so it's now update locked for us
  5. allocate the block node and insert it in the file A's block tree
  6. release update lock on the file A's node
  7. node corresponding to the record doesn't exist in block's tree
  8. update lock wasn't set at block's node at upper level so it's now update locked for us
  9. allocate the record's node and insert it in the block's tree
  10. set advisory write lock on the block's node
  11. release update lock on the block's node
  12. write lock wasn't set on the record's node, so it's now locked . uff

When lock can't be obtained (locked by someone else) and if this is first unsucessfull attempt on this lock (nobody is waiting for this lock) library grabs first available semaphore and modifies lock held by other process and sets semaphore up. In all cases library waits until semaphore become zero. This can be only done by the process holding lock.