Class: Concurrent::MutexAtomicFixnum
- Inherits:
-
Object
- Object
- Concurrent::MutexAtomicFixnum
- Defined in:
- lib/concurrent/atomic/atomic_fixnum.rb
Overview
A numeric value that can be updated atomically. Reads and writes to an atomic fixnum and thread-safe and guaranteed to succeed. Reads and writes may block briefly but no explicit locking is required.
Testing with ruby 2.1.2
Testing with Concurrent::MutexAtomicFixnum...
3.130000 0.000000 3.130000 ( 3.136505)
Testing with Concurrent::CAtomicFixnum...
0.790000 0.000000 0.790000 ( 0.785550)
Testing with jruby 1.9.3
Testing with Concurrent::MutexAtomicFixnum...
5.460000 2.460000 7.920000 ( 3.715000)
Testing with Concurrent::JavaAtomicFixnum...
4.520000 0.030000 4.550000 ( 1.187000)
Direct Known Subclasses
Constant Summary collapse
- MIN_VALUE =
-2))
- MAX_VALUE =
(2**(0.size * 8 -2) -1)
Instance Method Summary collapse
-
#compare_and_set(expect, update) ⇒ Boolean
Atomically sets the value to the given updated value if the current value == the expected value.
-
#decrement ⇒ Fixnum
(also: #down)
Decreases the current value by 1.
-
#increment ⇒ Fixnum
(also: #up)
Increases the current value by 1.
-
#initialize(init = 0) ⇒ MutexAtomicFixnum
constructor
Creates a new ‘AtomicFixnum` with the given initial value.
-
#value ⇒ Fixnum
Retrieves the current ‘Fixnum` value.
-
#value=(value) ⇒ Fixnum
Explicitly sets the value.
Constructor Details
#initialize(init = 0) ⇒ MutexAtomicFixnum
Creates a new ‘AtomicFixnum` with the given initial value.
37 38 39 40 41 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 37 def initialize(init = 0) raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum) @value = init @mutex = Mutex.new end |
Instance Method Details
#compare_and_set(expect, update) ⇒ Boolean
Atomically sets the value to the given updated value if the current value == the expected value.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 109 def compare_and_set(expect, update) @mutex.lock if @value == expect @value = update true else false end ensure @mutex.unlock end |
#decrement ⇒ Fixnum Also known as: down
Decreases the current value by 1.
91 92 93 94 95 96 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 91 def decrement @mutex.lock @value -= 1 ensure @mutex.unlock end |
#increment ⇒ Fixnum Also known as: up
Increases the current value by 1.
77 78 79 80 81 82 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 77 def increment @mutex.lock @value += 1 ensure @mutex.unlock end |
#value ⇒ Fixnum
Retrieves the current ‘Fixnum` value.
48 49 50 51 52 53 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 48 def value @mutex.lock @value ensure @mutex.unlock end |
#value=(value) ⇒ Fixnum
Explicitly sets the value.
64 65 66 67 68 69 70 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 64 def value=(value) raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum) @mutex.lock @value = value ensure @mutex.unlock end |