Class: Ratomic::Counter
- Inherits:
-
Object
- Object
- Ratomic::Counter
- 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.
Class Method Summary collapse
-
.new ⇒ Ratomic::Counter
Create a counter initialized to zero.
Instance Method Summary collapse
-
#decrement(amt) ⇒ Integer
Decrement the counter by
amt. -
#increment(amt) ⇒ Integer
Increment the counter by
amt. -
#read ⇒ Integer
Read the current counter value.
-
#to_i ⇒ Integer
Coerce the counter to an Integer snapshot.
-
#value ⇒ Integer
Read the current counter value.
-
#zero? ⇒ Boolean
Check whether the current counter value is zero.
Class Method Details
.new ⇒ Ratomic::Counter
Create a counter initialized to zero.
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.
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.
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 |
#read ⇒ Integer
Read 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_i ⇒ Integer
Coerce the counter to an Integer snapshot.
46 47 48 |
# File 'lib/ratomic/counter.rb', line 46 def to_i read end |
#value ⇒ Integer
Read 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.
53 54 55 |
# File 'lib/ratomic/counter.rb', line 53 def zero? read.zero? end |