Class: Toaster::BlockingMap
- Inherits:
-
Object
- Object
- Toaster::BlockingMap
- Defined in:
- lib/toaster/util/blocking_map.rb
Overview
Simple implementation of a map with thread-safe blocking operations:
-
put(key, value): put a value to the map
-
get(key): get a value from the map, do not remove value
-
take(key): get a value from the map, remove value afterwards
Author: Waldemar Hummer ([email protected])
Instance Method Summary collapse
-
#get(key) ⇒ Object
Get a value from the map.
-
#initialize ⇒ BlockingMap
constructor
A new instance of BlockingMap.
-
#put(key, value) ⇒ Object
Put a value to the map, notifying waiting threads of new value.
-
#take(key) ⇒ Object
Get a value from the map.
Constructor Details
#initialize ⇒ BlockingMap
Returns a new instance of BlockingMap.
15 16 17 18 19 |
# File 'lib/toaster/util/blocking_map.rb', line 15 def initialize() @hash = {} @semaphore = Mutex.new @signal = ConditionVariable.new end |
Instance Method Details
#get(key) ⇒ Object
Get a value from the map. Blocks until a value with the given key becomes available.
34 35 36 |
# File 'lib/toaster/util/blocking_map.rb', line 34 def get(key) return do_get(key, false) end |
#put(key, value) ⇒ Object
Put a value to the map, notifying waiting threads of new value
24 25 26 27 28 29 |
# File 'lib/toaster/util/blocking_map.rb', line 24 def put(key, value) @semaphore.synchronize do @hash[key] = value @signal.signal end end |
#take(key) ⇒ Object
Get a value from the map. Blocks until a value with the given key becomes available. This operation emoves the value from the map.
42 43 44 |
# File 'lib/toaster/util/blocking_map.rb', line 42 def take(key) return do_get(key, true) end |