Class: Concurrent::MutexAtomicBoolean
- Inherits:
-
Object
- Object
- Concurrent::MutexAtomicBoolean
- Defined in:
- lib/concurrent/atomic/atomic_boolean.rb
Overview
A boolean value that can be updated atomically. Reads and writes to an atomic boolean and thread-safe and guaranteed to succeed. Reads and writes may block briefly but no explicit locking is required.
Direct Known Subclasses
Instance Method Summary collapse
-
#false? ⇒ Boolean
Is the current value ‘true`?false.
-
#initialize(initial = false) ⇒ MutexAtomicBoolean
constructor
Creates a new ‘AtomicBoolean` with the given initial value.
-
#make_false ⇒ Boolean
Explicitly sets the value to false.
-
#make_true ⇒ Boolean
Explicitly sets the value to true.
-
#true? ⇒ Boolean
Is the current value ‘true`?.
-
#value ⇒ Boolean
Retrieves the current ‘Boolean` value.
-
#value=(value) ⇒ Boolean
Explicitly sets the value.
Constructor Details
#initialize(initial = false) ⇒ MutexAtomicBoolean
Creates a new ‘AtomicBoolean` with the given initial value.
18 19 20 21 |
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 18 def initialize(initial = false) @value = !!initial @mutex = Mutex.new end |
Instance Method Details
#false? ⇒ Boolean
Is the current value ‘true`?false
67 68 69 70 71 72 |
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 67 def false? @mutex.lock !@value ensure @mutex.unlock end |
#make_false ⇒ Boolean
Explicitly sets the value to false.
93 94 95 96 97 98 99 100 |
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 93 def make_false @mutex.lock old = @value @value = false old ensure @mutex.unlock end |
#make_true ⇒ Boolean
Explicitly sets the value to true.
79 80 81 82 83 84 85 86 |
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 79 def make_true @mutex.lock old = @value @value = true !old ensure @mutex.unlock end |
#true? ⇒ Boolean
Is the current value ‘true`?
55 56 57 58 59 60 |
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 55 def true? @mutex.lock @value ensure @mutex.unlock end |
#value ⇒ Boolean
Retrieves the current ‘Boolean` value.
28 29 30 31 32 33 |
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 28 def value @mutex.lock @value ensure @mutex.unlock end |
#value=(value) ⇒ Boolean
Explicitly sets the value.
42 43 44 45 46 47 48 |
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 42 def value=(value) @mutex.lock @value = !!value @value ensure @mutex.unlock end |