Class: Farce::Local::Counter

Inherits:
Numeric
  • Object
show all
Includes:
Abstract::Counter, Shareable::Tracked
Defined in:
lib/farce/local/counter.rb

Overview

A shareable atomic counter with an independent value in each scope. Each scope starts at the initial integer. Reset affects only the current scope.

Instance Attribute Summary collapse

Attributes included from Scoped

#scope

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods included from Abstract::Counter

#add, #decrement_if_above, included, #increment_if_below, #reset, #wait_until, #wait_until_above, #wait_until_below, #wait_until_changed, #wait_until_match, #wait_until_value, #wait_while, #wait_while_above, #wait_while_below, #wait_while_match, #wait_while_value

Methods included from Internal::ValueSerialization

#as_extended_json, #as_json, #to_bson, #to_bson_normalized_value, #to_cbor, #to_json, #to_msgpack

Methods included from Abstract::Value

#blank?, #unwrap

Methods included from Internal::Copyable

#duplicable?

Constructor Details

#initialize(value = 0, scope: :ractor) ⇒ Counter

Valid scopes are:

  • :ractor - The value is shared by all threads within the same Ractor.
  • :thread_group - The value is shared by all threads within the same thread group.
  • :thread - The value is shared by all fibers within the same thread.
  • :fiber_storage - The value is shared by all fibers using the same storage. Unless explicitly specified, storage is inherited from the parent fiber for blocking fibers, but not for non-blocking fibers (such as those created by Scheduler#schedule).
  • :fiber - Each fiber has its own independent value.

Parameters:

  • value (Numeric, String, #to_int) (defaults to: 0) —

    the initial value, converted to an Integer

  • scope (Symbol) (defaults to: :ractor) —

    the scope of the counter

Raises:

  • (FrozenError)


21
22
23
24
25
# File 'lib/farce/local/counter.rb', line 21

def initialize(value = 0, scope: :ractor)
  raise FrozenError.new("can't initialize a frozen counter", receiver: self) if frozen?
  @initial = Integer(value)
  super(@initial, scope:)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Farce::Abstract::Counter

Instance Attribute Details

#initial ⇒ Integer (readonly)

The initial value used for new scopes and by Abstract::Counter#reset.

Returns:

  • (Integer)


16
17
18
# File 'lib/farce/local/counter.rb', line 16

def initial
  @initial
end

Instance Method Details

#compare_and_set(expected, replacement) ⇒ Boolean

Replace the value atomically if it matches the expected integer.

Parameters:

  • expected (Integer) —

    the expected value

  • replacement (Integer) —

    the replacement value

Returns:

  • (Boolean) —

    whether the value changed



49
# File 'lib/farce/local/counter.rb', line 49

def compare_and_set(expected, replacement) = internal_counter.compare_and_set(expected, replacement)

#decrement(by = 1) ⇒ self Also known as: subtract, remove

Decrement the counter atomically.

Parameters:

  • by (Numeric, String, #to_int) (defaults to: 1) —

    the amount, converted to an Integer

Returns:

  • (self)


62
63
64
65
# File 'lib/farce/local/counter.rb', line 62

def decrement(by = 1)
  internal_counter.decrement(by)
  self
end

#increment(by = 1) ⇒ self

Increment the counter atomically.

Parameters:

  • by (Numeric, String, #to_int) (defaults to: 1) —

    the amount, converted to an Integer

Returns:

  • (self)


54
55
56
57
# File 'lib/farce/local/counter.rb', line 54

def increment(by = 1)
  internal_counter.increment(by)
  self
end

#store(value) ⇒ Integer Also known as: value=

Store an integer value.

Parameters:

  • value (Integer) —

    the replacement value

Returns:

  • (Integer) —

    the stored value



36
# File 'lib/farce/local/counter.rb', line 36

def store(value) = internal_counter.store(value)

#swap(value) ⇒ Integer

Replace the value and return the previous integer.

Parameters:

  • value (Integer) —

    the replacement value

Returns:

  • (Integer)


43
# File 'lib/farce/local/counter.rb', line 43

def swap(value) = internal_counter.swap(value)

#value ⇒ Integer Also known as: get

Return the current integer value.

Returns:

  • (Integer)


29
# File 'lib/farce/local/counter.rb', line 29

def value = internal_counter.value