Class: Subserver::Listener::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/subserver/listener.rb

Overview

Ruby doesn’t provide atomic counters out of the box so we’ll implement something simple ourselves. bugs.ruby-lang.org/issues/14706

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



125
126
127
128
# File 'lib/subserver/listener.rb', line 125

def initialize
  @value = 0
  @lock = Mutex.new
end

Instance Method Details

#incr(amount = 1) ⇒ Object



130
131
132
# File 'lib/subserver/listener.rb', line 130

def incr(amount=1)
  @lock.synchronize { @value = @value + amount }
end

#resetObject



134
135
136
# File 'lib/subserver/listener.rb', line 134

def reset
  @lock.synchronize { val = @value; @value = 0; val }
end