Skip to content

Commit 74702e0

Browse files
committed
Use direct instance variable for async_task.
1 parent 7ad8fd8 commit 74702e0

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

lib/async/task.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
require_relative 'node'
1414
require_relative 'condition'
1515

16+
Fiber.attr_accessor :async_task
17+
1618
module Async
1719
# Raised when a task is explicitly stopped.
1820
class Stop < Exception
@@ -325,13 +327,13 @@ def defer_stop
325327
# @returns [Task]
326328
# @raises[RuntimeError] If task was not {set!} for the current fiber.
327329
def self.current
328-
Thread.current[:async_task] or raise RuntimeError, "No async task available!"
330+
Fiber.current.async_task or raise RuntimeError, "No async task available!"
329331
end
330332

331333
# Check if there is a task defined for the current fiber.
332334
# @returns [Interface(:async) | Nil]
333335
def self.current?
334-
Thread.current[:async_task]
336+
Fiber.current.async_task
335337
end
336338

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

398400
def schedule(&block)
399401
@fiber = Fiber.new(annotation: self.annotation) do
400-
set!
401-
402402
begin
403403
completed!(yield)
404404
rescue Stop
@@ -416,13 +416,9 @@ def schedule(&block)
416416
end
417417
end
418418

419+
@fiber.async_task = self
420+
419421
self.root.resume(@fiber)
420422
end
421-
422-
# Set the current fiber's `:async_task` to this task.
423-
def set!
424-
# This is actually fiber-local:
425-
Thread.current[:async_task] = self
426-
end
427423
end
428424
end

0 commit comments

Comments
 (0)