Class: EventStoreRuby::ReadWriteLockFIFO

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeReadWriteLockFIFO

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_readObject

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_writeObject

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_readObject



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_writeObject



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