The classes behave similar to Hashes, but the keys in the map are not strong references and can be reclaimed by the garbage collector at any time. When a key is reclaimed, the map entry will be removed.
@!visibility private
Create a new map. Values added to the hash will be cleaned up by the garbage collector if there are no other reference except in the map.
# File lib/concurrent/atomic/thread_local_var/weak_key_map.rb, line 179 def initialize @values = {} @references_to_keys_map = {} @lock = Monitor.new @reference_cleanup = lambda{|object_id| remove_reference_to(object_id)} end
Get a value from the map by key. If the value has been reclaimed by the garbage collector, this will return nil.
# File lib/concurrent/atomic/thread_local_var/weak_key_map.rb, line 188 def [](key) @lock.synchronize do rkey = ref_key(key) @values[rkey] if rkey end end
Add a key/value to the map.
# File lib/concurrent/atomic/thread_local_var/weak_key_map.rb, line 196 def []=(key, value) ObjectSpace.define_finalizer(key, @reference_cleanup) @lock.synchronize do @references_to_keys_map[key.__id__] = WeakReference.new(key) @values[key.__id__] = value end end
Remove the value associated with the key from the map.
# File lib/concurrent/atomic/thread_local_var/weak_key_map.rb, line 205 def delete(key) @lock.synchronize do rkey = ref_key(key) if rkey @references_to_keys_map.delete(rkey) @values.delete(rkey) else nil end end end
Generated with the Darkfish Rdoc Generator 2.