Parent

Files

Class/Module Index [+]

Quicksearch

Concurrent::AbstractThreadLocalVar

@!macro [attach] thread_local_var

A `ThreadLocalVar` is a variable where the value is different for each thread.
Each variable may have a default value, but when you modify the variable only
the current thread will ever see that change.

@example
  v = ThreadLocalVar.new(14)
  v.value #=> 14
  v.value = 2
  v.value #=> 2

@example
  v = ThreadLocalVar.new(14)

  t1 = Thread.new do
    v.value #=> 14
    v.value = 1
    v.value #=> 1
  end

  t2 = Thread.new do
    v.value #=> 14
    v.value = 2
    v.value #=> 2
  end

  v.value #=> 14

@see https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html Java ThreadLocal

@!visibility private


@!visibility private

Constants

NIL_SENTINEL

@!visibility private

Public Class Methods

new(default = nil) click to toggle source

@!macro [attach] thread_local_var_method_initialize

Creates a thread local variable.

@param [Object] default the default value when otherwise unset
# File lib/concurrent/atomic/thread_local_var.rb, line 48
def initialize(default = nil)
  @default = default
  allocate_storage
end

Public Instance Methods

bind(value, &block) click to toggle source

@!macro [attach] thread_local_var_method_bind

Bind the given value to thread local storage during
execution of the given block.

@param [Object] value the value to bind
@yield the operation to be performed with the bound variable
@return [Object] the value
# File lib/concurrent/atomic/thread_local_var.rb, line 88
def bind(value, &block)
  if value.nil?
    stored_value = NIL_SENTINEL
  else
    stored_value = value
  end

  set(stored_value, &block)

  value
end
value() click to toggle source

@!macro [attach] thread_local_var_method_get

Returns the value in the current thread's copy of this thread-local variable.

@return [Object] the current value
# File lib/concurrent/atomic/thread_local_var.rb, line 58
def value
  value = get

  if value.nil?
    @default
  elsif value == NIL_SENTINEL
    nil
  else
    value
  end
end
value=(value) click to toggle source

@!macro [attach] thread_local_var_method_set

Sets the current thread's copy of this thread-local variable to the specified value.

@param [Object] value the value to set
@return [Object] the new value
# File lib/concurrent/atomic/thread_local_var.rb, line 76
def value=(value)
  bind value
end

Protected Instance Methods

allocate_storage() click to toggle source

@!visibility private

# File lib/concurrent/atomic/thread_local_var.rb, line 103
def allocate_storage
  raise NotImplementedError
end
get() click to toggle source

@!visibility private

# File lib/concurrent/atomic/thread_local_var.rb, line 108
def get
  raise NotImplementedError
end
set(value) click to toggle source

@!visibility private

# File lib/concurrent/atomic/thread_local_var.rb, line 113
def set(value)
  raise NotImplementedError
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.