Module: DSLKit::ThreadGlobal
- Included in:
- Object
- Defined in:
- lib/dslkit/polite.rb
Instance Method Summary collapse
-
#instance_thread_global(name, value = nil) ⇒ Object
Define a thread global variable for the current instance with name name.
-
#thread_global(name, default_value = nil) ⇒ Object
Define a thread global variable named name in this module/class.
Instance Method Details
#instance_thread_global(name, value = nil) ⇒ Object
Define a thread global variable for the current instance with name name. If the value value is given, it is used to initialize the variable.
134 135 136 137 138 139 140 141 |
# File 'lib/dslkit/polite.rb', line 134 def instance_thread_global(name, value = nil) sc = class << self extend DSLKit::ThreadGlobal self end sc.thread_global name, value self end |
#thread_global(name, default_value = nil) ⇒ Object
Define a thread global variable named name in this module/class. If the value value is given, it is used to initialize the variable.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dslkit/polite.rb', line 110 def thread_global(name, default_value = nil) is_a?(Module) or raise TypeError, "receiver has to be a Module" name = name.to_s var_name = "@__#{name}_#{__id__.abs}__" lock = Mutex.new modul = self define_method(name) do lock.synchronize { modul.instance_variable_get var_name } end define_method(name + "=") do |value| lock.synchronize { modul.instance_variable_set var_name, value } end modul.instance_variable_set var_name, default_value if default_value self end |