Skip to content

Commit

Permalink
Rollup merge of rust-lang#129919 - kevinmehall:waker-getters, r=dtolnay
Browse files Browse the repository at this point in the history
Stabilize `waker_getters`

Tracking issue: rust-lang#96992

FCP completed on the tracking issue a while ago. It's not clear whether the libs-api team wanted the `RawWaker` methods moved to `Waker` or went back to the current API after further discussion. `@Amanieu` [wrote "This is just waiting for someone to submit a stabilization PR."](rust-lang#96992 (comment)) so I'm doing just that in hopes of nudging this along.

Edit: Moved the `data` and `vtable` methods from `RawWaker` to `Waker` and added `Waker::new` per rust-lang#96992 (comment)

```rs
impl Waker {
  pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self;
  pub fn data(&self) -> *const ();
  pub fn vtable(&self) -> &'static RawWakerVTable;
}
```

Closes rust-lang#96992
  • Loading branch information
matthiaskrgr authored Sep 5, 2024
2 parents 3d2a91f + 851f5b6 commit 181dc26
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 31 deletions.
103 changes: 79 additions & 24 deletions 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 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 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

0 comments on commit 181dc26

Please sign in to comment.