Skip to content

Commit

Permalink
Re-enable support for synchronous main functions and fix panic!() i…
Browse files Browse the repository at this point in the history
…n boards with no LED capsule.

This breaks the existing support for `async` `main()` functions. Instead, an `async_main!()` macro is provided to generate a synchronous `main()` that wraps the provided `async` `main()` function (which, unfortunately, must not be called `main()`).

This solves tock#111 and tock#113.

I would prefer to be more consistent with Tokio and use a procedural macro attribute (as was originally done in tock#104), but I found that has some major drawbacks:
  1. If main() does not parse, the error message does not point to the correct code.
  2. It adds a requirement to update `syn` every time the toolchain is updated.
These drawbacks are elaborated upon in tock#111.
  • Loading branch information
jrvanwhy committed Dec 12, 2019
1 parent 31c43c4 commit ab4569f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ impl<T, F: Fn() -> Option<T>> Future for WaitForValue<F> {
}
}
}

/// Generates a synchronous `main()` function that calls the provided
/// asynchronous function.
#[macro_export]
macro_rules! async_main {
($main_name:ident) => {
fn main() {
unsafe { ::core::executor::block_on($main_name()); }
}
};
}
24 changes: 7 additions & 17 deletions src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,26 @@
//! crate.
use crate::led;
use crate::result::TockError;
use crate::timer;
use crate::timer::Duration;
use core::alloc::Layout;
use core::executor;
use core::future::Future;
use core::panic::PanicInfo;

#[lang = "start"]
extern "C" fn start<T>(main: fn() -> T, _argc: isize, _argv: *const *const u8) -> i32
extern "C" fn start<T>(main: fn() -> T, _argc: isize, _argv: *const *const u8)
where
T: Termination,
{
main().report()
main();
}

// Termination is a required lang item, so we must include it. We don't have
// much use for it, however (unlike Unix-based OSes, Tock does not have a
// concept of an exit code), so it is a minimal trait.
#[lang = "termination"]
pub trait Termination {
fn report(self) -> i32;
}

impl<T> Termination for T
where
T: Future<Output = Result<(), TockError>>,
{
fn report(self) -> i32 {
let _ = unsafe { executor::block_on(self) };
0
}
}
pub trait Termination {}
impl Termination for () {}

#[panic_handler]
unsafe fn panic_handler(_info: &PanicInfo) -> ! {
Expand Down
4 changes: 3 additions & 1 deletion src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ pub fn get(led_num: usize) -> Option<Led> {
}
}

/// Returns an iterator over all available LEDs. If the LED driver is not
/// present, the iterator will be empty.
pub fn all() -> LedIter {
LedIter {
curr_led: 0,
led_count: count().ok().unwrap(),
led_count: count().unwrap_or(0),
}
}

Expand Down

0 comments on commit ab4569f

Please sign in to comment.