-
Notifications
You must be signed in to change notification settings - Fork 1k
Compilation guard && tokio::sync re-export from runtime crate.
#5255
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
Open
Shubham8287
wants to merge
2
commits into
master
Choose a base branch
from
shub/runtime-base
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−30
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| #[cfg(all(feature = "tokio", feature = "simulation"))] | ||
| compile_error!( | ||
| "spacetimedb-runtime requires exactly one runtime backend: enable either `tokio` or `simulation`, not both" | ||
| ); | ||
|
|
||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| compile_error!("spacetimedb-runtime requires exactly one runtime backend: enable either `tokio` or `simulation`"); | ||
|
|
||
| #[cfg(feature = "simulation")] | ||
| extern crate alloc; | ||
|
|
||
|
|
@@ -15,8 +23,19 @@ pub mod sim; | |
| #[cfg(feature = "simulation")] | ||
| pub mod sim_std; | ||
|
|
||
| #[cfg(feature = "tokio")] | ||
| pub type TokioHandle = tokio::runtime::Handle; | ||
| pub type TokioRuntime = tokio::runtime::Runtime; | ||
| pub type TokioRuntimeBuilder = tokio::runtime::Builder; | ||
|
|
||
| // We intentionally re-export `tokio::sync` even when the simulation backend is | ||
| // selected. Async and non-blocking synchronization operations are | ||
| // executor-agnostic, so driving them from the deterministic simulation runtime | ||
| // remains deterministic. | ||
| // | ||
| // Callers must avoid APIs that block or park OS threads on their own, such as | ||
|
Contributor
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. Are there any other known besides |
||
| // `blocking_send`, because those semantics are outside the simulation runtime's | ||
| // deterministic scheduler. | ||
| pub use tokio::sync; | ||
|
|
||
| #[derive(Clone)] | ||
| pub enum Handle { | ||
|
|
@@ -74,15 +93,22 @@ enum JoinErrorInner { | |
| Simulation(sim::JoinError), | ||
| } | ||
|
|
||
| #[cfg(feature = "tokio")] | ||
| impl From<tokio::task::AbortHandle> for AbortHandle { | ||
| fn from(handle: tokio::task::AbortHandle) -> Self { | ||
| Self { | ||
| inner: AbortHandleInner::Tokio(handle), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl AbortHandle { | ||
| pub fn abort(&self) { | ||
| match &self.inner { | ||
| #[cfg(feature = "tokio")] | ||
| AbortHandleInner::Tokio(handle) => handle.abort(), | ||
| #[cfg(feature = "simulation")] | ||
| AbortHandleInner::Simulation(handle) => handle.abort(), | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| _ => unreachable!("runtime abort handle has no enabled backend"), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -100,16 +126,10 @@ impl JoinErrorInner { | |
|
|
||
| impl fmt::Display for JoinError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| let _ = f; | ||
| #[cfg(any(feature = "tokio", feature = "simulation"))] | ||
| return self.inner.fmt(f); | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| unreachable!("runtime join error has no enabled backend") | ||
| self.inner.fmt(f) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(any(feature = "tokio", feature = "simulation"))] | ||
| impl std::error::Error for JoinError {} | ||
|
|
||
| impl<T> JoinHandleInner<T> { | ||
|
|
@@ -160,8 +180,6 @@ impl<T> Future for JoinHandle<T> { | |
| type Output = Result<T, JoinError>; | ||
|
|
||
| fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| let _ = cx; | ||
| match self.inner.poll_result(cx) { | ||
| Poll::Ready(Ok(output)) => { | ||
| self.inner = JoinHandleInner::Detached(PhantomData); | ||
|
|
@@ -197,17 +215,30 @@ impl fmt::Display for RuntimeTimeout { | |
| } | ||
| } | ||
|
|
||
| #[cfg(any(feature = "tokio", feature = "simulation"))] | ||
| impl std::error::Error for RuntimeTimeout {} | ||
|
|
||
| #[cfg(feature = "tokio")] | ||
| impl Handle { | ||
| pub fn tokio(handle: TokioHandle) -> Self { | ||
| Self::Tokio(handle) | ||
| #[cfg(feature = "tokio")] | ||
| { | ||
| Self::Tokio(handle) | ||
| } | ||
| #[cfg(not(feature = "tokio"))] | ||
| { | ||
| let _ = handle; | ||
| panic!("spacetimedb-runtime tokio handle requested without the `tokio` backend enabled") | ||
| } | ||
| } | ||
|
|
||
| pub fn tokio_current() -> Self { | ||
| Self::tokio(TokioHandle::current()) | ||
| #[cfg(feature = "tokio")] | ||
| { | ||
| Self::tokio(TokioHandle::current()) | ||
| } | ||
| #[cfg(not(feature = "tokio"))] | ||
| { | ||
| panic!("spacetimedb-runtime current tokio handle requested without the `tokio` backend enabled") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -220,8 +251,6 @@ impl Handle { | |
|
|
||
| impl Handle { | ||
| pub fn spawn<T: Send + 'static>(&self, future: impl Future<Output = T> + Send + 'static) -> JoinHandle<T> { | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| let _ = future; | ||
| match self { | ||
| #[cfg(feature = "tokio")] | ||
| Self::Tokio(handle) => JoinHandle { | ||
|
|
@@ -231,8 +260,6 @@ impl Handle { | |
| Self::Simulation(handle) => JoinHandle { | ||
| inner: JoinHandleInner::Simulation(handle.spawn_on(sim::NodeId::MAIN, future)), | ||
| }, | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| _ => unreachable!("runtime dispatch has no enabled backend"), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -241,8 +268,6 @@ impl Handle { | |
| F: FnOnce() -> R + Send + 'static, | ||
| R: Send + 'static, | ||
| { | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| let _ = &f; | ||
| match self { | ||
| #[cfg(feature = "tokio")] | ||
| Self::Tokio(_) => tokio::task::spawn_blocking(f) | ||
|
|
@@ -261,8 +286,6 @@ impl Handle { | |
| .spawn_on(sim::NodeId::MAIN, async move { f() }) | ||
| .await | ||
| .expect("simulation spawn_blocking task should not be cancelled"), | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| _ => unreachable!("runtime dispatch has no enabled backend"), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -271,17 +294,22 @@ impl Handle { | |
| timeout_after: Duration, | ||
| future: impl Future<Output = T>, | ||
| ) -> Result<T, RuntimeTimeout> { | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| let _ = (timeout_after, future); | ||
| match self { | ||
| #[cfg(feature = "tokio")] | ||
| Self::Tokio(_) => tokio::time::timeout(timeout_after, future) | ||
| .await | ||
| .map_err(|_| RuntimeTimeout), | ||
| #[cfg(feature = "simulation")] | ||
| Self::Simulation(handle) => handle.timeout(timeout_after, future).await.map_err(|_| RuntimeTimeout), | ||
| #[cfg(not(any(feature = "tokio", feature = "simulation")))] | ||
| _ => unreachable!("runtime dispatch has no enabled backend"), | ||
| } | ||
| } | ||
|
|
||
| pub async fn sleep(&self, duration: Duration) { | ||
| match self { | ||
| #[cfg(feature = "tokio")] | ||
| Self::Tokio(_) => tokio::time::sleep(duration).await, | ||
| #[cfg(feature = "simulation")] | ||
| Self::Simulation(handle) => handle.sleep(duration).await, | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
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.
Not that this is much clearer, feel free to ignore 😅