From fd59b628ea4d89099ae8ecfce01f271b2a1d9bf5 Mon Sep 17 00:00:00 2001 From: James Higgins Date: Tue, 12 Apr 2022 13:54:36 -0300 Subject: [PATCH 1/2] Add PhantomData marker to Context to make Context !Send and !Sync --- library/core/src/task/wake.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 1b7578376b42b..a4425fd234a4e 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -181,6 +181,9 @@ pub struct Context<'a> { // are contravariant while return-position lifetimes are // covariant). _marker: PhantomData &'a ()>, + // Ensure `Context` is `!Send` and `!Sync` in order to allow + // for future `!Send` and / or `!Sync` fields. + _marker2: PhantomData<*mut ()>, } impl<'a> Context<'a> { @@ -190,7 +193,7 @@ impl<'a> Context<'a> { #[must_use] #[inline] pub const fn from_waker(waker: &'a Waker) -> Self { - Context { waker, _marker: PhantomData } + Context { waker, _marker: PhantomData, _marker2: PhantomData } } /// Returns a reference to the [`Waker`] for the current task. From 257e766c0c17c1a3561a509c931cf89bce27dcf5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 2 Jan 2023 10:31:50 -0800 Subject: [PATCH 2/2] Remove test of static Context Context is no longer Sync so this doesn't work. error[E0277]: `*mut ()` cannot be shared between threads safely --> library/core/tests/task.rs:24:21 | 24 | static CONTEXT: Context<'static> = Context::from_waker(&WAKER); | ^^^^^^^^^^^^^^^^ `*mut ()` cannot be shared between threads safely | = help: within `Context<'static>`, the trait `Sync` is not implemented for `*mut ()` = note: required because it appears within the type `PhantomData<*mut ()>` = note: required because it appears within the type `Context<'static>` = note: shared static variables must have a type that implements `Sync` --- library/core/tests/task.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/library/core/tests/task.rs b/library/core/tests/task.rs index 56be30e928245..163b34c964852 100644 --- a/library/core/tests/task.rs +++ b/library/core/tests/task.rs @@ -1,4 +1,4 @@ -use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; +use core::task::{Poll, RawWaker, RawWakerVTable, Waker}; #[test] fn poll_const() { @@ -21,9 +21,5 @@ fn waker_const() { static WAKER: Waker = unsafe { Waker::from_raw(VOID_WAKER) }; - static CONTEXT: Context<'static> = Context::from_waker(&WAKER); - - static WAKER_REF: &'static Waker = CONTEXT.waker(); - - WAKER_REF.wake_by_ref(); + WAKER.wake_by_ref(); }