1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
require 'concurrent/utility/engine'
require 'concurrent/atomic/ruby_thread_local_var'
require 'concurrent/atomic/java_thread_local_var'
module Concurrent
###################################################################
# @!macro thread_local_var_method_initialize
#
# Creates a thread local variable.
#
# @param [Object] default the default value when otherwise unset
# @param [Proc] default_block Optional block that gets called to obtain the
# default value for each thread
# @!macro thread_local_var_method_get
#
# Returns the value in the current thread's copy of this thread-local variable.
#
# @return [Object] the current value
# @!macro 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
# @!macro 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
###################################################################
# @!macro thread_local_var_public_api
#
# @!method initialize(default = nil, &default_block)
# @!macro thread_local_var_method_initialize
#
# @!method value
# @!macro thread_local_var_method_get
#
# @!method value=(value)
# @!macro thread_local_var_method_set
#
# @!method bind(value, &block)
# @!macro thread_local_var_method_bind
###################################################################
# @!visibility private
# @!macro internal_implementation_note
ThreadLocalVarImplementation = case
when Concurrent.on_jruby?
JavaThreadLocalVar
else
RubyThreadLocalVar
end
private_constant :ThreadLocalVarImplementation
# @!macro 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.
#
# @!macro thread_safe_variable_comparison
#
# @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
#
# @!macro thread_local_var_public_api
class ThreadLocalVar < ThreadLocalVarImplementation
end
end
|