Module: ThreadAttrAccessor

Defined in:
lib/thread_attr_accessor.rb,
lib/thread_attr_accessor/version.rb

Overview

extend this module on your class/module to get per-thread class attribute accessors. Example:

class MyClass

extend ThreadAttrAccessor

thread_attr_accessor :setting

end

MyClass.setting = :original

threads = [

Thread.new { MyClass.setting = :foo; puts MyClass.setting },
Thread.new { MyClass.setting = :bar; puts MyClass.setting },

]

threads.each(&:join) MyClass.setting == :original # true

Defined Under Namespace

Classes: FiberStorage, ThreadStorage

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/thread_attr_accessor.rb', line 102

def self.extended(base)
  mod = Module.new

  unless base.const_defined?(:ThreadAttributeAccessors, false)
    base.const_set(:ThreadAttributeAccessors, mod)
    base.extend(mod)
  end
end

.search_in_ancestor_threads(key) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/thread_attr_accessor.rb', line 75

def self.search_in_ancestor_threads(key)
  fiber  = Fiber.current
  thread = Thread.current

  until fiber.nil?
    storage = FiberStorage.new(fiber, thread)

    if storage.has_key?(key)
      return storage[key]
    else
      fiber = fiber.parent_fiber
    end
  end

  until thread.nil?
    storage = ThreadStorage.new(thread)

    if storage.has_key?(key)
      return storage[key]
    else
      thread = thread.parent_thread
    end
  end

  nil
end

.thread_accessor_key(base, name) ⇒ Object



27
28
29
# File 'lib/thread_attr_accessor.rb', line 27

def self.thread_accessor_key(base, name)
  "#{base.name}.#{name}"
end

Instance Method Details

#thread_attr_accessor(*names, private: false, **opts) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/thread_attr_accessor.rb', line 166

def thread_attr_accessor(*names, private: false, **opts)
  private_reader = private.to_s == "reader" || private == true
  private_writer = private.to_s == "writer" || private == true

  thread_attr_reader(*names, private: private_reader, **opts)
  thread_attr_writer(*names, private: private_writer, **opts)
end

#thread_attr_reader(*names, default: nil, inherit: false, private: false, **opts) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/thread_attr_accessor.rb', line 128

def thread_attr_reader(*names, default: nil, inherit: false, private: false, **opts)
  if default && inherit
    get_default = ->(thread_key) {
      ThreadAttrAccessor.search_in_ancestor_threads(thread_key) ||
      default.call
    }
  elsif inherit
    get_default = ThreadAttrAccessor.method(:search_in_ancestor_threads)
  elsif default
    get_default = ->(*) { default.call }
  end

  if get_default
    get_value = ->(thread_key) {
      storage = FiberStorage.new
      storage[thread_key] ||= get_default.call(thread_key)
    }
  else
    get_value = ->(thread_key) {
      FiberStorage.new[thread_key]
    }
  end

  mod = const_get(:ThreadAttributeAccessors)

  names.each do |name|
    thread_key = ThreadAttrAccessor.thread_accessor_key(self, name)

    mod.send(:define_method, name) do
      get_value.call(thread_key)
    end

    if private
      mod.send :private, name
    end
  end
end

#thread_attr_writer(*names, private: false, **opts) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/thread_attr_accessor.rb', line 111

def thread_attr_writer(*names, private: false, **opts)
  mod = const_get(:ThreadAttributeAccessors)

  names.each do |name|
    thread_key = ThreadAttrAccessor.thread_accessor_key(self, name)

    mod.send(:define_method, "#{name}=") do |value|
      FiberStorage.new[thread_key] = value
      value
    end

    if private
      mod.send :private, "#{name}="
    end
  end
end