Class: EventMachine::CircuitBreaker::RWMutex

Inherits:
Object
  • Object
show all
Defined in:
lib/eventmachine/circuit_breaker/rw_mutex.rb

Instance Method Summary collapse

Constructor Details

#initializeRWMutex

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_lockObject



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_lockObject



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