Class: EventMachine::CircuitBreaker::RWMutex
- Inherits:
-
Object
- Object
- EventMachine::CircuitBreaker::RWMutex
- Defined in:
- lib/eventmachine/circuit_breaker/rw_mutex.rb
Instance Method Summary collapse
-
#initialize ⇒ RWMutex
constructor
A new instance of RWMutex.
- #read_lock ⇒ Object
- #write_lock ⇒ Object
Constructor Details
#initialize ⇒ RWMutex
Returns a new instance of RWMutex.
6 7 8 9 10 |
# File 'lib/eventmachine/circuit_breaker/rw_mutex.rb', line 6 def initialize @counter = 0 @counter_mutex = Mutex.new @global_mutex = Mutex.new end |
Instance Method Details
#read_lock ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/eventmachine/circuit_breaker/rw_mutex.rb', line 12 def read_lock @counter_mutex.synchronize do @counter += 1 @global_mutex.lock if @counter == 1 end yield if block_given? ensure @counter_mutex.synchronize do @counter -= 1 @global_mutex.unlock if @counter == 0 end end |
#write_lock ⇒ Object
25 26 27 28 29 |
# File 'lib/eventmachine/circuit_breaker/rw_mutex.rb', line 25 def write_lock @global_mutex.synchronize do yield if block_given? end end |