module Local
Definitions
def local
Instantiate a new thread-local object.
By default, invokes new
to generate the instance.
Signature
-
returns
Object
Implementation
def local
self.new
end
def instance
Get the current thread-local instance. Create it if required.
Signature
-
returns
Object
The thread-local instance.
Implementation
def instance
# This is considered a local "override" in the dynamic scope of the fiber:
if instance = Fiber[@fiber_local_attribute_name]
return instance
end
# This is generally the fast path:
thread = Thread.current
unless instance = thread.instance_variable_get(@fiber_local_variable_name)
if instance = self.local
thread.instance_variable_set(@fiber_local_variable_name, instance)
end
end
return instance
end
def instance= instance
Assigns to the fiber-local instance.
Signature
-
parameter
instance
Object
The object that will become the thread-local instance.
Implementation
def instance= instance
Fiber[@fiber_local_attribute_name] = instance
end