Sha256: eb5109963f3a6717355abe1e121fe3361bea45e82979a5db4e4fde8b5c53950e

Contents?: true

Size: 1.55 KB

Versions: 51

Compression:

Stored size: 1.55 KB

Contents

module Tins
  module ThreadLocal
    @@mutex = Mutex.new

    @@cleanup = lambda do |my_object_id|
      my_id = "__thread_local_#{my_object_id}__"
      @@mutex.synchronize do
        for t in Thread.list
          t[my_id] = nil if t[my_id]
        end
      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

51 entries across 41 versions & 2 rubygems

Version Path
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/thread_local.rb
tins-1.32.0 lib/tins/thread_local.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/thread_local.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/thread_local.rb
tins-1.31.1 lib/tins/thread_local.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/tins-1.31.0/lib/tins/thread_local.rb
tins-1.31.0 lib/tins/thread_local.rb
tins-1.30.0 lib/tins/thread_local.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/thread_local.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/thread_local.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/thread_local.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/thread_local.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tins-1.28.0/lib/tins/thread_local.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/tins-1.28.0/lib/tins/thread_local.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tins-1.26.0/lib/tins/thread_local.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/thread_local.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/tins-1.26.0/lib/tins/thread_local.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/thread_local.rb
tins-1.29.1 lib/tins/thread_local.rb
tins-1.29.0 lib/tins/thread_local.rb