Class: EventStoreRuby::ReadWriteLockFIFO
- Inherits:
-
Object
- Object
- EventStoreRuby::ReadWriteLockFIFO
- Defined in:
- lib/eventstore_ruby/read_write_lock_fifo.rb
Overview
A deterministic FIFO read-write lock mirroring the original TypeScript logic but using explicit ticket numbers so Ruby thread scheduling cannot reorder requests. Readers queued before a writer are granted together; writers get exclusive access in the exact order they arrived.
Defined Under Namespace
Classes: Entry
Instance Method Summary collapse
-
#acquire_read ⇒ Object
Acquire a read lock (blocks until granted).
-
#acquire_write ⇒ Object
Acquire a write lock (blocks until granted).
-
#initialize ⇒ ReadWriteLockFIFO
constructor
A new instance of ReadWriteLockFIFO.
- #release_read ⇒ Object
- #release_write ⇒ Object
Constructor Details
#initialize ⇒ ReadWriteLockFIFO
Returns a new instance of ReadWriteLockFIFO.
11 12 13 14 15 16 17 |
# File 'lib/eventstore_ruby/read_write_lock_fifo.rb', line 11 def initialize @read_count = 0 @write_locked = false @queue = [] @next_ticket = 0 @mutex = Mutex.new end |
Instance Method Details
#acquire_read ⇒ Object
Acquire a read lock (blocks until granted)
20 21 22 |
# File 'lib/eventstore_ruby/read_write_lock_fifo.rb', line 20 def acquire_read wait_for_grant(create_entry(:read)) end |
#acquire_write ⇒ Object
Acquire a write lock (blocks until granted)
25 26 27 |
# File 'lib/eventstore_ruby/read_write_lock_fifo.rb', line 25 def acquire_write wait_for_grant(create_entry(:write)) end |
#release_read ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/eventstore_ruby/read_write_lock_fifo.rb', line 29 def release_read @mutex.synchronize do raise 'No read locks to release' if @read_count.zero? @read_count -= 1 process_queue if @read_count.zero? end end |
#release_write ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/eventstore_ruby/read_write_lock_fifo.rb', line 37 def release_write @mutex.synchronize do raise 'No write lock to release' unless @write_locked @write_locked = false process_queue end end |