Module: OpenHAB::Core::ThreadLocal

Includes:
Log
Included in:
DSL::Items::TimedCommand::GenericItem::TimedCommandCancelRule, DSL::Rules::AutomationRule, DSL::Timer
Defined in:
lib/openhab/core/thread_local.rb

Overview

Manages thread local varaibles for access inside of blocks

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

included, logger

Class Method Details

.thread_local(**values) ⇒ Object

Execute the supplied block with the supplied values set for the currently running thread The previous values for each key are restored after the block is executed

Parameters:

  • values (Hash)

    Keys and values to set for running thread, if hash is nil no values are set



20
21
22
23
24
25
26
27
# File 'lib/openhab/core/thread_local.rb', line 20

def self.thread_local(**values)
  old_values = values.to_h { |key, _value| [key, Thread.current[key]] }
  values.each { |key, value| Thread.current[key] = value }
  logger.trace "Executing block with thread local context: #{values} - old context: #{old_values}"
  yield
ensure
  old_values.each { |key, value| Thread.current[key] = value }
end

Instance Method Details

#thread_local(**values, &block) ⇒ Object

Execute the supplied block with the supplied values set for the currently running thread The previous values for each key are restored after the block is executed

Parameters:

  • values (Hash)

    Keys and values to set for running thread, if hash is nil no values are set



35
36
37
# File 'lib/openhab/core/thread_local.rb', line 35

def thread_local(**values, &block)
  ThreadLocal.thread_local(**values, &block)
end