@@ -32,6 +32,7 @@ class RubyThreadLocalVar < AbstractThreadLocalVar
3232 FREE = [ ]
3333 LOCK = Mutex . new
3434 ARRAYS = { } # used as a hash set
35+ # noinspection RubyClassVariableUsageInspection
3536 @@next = 0
3637 QUEUE = Queue . new
3738 THREAD = Thread . new do
@@ -62,7 +63,7 @@ class RubyThreadLocalVar < AbstractThreadLocalVar
6263
6364 # @!macro thread_local_var_method_get
6465 def value
65- if array = get_threadlocal_array
66+ if ( array = get_threadlocal_array )
6667 value = array [ @index ]
6768 if value . nil?
6869 default
@@ -82,7 +83,7 @@ def value=(value)
8283 # We could keep the thread-local arrays in a hash, keyed by Thread
8384 # But why? That would require locking
8485 # Using Ruby's built-in thread-local storage is faster
85- unless array = get_threadlocal_array ( me )
86+ unless ( array = get_threadlocal_array ( me ) )
8687 array = set_threadlocal_array ( [ ] , me )
8788 LOCK . synchronize { ARRAYS [ array . object_id ] = array }
8889 ObjectSpace . define_finalizer ( me , self . class . thread_finalizer ( array . object_id ) )
@@ -94,6 +95,7 @@ def value=(value)
9495 protected
9596
9697 # @!visibility private
98+ # noinspection RubyClassVariableUsageInspection
9799 def allocate_storage
98100 @index = LOCK . synchronize do
99101 FREE . pop || begin
@@ -107,6 +109,7 @@ def allocate_storage
107109
108110 # @!visibility private
109111 def self . thread_local_finalizer ( index )
112+ # avoid error: can't be called from trap context
110113 proc { QUEUE . push [ :thread_local_finalizer , index ] }
111114 end
112115
@@ -142,21 +145,22 @@ def set_threadlocal_array(array, thread = Thread.current)
142145 # This exists only for use in testing
143146 # @!visibility private
144147 def value_for ( thread )
145- if array = get_threadlocal_array ( thread )
148+ if ( array = get_threadlocal_array ( thread ) )
146149 value = array [ @index ]
147150 if value . nil?
148- default_for ( thread )
151+ get_default
149152 elsif value . equal? ( NULL )
150153 nil
151154 else
152155 value
153156 end
154157 else
155- default_for ( thread )
158+ get_default
156159 end
157160 end
158161
159- def default_for ( thread )
162+ # @!visibility private
163+ def get_default
160164 if @default_block
161165 raise "Cannot use default_for with default block"
162166 else
0 commit comments