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

Rework asynchronous timers #31

Merged
merged 9 commits into from
Oct 20, 2023
Merged
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
31 changes: 25 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ members = [
"crates/sel4-async/network/mbedtls/mozilla-ca-list",
"crates/sel4-async/request-statuses",
"crates/sel4-async/single-threaded-executor",
"crates/sel4-async/timers",
"crates/sel4-async/time",
"crates/sel4-async/unsync",
"crates/sel4-backtrace",
"crates/sel4-backtrace/addr2line-context-helper",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sel4-async-block-io = { path = "../../../../../sel4-async/block-io" }
sel4-async-block-io-fat = { path = "../../../../../sel4-async/block-io/fat" }
sel4-async-network = { path = "../../../../../sel4-async/network" }
sel4-async-request-statuses = { path = "../../../../../sel4-async/request-statuses" }
sel4-async-timers = { path = "../../../../../sel4-async/timers" }
sel4-async-time = { path = "../../../../../sel4-async/time" }
sel4-bounce-buffer-allocator = { path = "../../../../../sel4-bounce-buffer-allocator" }
sel4-immediate-sync-once-cell = { path = "../../../../../sel4-immediate-sync-once-cell" }
sel4-logging = { path = "../../../../../sel4-logging" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sel4-async-block-io = { path = "../../../../../../sel4-async/block-io" }
sel4-async-block-io-fat = { path = "../../../../../../sel4-async/block-io/fat" }
sel4-async-network = { path = "../../../../../../sel4-async/network" }
sel4-async-network-mbedtls = { path = "../../../../../../sel4-async/network/mbedtls" }
sel4-async-timers = { path = "../../../../../../sel4-async/timers" }
sel4-async-time = { path = "../../../../../../sel4-async/time" }
sel4-async-unsync = { path = "../../../../../../sel4-async/unsync" }
sel4-panicking-env = { path = "../../../../../../sel4-panicking/env" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use mbedtls::ssl::async_io::ClosedError;

use sel4_async_block_io::{constant_block_sizes, BlockIO};
use sel4_async_block_io_fat as fat;
use sel4_async_network::{SharedNetwork, TcpSocketError};
use sel4_async_network::{ManagedInterface, TcpSocketError};
use sel4_async_network_mbedtls::{
insecure_dummy_rng, mbedtls, seed_insecure_dummy_rng, DbgCallbackBuilder, TcpSocketWrapper,
};
use sel4_async_single_threaded_executor::LocalSpawner;
use sel4_async_timers::SharedTimers;
use sel4_async_time::TimerManager;

mod mime;
mod server;
Expand All @@ -30,8 +30,8 @@ const HTTP_PORT: u16 = 80;
const HTTPS_PORT: u16 = 443;

pub async fn run_server<T: BlockIO<BlockSize = constant_block_sizes::BlockSize512> + Clone>(
_timers_ctx: SharedTimers,
network_ctx: SharedNetwork,
_timers_ctx: TimerManager,
network_ctx: ManagedInterface,
fs_block_io: T,
spawner: LocalSpawner,
cert_pem: &str,
Expand Down
35 changes: 21 additions & 14 deletions crates/examples/microkit/http-server/pds/server/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use alloc::boxed::Box;
use core::future::Future;
use core::pin::Pin;
use core::time::Duration;

use futures::future::LocalBoxFuture;

use smoltcp::iface::Config;
use smoltcp::time::{Duration, Instant};
use smoltcp::time::Instant as SmoltcpInstant;

use sel4_async_block_io::constant_block_sizes::BlockSize512;
use sel4_async_network::{DhcpOverrides, SharedNetwork};
use sel4_async_network::{DhcpOverrides, ManagedInterface};
use sel4_async_single_threaded_executor::{LocalPool, LocalSpawner};
use sel4_async_timers::SharedTimers;
use sel4_async_time::{Instant, TimerManager};
use sel4_shared_ring_buffer_block_io::SharedRingBufferBlockIO;

use crate::{DeviceImpl, TimerClient};
Expand All @@ -22,8 +23,8 @@ pub(crate) struct HandlerImpl {
timer: TimerClient,
net_device: DeviceImpl,
shared_block_io: SharedRingBufferBlockIO<BlockSize512>,
shared_timers: SharedTimers,
shared_network: SharedNetwork,
shared_timers: TimerManager,
shared_network: ManagedInterface,
local_pool: LocalPool,
fut: LocalBoxFuture<'static, !>,
}
Expand All @@ -38,14 +39,19 @@ impl HandlerImpl {
mut net_device: DeviceImpl,
net_config: Config,
shared_block_io: SharedRingBufferBlockIO<BlockSize512>,
f: impl FnOnce(SharedTimers, SharedNetwork, LocalSpawner) -> T,
f: impl FnOnce(TimerManager, ManagedInterface, LocalSpawner) -> T,
) -> Self {
let now = Self::now_with_timer_client(&timer);
let now_smoltcp = SmoltcpInstant::ZERO + now.since_zero().into();

let shared_timers = SharedTimers::new(now);
let shared_timers = TimerManager::new();

let shared_network =
SharedNetwork::new(net_config, DhcpOverrides::default(), &mut net_device, now);
let shared_network = ManagedInterface::new(
net_config,
DhcpOverrides::default(),
&mut net_device,
now_smoltcp,
);

let local_pool = LocalPool::new();
let spawner = local_pool.spawner();
Expand Down Expand Up @@ -75,11 +81,11 @@ impl HandlerImpl {
}

fn now_with_timer_client(timer: &TimerClient) -> Instant {
Instant::from_micros(i64::try_from(timer.now()).unwrap())
Instant::new(Duration::from_micros(timer.now()))
}

fn set_timeout(&self, d: Duration) {
self.timer.set_timeout(d.micros())
self.timer.set_timeout(d.as_micros().try_into().unwrap())
}

// TODO focused polling using these args doesn't play nicely with "repoll" mechanism below
Expand All @@ -92,15 +98,16 @@ impl HandlerImpl {
loop {
let _ = self.local_pool.run_until_stalled(Pin::new(&mut self.fut));
let now = self.now();
let now_smoltcp = SmoltcpInstant::ZERO + now.since_zero().into();
let mut activity = false;
activity |= self.shared_timers.poll(now);
activity |= self.net_device.poll();
activity |= self.shared_network.poll(now, &mut self.net_device);
activity |= self.shared_network.poll(now_smoltcp, &mut self.net_device);
activity |= self.shared_block_io.poll();
if !activity {
let delays = &[
self.shared_timers.poll_delay(now),
self.shared_network.poll_delay(now),
self.shared_timers.poll_at().map(|absolute| absolute - now),
self.shared_network.poll_delay(now_smoltcp).map(Into::into),
];
let mut repoll = false;
if let Some(delay) = delays.iter().filter_map(Option::as_ref).min() {
Expand Down
2 changes: 1 addition & 1 deletion crates/private/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sel4-async-block-io-fat = { path = "../../sel4-async/block-io/fat" }
sel4-async-network = { path = "../../sel4-async/network" }
sel4-async-request-statuses = { path = "../../sel4-async/request-statuses" }
sel4-async-single-threaded-executor = { path = "../../sel4-async/single-threaded-executor" }
sel4-async-timers = { path = "../../sel4-async/timers" }
sel4-async-time = { path = "../../sel4-async/time" }
sel4-async-unsync = { path = "../../sel4-async/unsync" }
sel4-bounce-buffer-allocator = { path = "../../sel4-bounce-buffer-allocator" }
sel4-config = { path = "../../sel4/config" }
Expand Down
2 changes: 1 addition & 1 deletion crates/private/meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ definitely! {
sel4_async_network
sel4_async_request_statuses
sel4_async_single_threaded_executor
sel4_async_timers
sel4_async_time
sel4_async_unsync
sel4_bounce_buffer_allocator
sel4_config
Expand Down
24 changes: 16 additions & 8 deletions crates/sel4-async/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub(crate) const DEFAULT_KEEP_ALIVE_INTERVAL: u64 = 75000;
pub(crate) const DEFAULT_TCP_SOCKET_BUFFER_SIZE: usize = 65535;

#[derive(Clone)]
pub struct SharedNetwork {
inner: Rc<RefCell<SharedNetworkInner>>,
pub struct ManagedInterface {
inner: Rc<RefCell<ManagedInterfaceShared>>,
}

struct SharedNetworkInner {
struct ManagedInterfaceShared {
iface: Interface,
socket_set: SocketSet<'static>,
dns_socket_handle: SocketHandle,
Expand All @@ -46,7 +46,7 @@ pub type TcpSocket = Socket<tcp::Socket<'static>>;

pub struct Socket<T> {
handle: SocketHandle,
shared: SharedNetwork,
shared: ManagedInterface,
_phantom: PhantomData<T>,
}

Expand All @@ -64,7 +64,7 @@ pub enum DnsError {
GetQueryResultError(dns::GetQueryResultError),
}

impl SharedNetwork {
impl ManagedInterface {
pub fn new<D: Device + ?Sized>(
config: Config,
dhcp_overrides: DhcpOverrides,
Expand All @@ -78,7 +78,7 @@ impl SharedNetwork {
let dhcp_socket = dhcpv4::Socket::new();
let dhcp_socket_handle = socket_set.add(dhcp_socket);

let mut this = SharedNetworkInner {
let mut this = ManagedInterfaceShared {
iface,
socket_set,
dns_socket_handle,
Expand All @@ -93,7 +93,7 @@ impl SharedNetwork {
}
}

fn inner(&self) -> &Rc<RefCell<SharedNetworkInner>> {
fn inner(&self) -> &Rc<RefCell<ManagedInterfaceShared>> {
&self.inner
}

Expand Down Expand Up @@ -123,6 +123,10 @@ impl SharedNetwork {
}
}

pub fn poll_at(&self, timestamp: Instant) -> Option<Instant> {
self.inner().borrow_mut().poll_at(timestamp)
}

pub fn poll_delay(&self, timestamp: Instant) -> Option<Duration> {
self.inner().borrow_mut().poll_delay(timestamp)
}
Expand Down Expand Up @@ -415,7 +419,7 @@ impl<T> Drop for Socket<T> {
}
}

impl SharedNetworkInner {
impl ManagedInterfaceShared {
fn dhcp_socket_mut(&mut self) -> &mut dhcpv4::Socket<'static> {
self.socket_set.get_mut(self.dhcp_socket_handle)
}
Expand All @@ -424,6 +428,10 @@ impl SharedNetworkInner {
self.socket_set.get_mut(self.dns_socket_handle)
}

fn poll_at(&mut self, timestamp: Instant) -> Option<Instant> {
self.iface.poll_at(timestamp, &self.socket_set)
}

fn poll_delay(&mut self, timestamp: Instant) -> Option<Duration> {
self.iface.poll_delay(timestamp, &self.socket_set)
}
Expand Down
10 changes: 10 additions & 0 deletions crates/sel4-async/time/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "sel4-async-time"
version = "0.1.0"
authors = ["Nick Spinale <[email protected]>"]
edition = "2021"
license = "BSD-2-Clause"

[dependencies]
log = "0.4.17"
pin-project = "1.1.3"
74 changes: 74 additions & 0 deletions crates/sel4-async/time/src/instant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant {
since_zero: Duration,
}

impl Instant {
pub const ZERO: Self = Self::new(Duration::ZERO);

pub const fn new(since_zero: Duration) -> Self {
Self { since_zero }
}

pub const fn since_zero(&self) -> Duration {
self.since_zero
}

pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
self.since_zero.checked_sub(earlier.since_zero)
}

pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
}

pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
self.since_zero.checked_add(duration).map(Self::new)
}

pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
self.since_zero.checked_sub(duration).map(Self::new)
}
}

impl Add<Duration> for Instant {
type Output = Instant;

fn add(self, other: Duration) -> Instant {
self.checked_add(other)
.expect("overflow when adding duration to instant")
}
}

impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, other: Duration) {
*self = *self + other;
}
}

impl Sub<Duration> for Instant {
type Output = Instant;

fn sub(self, other: Duration) -> Instant {
self.checked_sub(other)
.expect("overflow when subtracting duration from instant")
}
}

impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, other: Duration) {
*self = *self - other;
}
}

impl Sub<Instant> for Instant {
type Output = Duration;

fn sub(self, other: Instant) -> Duration {
self.checked_duration_since(other)
.expect("overflow when instant from instant")
}
}
Loading