Class: Garcon::MutexAtomicBoolean
- Defined in:
- lib/garcon/task/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 ‘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.
34 35 36 37 |
# File 'lib/garcon/task/atomic_boolean.rb', line 34 def initialize(initial = false) @value = !!initial @mutex = Mutex.new end |
Instance Method Details
#false? ⇒ Boolean
Is the current value ‘false`
88 89 90 91 92 93 |
# File 'lib/garcon/task/atomic_boolean.rb', line 88 def false? @mutex.lock !@value ensure @mutex.unlock end |
#make_false ⇒ Boolean
Explicitly sets the value to false.
116 117 118 119 120 121 122 123 |
# File 'lib/garcon/task/atomic_boolean.rb', line 116 def make_false @mutex.lock old = @value @value = false old ensure @mutex.unlock end |
#make_true ⇒ Boolean
Explicitly sets the value to true.
101 102 103 104 105 106 107 108 |
# File 'lib/garcon/task/atomic_boolean.rb', line 101 def make_true @mutex.lock old = @value @value = true !old ensure @mutex.unlock end |
#true? ⇒ Boolean
Is the current value ‘true`
75 76 77 78 79 80 |
# File 'lib/garcon/task/atomic_boolean.rb', line 75 def true? @mutex.lock @value ensure @mutex.unlock end |
#value ⇒ Boolean
Retrieves the current ‘Boolean` value.
45 46 47 48 49 50 |
# File 'lib/garcon/task/atomic_boolean.rb', line 45 def value @mutex.lock @value ensure @mutex.unlock end |
#value=(value) ⇒ Boolean
Explicitly sets the value.
61 62 63 64 65 66 67 |
# File 'lib/garcon/task/atomic_boolean.rb', line 61 def value=(value) @mutex.lock @value = !!value @value ensure @mutex.unlock end |