Class: Ferrum::Concurrency::AwaitableMap
- Inherits:
-
Object
- Object
- Ferrum::Concurrency::AwaitableMap
- Defined in:
- lib/ferrum/concurrency/awaitable_map.rb,
sig/ferrum/concurrency/awaitable_map.rbs
Overview
A thread-safe hash that threads can wait on: #await returns the value of a key as soon as it is stored, at once if it already is, so it doesn't matter whether the waiter or the writer comes first.
A writer that only answers waiters, like a response to a command that may have nobody waiting, uses #expect on the waiting side and #fulfill on the writing side: values nobody expects are dropped.
Instance Method Summary collapse
- #[](key) ⇒ Object?
-
#[]=(key, value) ⇒ Object
Stores the value and wakes the threads waiting for the key.
-
#await(key, timeout) ⇒ Object?
Blocks until the key is stored or the timeout is reached.
-
#close ⇒ void
Wakes every waiter for good: once closed, #await doesn't block and returns what is stored.
-
#delete(key) ⇒ Object?
Removes the key and the expectation of it.
-
#expect(key) ⇒ Object
Announces that a thread is going to wait for the key, so #fulfill keeps its value.
- #expecting?(key) ⇒ Boolean
-
#fulfill(key, value) ⇒ Boolean
Stores the value and wakes the waiters, only if the key was expected.
-
#initialize ⇒ AwaitableMap
constructor
A new instance of AwaitableMap.
- #inspect ⇒ String
- #key?(key) ⇒ Boolean
-
#to_h ⇒ Hash
A copy of the stored pairs.
- #values ⇒ Array<Object>
Constructor Details
#initialize ⇒ AwaitableMap
Returns a new instance of AwaitableMap.
18 19 20 21 22 23 24 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 18 def initialize @mutex = Mutex.new @condition = ConditionVariable.new @map = {} @expected = {} @closed = false end |
Instance Method Details
#[](key) ⇒ Object?
31 32 33 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 31 def [](key) @mutex.synchronize { @map[key] } end |
#[]=(key, value) ⇒ Object
Stores the value and wakes the threads waiting for the key.
44 45 46 47 48 49 50 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 44 def []=(key, value) @mutex.synchronize do @map[key] = value @condition.broadcast end value end |
#await(key, timeout) ⇒ Object?
Blocks until the key is stored or the timeout is reached.
155 156 157 158 159 160 161 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 155 def await(key, timeout) deadline = Deadline.wrap(timeout) @mutex.synchronize do @condition.wait(@mutex, deadline.remaining) until @map.key?(key) || @closed || deadline.expired? @map[key] end end |
#close ⇒ void
This method returns an undefined value.
Wakes every waiter for good: once closed, #await doesn't block and returns what is stored.
114 115 116 117 118 119 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 114 def close @mutex.synchronize do @closed = true @condition.broadcast end end |
#delete(key) ⇒ Object?
Removes the key and the expectation of it.
102 103 104 105 106 107 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 102 def delete(key) @mutex.synchronize do @expected.delete(key) @map.delete(key) end end |
#expect(key) ⇒ Object
Announces that a thread is going to wait for the key, so #fulfill keeps its value.
60 61 62 63 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 60 def expect(key) @mutex.synchronize { @expected[key] = true } key end |
#expecting?(key) ⇒ Boolean
70 71 72 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 70 def expecting?(key) @mutex.synchronize { @expected.key?(key) } end |
#fulfill(key, value) ⇒ Boolean
Stores the value and wakes the waiters, only if the key was expected.
84 85 86 87 88 89 90 91 92 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 84 def fulfill(key, value) @mutex.synchronize do next false unless @expected.key?(key) @map[key] = value @condition.broadcast true end end |
#inspect ⇒ String
166 167 168 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 166 def inspect @mutex.synchronize { @map.inspect } end |
#key?(key) ⇒ Boolean
126 127 128 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 126 def key?(key) @mutex.synchronize { @map.key?(key) } end |
#to_h ⇒ Hash
Returns A copy of the stored pairs.
134 135 136 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 134 def to_h @mutex.synchronize { @map.dup } end |
#values ⇒ Array<Object>
141 142 143 |
# File 'lib/ferrum/concurrency/awaitable_map.rb', line 141 def values @mutex.synchronize { @map.values } end |