Sha256: 67c1f630157fdecb112d5f8c15a009c8c59175c35b6ea32b54702df7718de2dd

Contents?: true

Size: 814 Bytes

Versions: 23

Compression:

Stored size: 814 Bytes

Contents

module Restly::ThreadLocal

  private

  def thread_local_accessor(*names)
    names.each do |name|
      class_variable_set :"@@#{name}", {}

      define_thread_getter(name)
      define_thread_setter(name)

    end
  end

  def define_thread_getter(name)
    accessor = class_variable_get("@@#{name}")
    define_singleton_method name do
      thread_id = Thread.current.object_id
      accessor[thread_id]
    end
  end

  def define_thread_setter(name)
    accessor = class_variable_get("@@#{name}")
    define_singleton_method "#{name}=" do |val|
      thread_id = Thread.current.object_id
      finalizer = ->(id){ class_variable_get("@@#{name}").delete(id) }
      ObjectSpace.define_finalizer Thread.current, finalizer unless accessor.has_key? thread_id
      accessor[thread_id] = val
    end
  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
restly-0.0.1.beta.10 lib/restly/thread_local.rb
restly-0.0.1.beta.9 lib/restly/thread_local.rb
restly-0.0.1.beta.6 lib/restly/thread_local.rb
restly-0.0.1.beta.5 lib/restly/thread_local.rb
restly-0.0.1.beta.4 lib/restly/thread_local.rb
restly-0.0.1.beta.3 lib/restly/thread_local.rb
restly-0.0.1.beta.2 lib/restly/thread_local.rb
restly-0.0.1.beta.1 lib/restly/thread_local.rb
restly-0.0.1.alpha.22 lib/restly/thread_local.rb
restly-0.0.1.alpha.19 lib/restly/thread_local.rb
restly-0.0.1.alpha.18 lib/restly/thread_local.rb
restly-0.0.1.alpha.16 lib/restly/thread_local.rb
restly-0.0.1.alpha.12 lib/restly/thread_local.rb
restly-0.0.1.alpha.11 lib/restly/thread_local.rb
restly-0.0.1.alpha.10 lib/restly/thread_local.rb
restly-0.0.1.alpha.9 lib/restly/thread_local.rb
restly-0.0.1.alpha.8 lib/restly/thread_local.rb
restly-0.0.1.alpha.7 lib/restly/thread_local.rb
restly-0.0.1.alpha.6 lib/restly/thread_local.rb
restly-0.0.1.alpha.4 lib/restly/thread_local.rb