Module: OpenHAB::Core::ThreadLocal

Overview

Manages thread local varaibles for access inside of blocks

Instance Method Summary collapse

Methods included from Log

included, logger, #logger

Instance 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:

  • Keys (Hash)

    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 thread_local(**values)
  old_values = values.map { |key, _value| [key, Thread.current[key]] }.to_h
  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