Skip to content

Commit 55fb4c8

Browse files
committed
Remove the async-support crate
1 parent 682a3e3 commit 55fb4c8

File tree

11 files changed

+63
-142
lines changed

11 files changed

+63
-142
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ custom_alloc_error_handler = ["libtock-core/custom_alloc_error_handler"]
1212
__internal_disable_gpio_in_integration_test = []
1313

1414
[dependencies]
15-
core = { package = "async-support", path = "async-support" }
1615
libtock-core = { path = "core" }
1716
libtock_codegen = { path = "codegen" }
1817
futures = { version = "0.3.1", default-features = false, features = ["unstable", "cfg-target-has-atomic"] }
@@ -58,7 +57,6 @@ lto = true
5857

5958
[workspace]
6059
members = [
61-
"async-support",
6260
"codegen",
6361
"core",
6462
"test-runner"

async-support/Cargo.toml

Lines changed: 0 additions & 8 deletions
This file was deleted.

async-support/src/lib.rs

Lines changed: 0 additions & 109 deletions
This file was deleted.

codegen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn try_generate_main_wrapped(
9898
MAIN_INVOKED = true;
9999
}
100100
let _block = async #block;
101-
unsafe {::core::executor::block_on(_block) }
101+
unsafe { ::libtock::executor::block_on(_block) }
102102
}
103103
))
104104
}

core/src/syscalls/mod.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,7 @@ use crate::result::SubscribeError;
1010
use crate::shared_memory::SharedMemory;
1111

1212
pub mod raw {
13-
use super::platform;
14-
15-
pub use platform::*;
16-
17-
/// # Safety
18-
///
19-
/// Yielding in the main function should be safe. Nevertheless, yielding manually is not required as this is already achieved by the `async` runtime.
20-
///
21-
/// When yielding in callbacks, two problems can arise:
22-
/// - The guarantees of `FnMut` are violated. In this case, make sure your callback has `Fn` behavior.
23-
/// - Callbacks can get executed in a nested manner and overflow the stack quickly.
24-
///
25-
/// This function is exported as `libtock::syscalls::raw::yieldk`. Do not reference this name directly. Its only purpose is to establish a back-channel from `async-support`, a patched version of `core` to `libtock-rs` via linking. This workaround has been chosen to keep the `core` crate free of dependencies on platform-specific syscall implementations and is expected to get removed as soon as possible.
26-
#[export_name = "libtock::syscalls::raw::yieldk"]
27-
pub unsafe fn yieldk() {
28-
platform::yieldk()
29-
}
13+
pub use super::platform::*;
3014
}
3115

3216
pub fn subscribe<C: Consumer<T>, T>(

core/src/syscalls/platform.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ use core::cell::RefCell;
33
use std::vec::Vec;
44

55
/// yield for a callback fired by the kernel
6+
///
67
/// # Safety
7-
/// Yielding inside a callback conflicts with Rust's safety guarantees. For example,
8-
/// a FnMut closure could be triggered multiple times making a &mut a shared reference.
8+
///
9+
/// Yielding in the main function should be safe. Nevertheless, yielding manually is not required as this is already achieved by the `async` runtime.
10+
///
11+
/// When yielding in callbacks, two problems can arise:
12+
/// - The guarantees of `FnMut` are violated. In this case, make sure your callback has `Fn` behavior.
13+
/// - Callbacks can get executed in a nested manner and overflow the stack quickly.
914
pub unsafe fn yieldk() {
1015
EVENTS.with(|e| e.borrow_mut().push(Event::YieldK));
1116
}

src/console.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::callback::Identity0Consumer;
2+
use crate::executor;
23
use crate::futures;
34
use crate::result::TockResult;
45
use crate::syscalls;
56
use core::cell::Cell;
6-
use core::executor;
77
use core::fmt;
88
use core::mem;
99

src/executor.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use futures::Future;
2+
use core::task::Waker;
3+
use core::task::Poll;
4+
use core::task::Context;
5+
use core::task::RawWaker;
6+
use core::task::RawWakerVTable;
7+
use core::pin::Pin;
8+
use core::ptr;
9+
use crate::syscalls;
10+
11+
/// # Safety
12+
///
13+
/// [[block_on]] yields whenever a future cannot make any progress at present. Yielding is considered unsafe.
14+
pub unsafe fn block_on<T>(mut future: impl Future<Output = T>) -> T {
15+
// Contract described in the Rustdoc: "A value, once pinned, must remain pinned forever (...).".
16+
// IOW calling Pin::new_unchecked is safe as long as no &mut future is leaked after pinning.
17+
let mut pinned_future = Pin::new_unchecked(&mut future);
18+
19+
loop {
20+
match poll(pinned_future.as_mut()) {
21+
Poll::Pending => syscalls::raw::yieldk(),
22+
Poll::Ready(value) => {
23+
return value;
24+
}
25+
}
26+
}
27+
}
28+
29+
fn poll<F: Future>(pinned_future: Pin<&mut F>) -> Poll<F::Output> {
30+
let waker = unsafe { Waker::from_raw(get_dummy_waker()) };
31+
let mut context = Context::from_waker(&waker);
32+
pinned_future.poll(&mut context)
33+
}
34+
35+
// Since Tock OS comes with waking-up functionality built-in, we use dummy wakers that do nothing at all.
36+
fn get_dummy_waker() -> RawWaker {
37+
fn clone(_x: *const ()) -> RawWaker {
38+
get_dummy_waker()
39+
}
40+
41+
fn do_nothing(_x: *const ()) {}
42+
43+
// This vtable implements the methods required for managing the lifecycle of the wakers.
44+
// Our wakers are dummies, so those functions don't do anything.
45+
static DUMMY_WAKER_VTABLE: RawWakerVTable =
46+
RawWakerVTable::new(clone, do_nothing, do_nothing, do_nothing);
47+
48+
// The wakers don't have any implementation, so the instance can simply be null.
49+
RawWaker::new(ptr::null(), &DUMMY_WAKER_VTABLE)
50+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod console;
88
pub mod debug;
99
pub mod drivers;
1010
pub mod electronics;
11+
pub mod executor;
1112
pub mod futures;
1213
pub mod gpio;
1314
pub mod leds;

src/sensors/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use crate::executor;
12
use crate::futures;
23
use crate::result::TockResult;
34
use crate::syscalls;
45
use core::cell::Cell;
56
use core::convert::From;
6-
use core::executor;
77
use core::fmt;
88
use core::mem;
99

0 commit comments

Comments
 (0)