Skip to content

Commit

Permalink
Expose fiber_local_attribute_name and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 2, 2024
1 parent 7bee19f commit ac223df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 15 additions & 8 deletions lib/fiber/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
class Fiber
module Local
def self.extended(klass)
instance_attribute_name = klass.name.gsub('::', '_').gsub(/\W/, '').downcase
klass.instance_variable_set(:@instance_attribute_name, instance_attribute_name)
klass.instance_variable_set(:@instance_variable_name, :"@#{instance_attribute_name}")
attribute_name = klass.name.gsub('::', '_').gsub(/\W/, '').downcase.to_sym

Thread.attr_accessor(instance_attribute_name)
# This is used for the general interface and fiber storage key:
klass.instance_variable_set(:@fiber_local_attribute_name, attribute_name)
klass.singleton_class.attr :fiber_local_attribute_name

# This is used for reading and writing directly to the thread instance variables:
klass.instance_variable_set(:@fiber_local_variable_name, :"@#{attribute_name}")

Thread.attr_accessor(attribute_name)
end

# Instantiate a new thread-local object.
Expand All @@ -26,14 +31,16 @@ def local
# Get the current thread-local instance. Create it if required.
# @returns [Object] The thread-local instance.
def instance
if instance = Fiber[@instance_variable_name]
# 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(@instance_variable_name)
unless instance = thread.instance_variable_get(@fiber_local_variable_name)
if instance = self.local
thread.instance_variable_set(@instance_variable_name, instance)
thread.instance_variable_set(@fiber_local_variable_name, instance)
end
end

Expand All @@ -43,7 +50,7 @@ def instance
# Assigns to the fiber-local instance.
# @parameter instance [Object] The object that will become the thread-local instance.
def instance= instance
Fiber[@instance_variable_name] = instance
Fiber[@fiber_local_attribute_name] = instance
end
end
end
19 changes: 17 additions & 2 deletions test/fiber/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,23 @@ class MyThing
expect(MyThing.instance).to be_equal(MyThing.instance)
end

it "inherits assigned values in same thread" do
Thread
it "inherits assigned values in nested fibers" do
instance = Object.new

storage = Fiber.new do
MyThing.instance = instance
expect(MyThing.instance).to be_equal(instance)

Fiber.new do
expect(MyThing.instance).to be_equal(instance)

Fiber.current.storage
end.resume
end.resume

expect(storage).to have_keys(
MyThing.fiber_local_attribute_name => be_equal(instance)
)
end
end

Expand Down

0 comments on commit ac223df

Please sign in to comment.