Parent

Files

Class/Module Index [+]

Quicksearch

Concurrent::AbstractThreadLocalVar::WeakKeyMap

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

Public Class Methods

new() click to toggle source

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

Public Instance Methods

[](key) click to toggle source

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
[]=(key, value) click to toggle source

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
delete(key) click to toggle source

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
keys() click to toggle source

Get an array of keys that have not yet been garbage collected.

# File lib/concurrent/atomic/thread_local_var/weak_key_map.rb, line 218
def keys
  @values.keys.collect{|rkey| @references_to_keys_map[rkey].object}.compact
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.