Module mkit::spinlock [−][src]
Module spinlock
implement read-write-spinlock, useful for
non-blocking concurrency.
Blocking concurrency can have impact on latency. When operations that require rw-exclusion is going to be quick and short, we can use non-blocking primitives like latch-and-spin.
What is Latch and spin ?
In typical multi-core processors, concurrent read operations are always safe and consistent. But it becomes unsafe, when there is a writer concurrently modifying data while readers are loading it from memory.
Latch-and-lock mechanism can be used when we want to allow one or more concurrent writer(s) along with readers.
Imagine a door leading into a room. This door has some special properties:
- The door has a latch and a lock.
- A reader can enter the room only when the door is un-locked and un-latched.
- A writer can enter the room only when the door is un-locked and, un-latched and, there are no other reader or writer in the room.
- Once the door is latched by a writer, no other writer or reader can enter the room because of (2) and (3) properties. But all readers who are already inside the room can finish their job and then exit.
- A writer can enter the room only after locking the door, which the writer can do only after all the readers have exited.
- When trying to acquire read-permission or write-permission, the caller thread shall spin until all the conditions from (1) to (5) are met. Once a thread acquires necessary permission it can continue to finish its job and then release the permission.
Structs
ReadGuard | Type to handle read-latch, when latchlock gets dropped the latch is released. |
Spinlock | Spinlock implements latch-and-spin mechanism for non-blocking concurrency. |
Stats | Statistic type, to capture Spinlock metrics. |
WriteGuard | Type to handle write-latch, when latchlock gets dropped the latch is released. |