Sha256: 4199d6fad252d5872e4c7fd5544e6d459123b6954cafbdfbc5f020b50128fec4

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

module Tins
  module ThreadLocal
    @@cleanup = lambda do |my_object_id|
      my_id = "__thread_local_#{my_object_id}__"
      for t in Thread.list
        t[my_id] = nil if t[my_id]
      end
    end

    # Define a thread local variable named _name_ in this module/class. If the
    # value _value_ is given, it is used to initialize the variable.
    def thread_local(name, default_value = nil, &default)
      is_a?(Module) or raise TypeError, "receiver has to be a Module"

      default_value && default and raise ArgumentError,
        "require either default_falue or default block"

      if default_value
        default = -> * { default_value }
      end

      name = name.to_s
      my_id = "__thread_local_#{__id__}__"

      ObjectSpace.define_finalizer(self, @@cleanup)

      define_method(name) do
        values = Thread.current[my_id] ||= {}
        if default && !values.key?(name)
          values[name] = default.call
        end
        values[name]
      end

      define_method("#{name}=") do |value|
        Thread.current[my_id] ||= {}
        Thread.current[my_id][name] = value
      end

      self
    end

    # Define a thread local variable for the current instance with name _name_.
    # If the value _value_ is given, it is used to initialize the variable.
    def instance_thread_local(name, default_value = nil, &default)
      class << self
        extend Tins::ThreadLocal
        self
      end.thread_local name, default_value, &default
      self
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tins-1.37.0 lib/tins/thread_local.rb
tins-1.36.1 lib/tins/thread_local.rb
tins-1.36.0 lib/tins/thread_local.rb
tins-1.35.0 lib/tins/thread_local.rb
tins-1.34.0 lib/tins/thread_local.rb
tins-1.33.0 lib/tins/thread_local.rb
tins-1.32.1 lib/tins/thread_local.rb