Class: SmartCore::Engine::ReadWriteLock
- Inherits:
-
Object
- Object
- SmartCore::Engine::ReadWriteLock
- Defined in:
- lib/smart_core/engine/read_write_lock.rb
Overview
Instance Method Summary collapse
- #initialize ⇒ void constructor
- #read_sync(&block) ⇒ Any
- #write_owned? ⇒ Boolean
- #write_sync(&block) ⇒ Any
Constructor Details
#initialize ⇒ void
11 12 13 14 15 16 17 |
# File 'lib/smart_core/engine/read_write_lock.rb', line 11 def initialize # NOTE: # ivars has no readers cuz we want to avoid # Ruby VM's context-switching during reade-method invokation. @active_reader = false @write_lock = ::Mutex.new end |
Instance Method Details
#read_sync(&block) ⇒ Any
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/smart_core/engine/read_write_lock.rb', line 25 def read_sync(&block) @active_reader = true if @write_lock.locked? && @write_lock.owned? yield else while @write_lock.locked? do; end yield end ensure @active_reader = false end |
#write_owned? ⇒ Boolean
41 42 43 |
# File 'lib/smart_core/engine/read_write_lock.rb', line 41 def write_owned? @write_lock.owned? end |
#write_sync(&block) ⇒ Any
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/smart_core/engine/read_write_lock.rb', line 51 def write_sync(&block) if @write_lock.owned? yield else while @active_reader do; end @write_lock.synchronize do @active_reader = true begin yield ensure @active_reader = false end end end end |