Class: Cond::CondPrivate::ThreadLocal
- Inherits:
-
Object
- Object
- Cond::CondPrivate::ThreadLocal
- Includes:
- SymbolGenerator
- Defined in:
- lib/cond/cond_private/thread_local.rb
Overview
Thread-local variable.
Class Method Summary collapse
- .accessor_module(name, subclass = self, &block) ⇒ Object
- .reader_module(name, subclass = self, &block) ⇒ Object
Instance Method Summary collapse
-
#clear(&default) ⇒ Object
Reset to just-initialized state for all threads.
-
#initialize(&default) ⇒ ThreadLocal
constructor
If
value
is called beforevalue=
then the result of &default is used. - #value ⇒ Object
- #value=(value) ⇒ Object
Methods included from SymbolGenerator
Constructor Details
#initialize(&default) ⇒ ThreadLocal
If value
is called before value=
then the result of &default is used.
&default normally creates a new object, otherwise the returned object will be shared across threads.
20 21 22 23 24 25 |
# File 'lib/cond/cond_private/thread_local.rb', line 20 def initialize(&default) @name = gensym @accessed = gensym @default = default SymbolGenerator.track(self, [@name, @accessed]) end |
Class Method Details
.accessor_module(name, subclass = self, &block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cond/cond_private/thread_local.rb', line 56 def accessor_module(name, subclass = self, &block) var = subclass.new(&block) Module.new { define_method(name) { var.value } define_method("#{name}=") { |value| var.value = value } } end |
.reader_module(name, subclass = self, &block) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/cond/cond_private/thread_local.rb', line 68 def reader_module(name, subclass = self, &block) accessor_module(name, subclass, &block).instance_eval { remove_method "#{name}=" self } end |
Instance Method Details
#clear(&default) ⇒ Object
Reset to just-initialized state for all threads.
30 31 32 33 34 35 36 37 38 |
# File 'lib/cond/cond_private/thread_local.rb', line 30 def clear(&default) Thread.exclusive { @default = default Thread.list.each { |thread| thread[@accessed] = nil thread[@name] = nil } } end |
#value ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/cond/cond_private/thread_local.rb', line 40 def value unless Thread.current[@accessed] if @default Thread.current[@name] = @default.call end Thread.current[@accessed] = true end Thread.current[@name] end |
#value=(value) ⇒ Object
50 51 52 53 |
# File 'lib/cond/cond_private/thread_local.rb', line 50 def value=(value) Thread.current[@accessed] = true Thread.current[@name] = value end |