generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 45
Next Event on Task struct #212
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
Open
dylanjwolff
wants to merge
2
commits into
awslabs:main
Choose a base branch
from
dylanjwolff:next-event-on-task
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,6 +255,132 @@ impl PartialEq for TaskSignature { | |
|
|
||
| impl Eq for TaskSignature {} | ||
|
|
||
| pub(crate) type Loc = &'static Location<'static>; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
| pub(crate) enum Event<'a> { | ||
| AtomicRead(&'a ResourceSignature, Loc), | ||
| AtomicWrite(&'a ResourceSignature, Loc), | ||
| AtomicReadWrite(&'a ResourceSignature, Loc), | ||
| BatchSemaphoreAcq(&'a ResourceSignature, Loc), | ||
| BatchSemaphoreRel(&'a ResourceSignature, Loc), | ||
| BarrierWait(&'a ResourceSignature, Loc), | ||
| CondvarWait(&'a ResourceSignature, Loc), | ||
| CondvarNotify(Loc), | ||
| Park(Loc), | ||
| Unpark(&'a TaskSignature, Loc), | ||
| ChannelSend(&'a ResourceSignature, Loc), | ||
| ChannelRecv(&'a ResourceSignature, Loc), | ||
| Spawn(&'a TaskSignature), | ||
| Yield(Loc), | ||
| Sleep(Loc), | ||
| Exit, | ||
| Join(&'a TaskSignature, Loc), | ||
| Unknown, | ||
| } | ||
|
|
||
| impl<'a> std::fmt::Display for Event<'a> { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match self { | ||
| Event::AtomicRead(_, loc) => write!(f, "AtomicRead at {}", loc), | ||
| Event::AtomicWrite(_, loc) => write!(f, "AtomicWrite at {}", loc), | ||
| Event::AtomicReadWrite(_, loc) => write!(f, "AtomicReadWrite at {}", loc), | ||
| Event::BatchSemaphoreAcq(_, loc) => write!(f, "BatchSemaphoreAcq at {}", loc), | ||
| Event::BatchSemaphoreRel(_, loc) => write!(f, "BatchSemaphoreRel at {}", loc), | ||
| Event::BarrierWait(_, loc) => write!(f, "BarrierWait at {}", loc), | ||
| Event::CondvarWait(_, loc) => write!(f, "CondvarWait at {}", loc), | ||
| Event::CondvarNotify(loc) => write!(f, "CondvarNotify at {}", loc), | ||
| Event::Park(loc) => write!(f, "Park at {}", loc), | ||
| Event::Unpark(_, loc) => write!(f, "Unpark at {}", loc), | ||
| Event::ChannelSend(_, loc) => write!(f, "ChannelSend at {}", loc), | ||
| Event::ChannelRecv(_, loc) => write!(f, "ChannelRecv at {}", loc), | ||
| Event::Spawn(sig) => write!(f, "Spawn at {}", sig.task_creation_stack.last().unwrap().0), | ||
| Event::Yield(loc) => write!(f, "Yield at {}", loc), | ||
| Event::Sleep(loc) => write!(f, "Sleep at {}", loc), | ||
| Event::Exit => write!(f, "Exit"), | ||
| Event::Join(_, loc) => write!(f, "Join at {}", loc), | ||
| Event::Unknown => write!(f, "Unknown"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a> Event<'a> { | ||
| #[track_caller] | ||
| pub(crate) fn atomic_read(sig: &'a ResourceSignature) -> Self { | ||
| Self::AtomicRead(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn atomic_write(sig: &'a ResourceSignature) -> Self { | ||
| Self::AtomicWrite(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn atomic_read_write(sig: &'a ResourceSignature) -> Self { | ||
| Self::AtomicReadWrite(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn batch_semaphore_acq(sig: &'a ResourceSignature) -> Self { | ||
| Self::BatchSemaphoreAcq(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn batch_semaphore_rel(sig: &'a ResourceSignature) -> Self { | ||
| Self::BatchSemaphoreRel(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn barrier_wait(sig: &'a ResourceSignature) -> Self { | ||
| Self::BarrierWait(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn condvar_wait(sig: &'a ResourceSignature) -> Self { | ||
| Self::CondvarWait(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn condvar_notify() -> Self { | ||
| Self::CondvarNotify(Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn park() -> Self { | ||
| Self::Park(Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn unpark(sig: &'a TaskSignature) -> Self { | ||
| Self::Unpark(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn channel_send(sig: &'a ResourceSignature) -> Self { | ||
| Self::ChannelSend(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn channel_recv(sig: &'a ResourceSignature) -> Self { | ||
| Self::ChannelRecv(sig, Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn yield_now() -> Self { | ||
| Self::Yield(Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn sleep() -> Self { | ||
| Self::Sleep(Location::caller()) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub(crate) fn join(sig: &'a TaskSignature) -> Self { | ||
| Self::Join(sig, Location::caller()) | ||
| } | ||
| } | ||
|
|
||
| /// A `Task` represents a user-level unit of concurrency. Each task has an `id` that is unique within | ||
| /// the execution, and a `state` reflecting whether the task is runnable (enabled) or not. | ||
| #[derive(Debug)] | ||
|
|
@@ -276,6 +402,8 @@ pub struct Task { | |
| // Remember whether the waker was invoked while we were running | ||
| woken: bool, | ||
|
|
||
| next_event: Event<'static>, | ||
|
|
||
| name: Option<String>, | ||
|
|
||
| local_storage: StorageMap, | ||
|
|
@@ -351,6 +479,7 @@ impl Task { | |
| waiter: None, | ||
| waker, | ||
| woken: false, | ||
| next_event: Event::Unknown, | ||
| detached: false, | ||
| park_state: ParkState::default(), | ||
| name, | ||
|
|
@@ -425,7 +554,7 @@ impl Task { | |
| let cx = &mut Context::from_waker(&waker); | ||
| while future.as_mut().poll(cx).is_pending() { | ||
| ExecutionState::with(|state| state.current_mut().sleep_unless_woken()); | ||
| thread::switch(); | ||
| thread::switch_keeping_current_event(); | ||
| } | ||
| }), | ||
| stack_size, | ||
|
|
@@ -679,6 +808,27 @@ impl Task { | |
| } | ||
| ) | ||
| } | ||
|
|
||
| /// Get the next_event with a downcast lifetime tied to self. This prevents the caller from | ||
| /// borrowing as `'static`, which is only used to avoid borrow-checker issues and spurious | ||
| /// lifetime annotations on the Task struct | ||
| /// SAFETY: The borrowed lifetime should not extend past resuming the task after a switch | ||
| pub(crate) fn next_event(&self) -> &Event<'_> { | ||
| unsafe { std::mem::transmute(&self.next_event) } | ||
dylanjwolff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Transmutes an Event reference to a `'static` lifetime to avoid borrow checker issues when | ||
| /// switching coroutines | ||
| /// SAFETY: For this to be safe, the next event must be unset with `unset_next_event` before | ||
| /// the actual lifetime of the event expires. In general, as long as the event is unset when | ||
| /// the task is resumed, this will be safe. | ||
| pub(crate) unsafe fn set_next_event(&mut self, event: Event<'_>) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SAFETY comment describing how to make it safe. Also describe why unsafe is needed |
||
| self.next_event = unsafe { std::mem::transmute::<Event<'_>, Event<'static>>(event) }; | ||
| } | ||
|
|
||
| pub(crate) fn unset_next_event(&mut self) { | ||
| self.next_event = Event::Unknown; | ||
| } | ||
| } | ||
|
|
||
| #[derive(PartialEq, Eq, Clone, Copy, Debug)] | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.