-
-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
signal-neon-futures: Poll a future once before queuing it #283
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -20,11 +20,7 @@ pub trait EventQueueEx { | |
impl EventQueueEx for EventQueue { | ||
fn send_future(&self, future: impl Future<Output = ()> + 'static + Send) { | ||
self.send(move |mut cx| { | ||
let task = Arc::new(FutureTask { | ||
queue: cx.queue(), | ||
future: Mutex::new(Some(Box::pin(future))), | ||
}); | ||
task.poll(); | ||
cx.run_future(future); | ||
Ok(()) | ||
}) | ||
} | ||
|
@@ -43,29 +39,44 @@ impl<T: Future> Future for AssertSendSafe<T> { | |
} | ||
} | ||
|
||
/// Adds support for executing closures and futures on the JavaScript main thread's microtask queue. | ||
/// Adds support for executing closures and futures on the JavaScript main thread's event queue. | ||
pub trait ContextEx<'a>: Context<'a> { | ||
/// Schedules `f` to run on the microtask queue. | ||
/// Schedules `f` to run on the JavaScript thread's event queue. | ||
/// | ||
/// Equivalent to `cx.queue().send(f)` except that `f` doesn't need to be `Send`. | ||
fn run_on_queue(&mut self, f: impl FnOnce(TaskContext<'_>) -> NeonResult<()> + 'static) { | ||
fn queue_task(&mut self, f: impl FnOnce(TaskContext<'_>) -> NeonResult<()> + 'static) { | ||
// Because we're currently in a JavaScript context, | ||
// and `f` will run on the event queue associated with the current context, | ||
// we can assert that it's safe to Send `f` to the queue. | ||
let f = AssertSendSafe(f); | ||
self.queue().send(move |cx| f.0(cx)); | ||
} | ||
|
||
/// Schedules `f` to run on the microtask queue. | ||
/// Schedules `f` to run on the JavaScript thread's event queue. | ||
/// | ||
/// Equivalent to `cx.queue().send_future(f)` except that `f` doesn't need to be `Send`. | ||
fn run_future_on_queue(&mut self, f: impl Future<Output = ()> + 'static) { | ||
fn queue_future(&mut self, f: impl Future<Output = ()> + 'static) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No one calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right; they're there for "completeness" but I could just drop them. |
||
// Because we're currently in a JavaScript context, | ||
// and `f` will run on the event queue associated with the current context, | ||
// we can assert that it's safe to Send `f` to the queue. | ||
let f = AssertSendSafe(f); | ||
self.queue().send_future(f); | ||
} | ||
|
||
/// Runs `f` on the JavaScript thread's event queue. | ||
/// | ||
/// Polls the future once synchronously, then schedules it to resume on the event queue. | ||
fn run_future(&mut self, f: impl Future<Output = ()> + 'static) { | ||
// Because we're currently in a JavaScript context, | ||
// and `f` will run on the event queue associated with the current context, | ||
// we can assert that it's safe to Send `f` to the queue. | ||
let f = AssertSendSafe(f); | ||
let task = Arc::new(FutureTask { | ||
queue: self.queue(), | ||
future: Mutex::new(Some(Box::pin(f))), | ||
}); | ||
task.poll(); | ||
} | ||
} | ||
|
||
impl<'a, T: Context<'a>> ContextEx<'a> for T {} | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my mistake; we're using Node's event queue rather than JavaScript's microtask queue.