Skip to content

Commit

Permalink
Use direct instance variable for async_task.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 15, 2024
1 parent 7ad8fd8 commit 74702e0
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
require_relative 'node'
require_relative 'condition'

Fiber.attr_accessor :async_task

This comment has been minimized.

Copy link
@paddor

paddor Jul 16, 2024

Contributor

Since this accessor is only used in this file, a refinement would probably work.

This comment has been minimized.

Copy link
@ioquatix

ioquatix Jul 16, 2024

Author Member

I would be concerned about the performance and compatibility. Feel free to make a PR.

This comment has been minimized.

Copy link
@paddor

paddor Jul 17, 2024

Contributor

Done in #335


module Async
# Raised when a task is explicitly stopped.
class Stop < Exception
Expand Down Expand Up @@ -325,13 +327,13 @@ def defer_stop
# @returns [Task]
# @raises[RuntimeError] If task was not {set!} for the current fiber.
def self.current
Thread.current[:async_task] or raise RuntimeError, "No async task available!"
Fiber.current.async_task or raise RuntimeError, "No async task available!"
end

# Check if there is a task defined for the current fiber.
# @returns [Interface(:async) | Nil]
def self.current?
Thread.current[:async_task]
Fiber.current.async_task
end

# @returns [Boolean] Whether this task is the currently executing task.
Expand Down Expand Up @@ -397,8 +399,6 @@ def stop!

def schedule(&block)
@fiber = Fiber.new(annotation: self.annotation) do
set!

begin
completed!(yield)
rescue Stop
Expand All @@ -416,13 +416,9 @@ def schedule(&block)
end
end

@fiber.async_task = self

self.root.resume(@fiber)
end

# Set the current fiber's `:async_task` to this task.
def set!
# This is actually fiber-local:
Thread.current[:async_task] = self
end
end
end

0 comments on commit 74702e0

Please sign in to comment.