Class: Shoryuken::Helpers::AtomicCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/shoryuken/helpers/atomic_counter.rb

Overview

Note:

This class is optimized for scenarios with frequent atomic updates and occasional reads, such as tracking active worker counts.

A thread-safe counter implementation using Ruby’s Mutex.

This class provides atomic operations for incrementing, decrementing, and reading integer values in a thread-safe manner. It serves as a drop-in replacement for Concurrent::AtomicFixnum without requiring external dependencies.

The implementation uses a Mutex to ensure thread safety across all Ruby implementations including JRuby, where true parallelism makes atomic operations critical for data integrity.

Examples:

Basic usage

counter = Shoryuken::Helpers::AtomicCounter.new(0)
counter.increment  # => 1
counter.increment  # => 2
counter.value      # => 2
counter.decrement  # => 1

Tracking busy processors

@busy_processors = Shoryuken::Helpers::AtomicCounter.new(0)

# When starting work
@busy_processors.increment

# When work is done
@busy_processors.decrement

# Check current load
current_busy = @busy_processors.value

Direct Known Subclasses

AtomicBoolean

Instance Method Summary collapse

Constructor Details

#initialize(initial_value = 0) ⇒ AtomicCounter

Creates a new atomic counter with the specified initial value.

Examples:

Create counter starting at zero

counter = Shoryuken::Helpers::AtomicCounter.new
counter.value  # => 0

Create counter with custom initial value

counter = Shoryuken::Helpers::AtomicCounter.new(100)
counter.value  # => 100


49
50
51
52
# File 'lib/shoryuken/helpers/atomic_counter.rb', line 49

def initialize(initial_value = 0)
  @mutex = Mutex.new
  @value = initial_value
end

Instance Method Details

#decrementInteger

Atomically decrements the counter by 1 and returns the new value.

This operation is thread-safe and can be called concurrently from multiple threads without risk of data corruption or lost updates. The counter can go negative if decremented below zero.

Examples:

Decrementing the counter

counter = Shoryuken::Helpers::AtomicCounter.new(5)
counter.decrement  # => 4
counter.decrement  # => 3

Counter can go negative

counter = Shoryuken::Helpers::AtomicCounter.new(0)
counter.decrement  # => -1


99
100
101
# File 'lib/shoryuken/helpers/atomic_counter.rb', line 99

def decrement
  @mutex.synchronize { @value -= 1 }
end

#incrementInteger

Atomically increments the counter by 1 and returns the new value.

This operation is thread-safe and can be called concurrently from multiple threads without risk of data corruption or lost updates.

Examples:

Incrementing the counter

counter = Shoryuken::Helpers::AtomicCounter.new(5)
counter.increment  # => 6
counter.increment  # => 7


79
80
81
# File 'lib/shoryuken/helpers/atomic_counter.rb', line 79

def increment
  @mutex.synchronize { @value += 1 }
end

#valueInteger

Returns the current value of the counter.

This operation is thread-safe and will return a consistent value even when called concurrently with increment/decrement operations.

Examples:

Reading the current value

counter = Shoryuken::Helpers::AtomicCounter.new(42)
counter.value  # => 42


64
65
66
# File 'lib/shoryuken/helpers/atomic_counter.rb', line 64

def value
  @mutex.synchronize { @value }
end