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

Stabilize waker_getters #129919

Merged
merged 3 commits into from
Sep 5, 2024
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
103 changes: 79 additions & 24 deletions library/core/src/task/wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ impl RawWaker {
RawWaker { data, vtable }
}

/// Gets the `data` pointer used to create this `RawWaker`.
#[inline]
#[must_use]
#[unstable(feature = "waker_getters", issue = "96992")]
pub fn data(&self) -> *const () {
self.data
}

/// Gets the `vtable` pointer used to create this `RawWaker`.
#[inline]
#[must_use]
#[unstable(feature = "waker_getters", issue = "96992")]
pub fn vtable(&self) -> &'static RawWakerVTable {
self.vtable
}

#[unstable(feature = "noop_waker", issue = "98286")]
const NOOP: RawWaker = {
const VTABLE: RawWakerVTable = RawWakerVTable::new(
Expand Down Expand Up @@ -509,6 +493,37 @@ impl Waker {
a_data == b_data && ptr::eq(a_vtable, b_vtable)
}

/// Creates a new `Waker` from the provided `data` pointer and `vtable`.
///
/// The `data` pointer can be used to store arbitrary data as required
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
/// that is associated with the task.
/// The value of this pointer will get passed to all functions that are part
/// of the `vtable` as the first parameter.
///
/// It is important to consider that the `data` pointer must point to a
/// thread safe type such as an `Arc`.
///
/// The `vtable` customizes the behavior of a `Waker`. For each operation
/// on the `Waker`, the associated function in the `vtable` will be called.
///
/// # Safety
///
/// The behavior of the returned `Waker` is undefined if the contract defined
/// in [`RawWakerVTable`]'s documentation is not upheld.
///
/// (Authors wishing to avoid unsafe code may implement the [`Wake`] trait instead, at the
/// cost of a required heap allocation.)
///
/// [`Wake`]: ../../alloc/task/trait.Wake.html
#[inline]
#[must_use]
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self {
Waker { waker: RawWaker { data, vtable } }
}

/// Creates a new `Waker` from [`RawWaker`].
///
/// # Safety
Expand Down Expand Up @@ -565,12 +580,20 @@ impl Waker {
WAKER
}

/// Gets a reference to the underlying [`RawWaker`].
/// Gets the `data` pointer used to create this `Waker`.
#[inline]
#[must_use]
#[unstable(feature = "waker_getters", issue = "96992")]
pub fn as_raw(&self) -> &RawWaker {
&self.waker
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
pub fn data(&self) -> *const () {
self.waker.data
}

/// Gets the `vtable` pointer used to create this `Waker`.
#[inline]
#[must_use]
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
pub fn vtable(&self) -> &'static RawWakerVTable {
self.waker.vtable
}
}

Expand Down Expand Up @@ -778,6 +801,30 @@ impl LocalWaker {
a_data == b_data && ptr::eq(a_vtable, b_vtable)
}

/// Creates a new `LocalWaker` from the provided `data` pointer and `vtable`.
///
/// The `data` pointer can be used to store arbitrary data as required
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
/// that is associated with the task.
/// The value of this pointer will get passed to all functions that are part
/// of the `vtable` as the first parameter.
///
/// The `vtable` customizes the behavior of a `LocalWaker`. For each
/// operation on the `LocalWaker`, the associated function in the `vtable`
/// will be called.
///
/// # Safety
///
/// The behavior of the returned `Waker` is undefined if the contract defined
/// in [`RawWakerVTable`]'s documentation is not upheld.
///
#[inline]
#[must_use]
#[unstable(feature = "local_waker", issue = "118959")]
pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self {
LocalWaker { waker: RawWaker { data, vtable } }
}

/// Creates a new `LocalWaker` from [`RawWaker`].
///
/// The behavior of the returned `LocalWaker` is undefined if the contract defined
Expand Down Expand Up @@ -831,12 +878,20 @@ impl LocalWaker {
WAKER
}

/// Gets a reference to the underlying [`RawWaker`].
/// Gets the `data` pointer used to create this `LocalWaker`.
#[inline]
#[must_use]
#[unstable(feature = "waker_getters", issue = "96992")]
pub fn as_raw(&self) -> &RawWaker {
&self.waker
#[unstable(feature = "local_waker", issue = "118959")]
pub fn data(&self) -> *const () {
self.waker.data
}

/// Gets the `vtable` pointer used to create this `LocalWaker`.
#[inline]
#[must_use]
#[unstable(feature = "local_waker", issue = "118959")]
pub fn vtable(&self) -> &'static RawWakerVTable {
self.waker.vtable
}
}
#[unstable(feature = "local_waker", issue = "118959")]
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
#![feature(unsize)]
#![feature(unsized_tuple_coercion)]
#![feature(unwrap_infallible)]
#![feature(waker_getters)]
// tidy-alphabetical-end
#![allow(internal_features)]
#![deny(fuzzy_provenance_casts)]
Expand Down
11 changes: 5 additions & 6 deletions library/core/tests/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use std::task::{RawWaker, RawWakerVTable, Waker};
#[test]
fn test_waker_getters() {
let raw_waker = RawWaker::new(ptr::without_provenance_mut(42usize), &WAKER_VTABLE);
assert_eq!(raw_waker.data() as usize, 42);
assert!(ptr::eq(raw_waker.vtable(), &WAKER_VTABLE));

let waker = unsafe { Waker::from_raw(raw_waker) };
assert_eq!(waker.data() as usize, 42);
assert!(ptr::eq(waker.vtable(), &WAKER_VTABLE));

let waker2 = waker.clone();
let raw_waker2 = waker2.as_raw();
assert_eq!(raw_waker2.data() as usize, 43);
assert!(ptr::eq(raw_waker2.vtable(), &WAKER_VTABLE));
assert_eq!(waker2.data() as usize, 43);
assert!(ptr::eq(waker2.vtable(), &WAKER_VTABLE));
}

static WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
Expand Down
Loading