Class: Farce::Local::Counter
- Inherits:
-
Numeric
- Object
- Numeric
- Farce::Local::Counter
- 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
-
#initial ⇒ Integer
readonly
The initial value used for new scopes and by Abstract::Counter#reset.
Attributes included from Scoped
Instance Method Summary collapse
-
#compare_and_set(expected, replacement) ⇒ Boolean
Replace the value atomically if it matches the expected integer.
-
#decrement(by = 1) ⇒ self
(also: #subtract, #remove)
Decrement the counter atomically.
-
#increment(by = 1) ⇒ self
Increment the counter atomically.
-
#initialize(value = 0, scope: :ractor) ⇒ Counter
constructor
Valid scopes are: *
:ractor- The value is shared by all threads within the same Ractor. -
#store(value) ⇒ Integer
(also: #value=)
Store an integer value.
-
#swap(value) ⇒ Integer
Replace the value and return the previous integer.
-
#value ⇒ Integer
(also: #get)
Return the current integer value.
Methods included from 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
Methods included from Internal::Copyable
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.
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.
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.
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.
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.
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.
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.
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.
29 |
# File 'lib/farce/local/counter.rb', line 29 def value = internal_counter.value |