Skip to content
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

WIP: Non-blocking Rust futures running on local thread #1051

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pkgs/create-neon/create-neon-manual-test-project
test/cli/lib
npm-debug.log
rls*.log
.vscode
111 changes: 108 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/neon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ doc-comment = { version = "0.3.3", optional = true }
send_wrapper = "0.6.0"
serde = { version = "1.0.197", optional = true }
serde_json = { version = "1.0.114", optional = true }
futures = { version = "0.3", optional = true }

[dependencies.tokio]
version = "1.34.0"
Expand All @@ -57,6 +58,8 @@ external-buffers = []
# https://github.com/neon-bindings/rfcs/pull/46
futures = ["tokio"]

async_local = ["dep:futures"]

# Enable low-level system APIs. The `sys` API allows augmenting the Neon API
# from external crates.
sys = []
Expand Down
53 changes: 53 additions & 0 deletions crates/neon/src/async_local/executor/enter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::cell::Cell;
use std::fmt;

std::thread_local!(static ENTERED: Cell<bool> = Cell::new(false));

pub struct Enter {
_priv: (),
}

pub struct EnterError {
_priv: (),
}

impl fmt::Debug for EnterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EnterError").finish()
}
}

impl fmt::Display for EnterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "an execution scope has already been entered")
}
}

impl std::error::Error for EnterError {}

pub fn enter() -> Result<Enter, EnterError> {
ENTERED.with(|c| {
if c.get() {
Err(EnterError { _priv: () })
} else {
c.set(true);

Ok(Enter { _priv: () })
}
})
}

impl fmt::Debug for Enter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Enter").finish()
}
}

impl Drop for Enter {
fn drop(&mut self) {
ENTERED.with(|c| {
assert!(c.get());
c.set(false);
});
}
}
Loading
Loading