-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use direct instance variable for
async_task
.
- Loading branch information
Showing
1 changed file
with
6 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
require_relative 'node' | ||
require_relative 'condition' | ||
|
||
Fiber.attr_accessor :async_task | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ioquatix
Author
Member
|
||
|
||
module Async | ||
# Raised when a task is explicitly stopped. | ||
class Stop < Exception | ||
|
@@ -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. | ||
|
@@ -397,8 +399,6 @@ def stop! | |
|
||
def schedule(&block) | ||
@fiber = Fiber.new(annotation: self.annotation) do | ||
set! | ||
|
||
begin | ||
completed!(yield) | ||
rescue Stop | ||
|
@@ -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 |
Since this accessor is only used in this file, a refinement would probably work.