Skip to content

Recently stabilised Waker::data() #28

Description

@midnightveil

Hi, was reading your code as a nice little reference, and I think the extract_mask() function of exec.rs could be replaced with the 1.83 stabilisation of Waker::data().

lilos/os/src/exec.rs

Lines 203 to 246 in 7fc6c64

/// Exploits our known Waker structure to extract the notification mask from a
/// Waker.
///
/// If this is applied to a Waker that isn't from this executor (specifically,
/// one not generated by `waker_for_task`), this will cause spurious and almost
/// certainly incorrect wakeups. Currently I don't feel like that risk is great
/// enough to mark this unsafe -- it can't violate *memory* safety for certain.
///
/// In practice this function compiles down to a single inlined load
/// instruction.
fn extract_mask(waker: &Waker) -> usize {
// Determine whether the pointer member comes first or second within the
// representation of RawWaker. This is currently compile-time simplified
// and goes away.
//
// Safety: we are using `transmute` to inspect the raw composition of a
// Waker. That direction is safe -- it's a fancy version of casting a
// pointer to an integer. Transmuting the _other_ direction would be very
// unsafe.
let ptr_first = unsafe {
let (cell0, _) = mem::transmute::<Waker, (usize, usize)>(
Waker::from_raw(RawWaker::new(
1234 as *const (),
&VTABLE,
))
);
cell0 == 1234usize
};
let waker: *const Waker = waker;
// Safety: at the moment, `Waker` consists exactly of a `*const ()` and a
// `&'static RawWakerVTable` (or equivalent pointer), and this is unlikely
// to change. We've already verified above that we can find the parameter
// word, which is what we care about. Extracting it cannot violate memory
// safety, since we're just reading initialized memory.
unsafe {
let parts = &*(waker as *const (usize, usize));
if ptr_first {
parts.0
} else {
parts.1
}
}
}

https://dev-doc.rust-lang.org/std/task/struct.Waker.html#method.data

Possibly not 100% desired since it's quite new, but it'd be nice to use if possible. (unfortunately, cfg-version gating seems to still be nightly only...)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions