Skip to content

Commit

Permalink
Remove resource abstraction from clocks (bytecodealliance#129)
Browse files Browse the repository at this point in the history
* wit: remove resource index from monotonic-clocks and wall-clocks, delete instance-*-clock interfaces

At this time, we don't have use cases for providing multiple wall clocks
or multiple monotonic clocks to the same component. So, according to the
principle of following preview 1's design as close as possible, we are
changing these interfaces to being provided by ambient functions, rather
than methods on a resource.

* adapter: unresourcify clocks

* host: de-resourcify clocks
  • Loading branch information
pchickey committed Apr 4, 2023
1 parent d9eb1fa commit e5b6452
Showing 1 changed file with 11 additions and 62 deletions.
73 changes: 11 additions & 62 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![allow(unused_variables)] // TODO: remove this when more things are implemented

use crate::bindings::{
exit, filesystem, instance_monotonic_clock, instance_wall_clock, monotonic_clock, network,
poll, random, streams, wall_clock,
exit, filesystem, monotonic_clock, network, poll, random, streams, wall_clock,
};
use core::cell::{Cell, RefCell, RefMut, UnsafeCell};
use core::cmp::min;
Expand Down Expand Up @@ -337,11 +336,11 @@ pub extern "C" fn clock_res_get(id: Clockid, resolution: &mut Timestamp) -> Errn
State::with(|state| {
match id {
CLOCKID_MONOTONIC => {
let res = monotonic_clock::resolution(state.instance_monotonic_clock());
let res = monotonic_clock::resolution();
*resolution = res;
}
CLOCKID_REALTIME => {
let res = wall_clock::resolution(state.instance_wall_clock());
let res = wall_clock::resolution();
*resolution = Timestamp::from(res.seconds)
.checked_mul(1_000_000_000)
.and_then(|ns| ns.checked_add(res.nanoseconds.into()))
Expand All @@ -368,10 +367,10 @@ pub unsafe extern "C" fn clock_time_get(
State::with(|state| {
match id {
CLOCKID_MONOTONIC => {
*time = monotonic_clock::now(state.instance_monotonic_clock());
*time = monotonic_clock::now();
}
CLOCKID_REALTIME => {
let res = wall_clock::now(state.instance_wall_clock());
let res = wall_clock::now();
*time = Timestamp::from(res.seconds)
.checked_mul(1_000_000_000)
.and_then(|ns| ns.checked_add(res.nanoseconds.into()))
Expand Down Expand Up @@ -1584,7 +1583,7 @@ pub unsafe extern "C" fn poll_oneoff(
};

// Subtract `now`.
let now = wall_clock::now(state.instance_wall_clock());
let now = wall_clock::now();
datetime.seconds -= now.seconds;
if datetime.nanoseconds < now.nanoseconds {
datetime.seconds -= 1;
Expand All @@ -1604,18 +1603,10 @@ pub unsafe extern "C" fn poll_oneoff(
clock.timeout
};

monotonic_clock::subscribe(
state.instance_monotonic_clock(),
timeout,
false,
)
monotonic_clock::subscribe(timeout, false)
}

CLOCKID_MONOTONIC => monotonic_clock::subscribe(
state.instance_monotonic_clock(),
clock.timeout,
absolute,
),
CLOCKID_MONOTONIC => monotonic_clock::subscribe(clock.timeout, absolute),

_ => return Err(ERRNO_INVAL),
}
Expand All @@ -1630,9 +1621,7 @@ pub unsafe extern "C" fn poll_oneoff(
// If the file descriptor isn't a stream, request a
// pollable which completes immediately so that it'll
// immediately fail.
Err(ERRNO_BADF) => {
monotonic_clock::subscribe(state.instance_monotonic_clock(), 0, false)
}
Err(ERRNO_BADF) => monotonic_clock::subscribe(0, false),
Err(e) => return Err(e),
}
}
Expand All @@ -1646,9 +1635,7 @@ pub unsafe extern "C" fn poll_oneoff(
// If the file descriptor isn't a stream, request a
// pollable which completes immediately so that it'll
// immediately fail.
Err(ERRNO_BADF) => {
monotonic_clock::subscribe(state.instance_monotonic_clock(), 0, false)
}
Err(ERRNO_BADF) => monotonic_clock::subscribe(0, false),
Err(e) => return Err(e),
}
}
Expand Down Expand Up @@ -2085,12 +2072,6 @@ struct State {
/// name that didn't fit into the caller's buffer.
dirent_cache: DirentCache,

/// The clock handle for `CLOCKID_MONOTONIC`.
instance_monotonic_clock: Cell<Option<Fd>>,

/// The clock handle for `CLOCKID_REALTIME`.
instance_wall_clock: Cell<Option<Fd>>,

/// The string `..` for use by the directory iterator.
dotdot: [UnsafeCell<u8>; 2],

Expand Down Expand Up @@ -2151,7 +2132,7 @@ const fn bump_arena_size() -> usize {
start -= size_of::<DirentCache>();

// Remove miscellaneous metadata also stored in state.
start -= 25 * size_of::<usize>();
start -= 22 * size_of::<usize>();

// Everything else is the `command_data` allocation.
start
Expand Down Expand Up @@ -2272,8 +2253,6 @@ impl State {
}),
path_data: UnsafeCell::new(MaybeUninit::uninit()),
},
instance_monotonic_clock: Cell::new(None),
instance_wall_clock: Cell::new(None),
dotdot: [UnsafeCell::new(b'.'), UnsafeCell::new(b'.')],
}));
&*ret
Expand Down Expand Up @@ -2304,36 +2283,6 @@ impl State {
RefMut::map(d, |d| d.as_mut().unwrap_or_else(|| unreachable!()))
}

/// Return a handle to the default wall clock, creating one if we
/// don't already have one.
fn instance_wall_clock(&self) -> Fd {
match self.instance_wall_clock.get() {
Some(fd) => fd,
None => self.init_instance_wall_clock(),
}
}

fn init_instance_wall_clock(&self) -> Fd {
let clock = instance_wall_clock::instance_wall_clock();
self.instance_wall_clock.set(Some(clock));
clock
}

/// Return a handle to the default monotonic clock, creating one if we
/// don't already have one.
fn instance_monotonic_clock(&self) -> Fd {
match self.instance_monotonic_clock.get() {
Some(fd) => fd,
None => self.init_instance_monotonic_clock(),
}
}

fn init_instance_monotonic_clock(&self) -> Fd {
let clock = instance_monotonic_clock::instance_monotonic_clock();
self.instance_monotonic_clock.set(Some(clock));
clock
}

fn get_environment(&self) -> &[StrTuple] {
if self.env_vars.get().is_none() {
#[link(wasm_import_module = "environment")]
Expand Down

0 comments on commit e5b6452

Please sign in to comment.