Class: MutexCounter
- Inherits:
-
Object
- Object
- MutexCounter
- Defined in:
- lib/distributed_logreader/distributer/mutex_counter.rb
Constant Summary collapse
- MAX =
(2 ** 30) - 1
Instance Method Summary collapse
-
#decr ⇒ Object
(also: #pred, #prev)
decr only to zero.
- #inc ⇒ Object (also: #next, #succ)
-
#initialize(max = MAX) ⇒ MutexCounter
constructor
A new instance of MutexCounter.
- #real_total ⇒ Object (also: #to_i)
- #value ⇒ Object
Constructor Details
#initialize(max = MAX) ⇒ MutexCounter
Returns a new instance of MutexCounter.
3 4 5 6 7 8 |
# File 'lib/distributed_logreader/distributer/mutex_counter.rb', line 3 def initialize(max = MAX) @mutex = Mutex.new @counter = 0 @resets = 0 @max = max end |
Instance Method Details
#decr ⇒ Object Also known as: pred, prev
decr only to zero
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/distributed_logreader/distributer/mutex_counter.rb', line 32 def decr @mutex.synchronize do if @counter > 0 @counter -= 1 else if @resets > 1 @resets -= 1 @counter = @max end end @counter end end |
#inc ⇒ Object Also known as: next, succ
19 20 21 22 23 24 25 26 27 |
# File 'lib/distributed_logreader/distributer/mutex_counter.rb', line 19 def inc @mutex.synchronize do if @counter >= @max @counter = 0 # to avoid Bignum, it's about 4x slower @resets += 1 end @counter += 1 end end |
#real_total ⇒ Object Also known as: to_i
10 11 12 |
# File 'lib/distributed_logreader/distributer/mutex_counter.rb', line 10 def real_total @mutex.synchronize { (@resets * @max) + @counter } end |
#value ⇒ Object
15 16 17 |
# File 'lib/distributed_logreader/distributer/mutex_counter.rb', line 15 def value @mutex.synchronize { @counter } end |