Class: Ratomic::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/ratomic/counter.rb

Overview

A Ractor-shareable atomic counter.

Counter stores an unsigned integer in native Rust atomics and can be shared safely across Ractors.

Examples:

Count work across Ractors

counter = Ratomic::Counter.new
counter.increment(1) # => 1
counter.read # => 1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newRatomic::Counter

Create a counter initialized to zero.

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ratomic/counter.rb', line 35

class Counter
  # Read the current counter value.
  #
  # @return [Integer] the current counter value
  def value
    read
  end

  # Coerce the counter to an Integer snapshot.
  #
  # @return [Integer] the current counter value
  def to_i
    read
  end

  # Check whether the current counter value is zero.
  #
  # @return [Boolean] true when the counter currently reads zero
  def zero?
    read.zero?
  end
end

Instance Method Details

#decrement(amt) ⇒ Integer

Decrement the counter by amt.

Parameters:

  • amt (Integer)

    amount to subtract from the counter

Returns:

  • (Integer)

    the updated counter value



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ratomic/counter.rb', line 35

class Counter
  # Read the current counter value.
  #
  # @return [Integer] the current counter value
  def value
    read
  end

  # Coerce the counter to an Integer snapshot.
  #
  # @return [Integer] the current counter value
  def to_i
    read
  end

  # Check whether the current counter value is zero.
  #
  # @return [Boolean] true when the counter currently reads zero
  def zero?
    read.zero?
  end
end

#increment(amt) ⇒ Integer

Increment the counter by amt.

Parameters:

  • amt (Integer)

    amount to add to the counter

Returns:

  • (Integer)

    the updated counter value



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ratomic/counter.rb', line 35

class Counter
  # Read the current counter value.
  #
  # @return [Integer] the current counter value
  def value
    read
  end

  # Coerce the counter to an Integer snapshot.
  #
  # @return [Integer] the current counter value
  def to_i
    read
  end

  # Check whether the current counter value is zero.
  #
  # @return [Boolean] true when the counter currently reads zero
  def zero?
    read.zero?
  end
end

#readInteger

Read the current counter value.

Returns:

  • (Integer)

    the current counter value



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ratomic/counter.rb', line 35

class Counter
  # Read the current counter value.
  #
  # @return [Integer] the current counter value
  def value
    read
  end

  # Coerce the counter to an Integer snapshot.
  #
  # @return [Integer] the current counter value
  def to_i
    read
  end

  # Check whether the current counter value is zero.
  #
  # @return [Boolean] true when the counter currently reads zero
  def zero?
    read.zero?
  end
end

#to_iInteger

Coerce the counter to an Integer snapshot.

Returns:

  • (Integer)

    the current counter value



46
47
48
# File 'lib/ratomic/counter.rb', line 46

def to_i
  read
end

#valueInteger

Read the current counter value.

Returns:

  • (Integer)

    the current counter value



39
40
41
# File 'lib/ratomic/counter.rb', line 39

def value
  read
end

#zero?Boolean

Check whether the current counter value is zero.

Returns:

  • (Boolean)

    true when the counter currently reads zero



53
54
55
# File 'lib/ratomic/counter.rb', line 53

def zero?
  read.zero?
end