Use ManuallyDrop instead of mem::forget where appropiate

This commit is contained in:
Nadja Reitzenstein 2022-06-21 16:21:13 +02:00
parent 8a35818b4f
commit ee0593dc6f
2 changed files with 32 additions and 5 deletions

View File

@ -20,6 +20,9 @@ use std::marker::PhantomData;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tracing::field::FieldSet;
use tracing::metadata::Kind;
use tracing::{Level, Span};
#[derive(Debug)] #[derive(Debug)]
struct Spooler<'a> { struct Spooler<'a> {
@ -45,12 +48,21 @@ impl Spooler<'_> {
/// Global executor /// Global executor
pub struct Executor<'a> { pub struct Executor<'a> {
spooler: Arc<Spooler<'a>>, spooler: Arc<Spooler<'a>>,
span: Span,
} }
impl<'a, 'executor: 'a> Executor<'executor> { impl<'a, 'executor: 'a> Executor<'executor> {
pub fn new() -> Self { pub fn new() -> Self {
Executor { Executor {
spooler: Arc::new(Spooler::new()), spooler: Arc::new(Spooler::new()),
span: tracing::span!(Level::INFO, "executor"),
}
}
pub fn new_with_parent_span(parent: &Span) -> Self {
Executor {
spooler: Arc::new(Spooler::new()),
span: tracing::span!(parent: parent, Level::INFO, "executor"),
} }
} }
@ -99,7 +111,15 @@ impl<'a, 'executor: 'a> Executor<'executor> {
F: Future<Output = R> + Send + 'a, F: Future<Output = R> + Send + 'a,
R: Send + 'a, R: Send + 'a,
{ {
let span = tracing::info_span!(
parent: &self.span,
//target: "executor::spawn",
"runtime.spawn"
);
let _guard = span.enter();
let (task, handle) = LightProc::recoverable(future, self.schedule()); let (task, handle) = LightProc::recoverable(future, self.schedule());
tracing::trace!("spawning sendable task");
task.schedule(); task.schedule();
handle handle
} }
@ -109,7 +129,14 @@ impl<'a, 'executor: 'a> Executor<'executor> {
F: Future<Output = R> + 'a, F: Future<Output = R> + 'a,
R: Send + 'a, R: Send + 'a,
{ {
let span = tracing::info_span!(
parent: &self.span,
//target: "executor::spawn",
"runtime.spawn_local"
);
let _guard = span.enter();
let (task, handle) = LightProc::recoverable(future, schedule_local()); let (task, handle) = LightProc::recoverable(future, schedule_local());
tracing::trace!("spawning sendable task");
task.schedule(); task.schedule();
handle handle
} }

View File

@ -33,7 +33,7 @@ use crate::raw_proc::RawProc;
use crate::recoverable_handle::RecoverableHandle; use crate::recoverable_handle::RecoverableHandle;
use std::fmt::{self, Debug, Formatter}; use std::fmt::{self, Debug, Formatter};
use std::future::Future; use std::future::Future;
use std::mem; use std::mem::ManuallyDrop;
use std::panic::AssertUnwindSafe; use std::panic::AssertUnwindSafe;
use std::ptr::NonNull; use std::ptr::NonNull;
@ -130,9 +130,9 @@ impl LightProc {
/// ///
/// Schedule the lightweight process with passed `schedule` function at the build time. /// Schedule the lightweight process with passed `schedule` function at the build time.
pub fn schedule(self) { pub fn schedule(self) {
let ptr = self.raw_proc.as_ptr(); let this = ManuallyDrop::new(self);
let ptr = this.raw_proc.as_ptr();
let pdata = ptr as *const ProcData; let pdata = ptr as *const ProcData;
mem::forget(self);
unsafe { unsafe {
((*pdata).vtable.schedule)(ptr); ((*pdata).vtable.schedule)(ptr);
@ -144,9 +144,9 @@ impl LightProc {
/// "Running" a lightproc means ticking it once and if it doesn't complete /// "Running" a lightproc means ticking it once and if it doesn't complete
/// immediately re-scheduling it as soon as it's Waker wakes it back up. /// immediately re-scheduling it as soon as it's Waker wakes it back up.
pub fn run(self) { pub fn run(self) {
let ptr = self.raw_proc.as_ptr(); let this = ManuallyDrop::new(self);
let ptr = this.raw_proc.as_ptr();
let pdata = ptr as *const ProcData; let pdata = ptr as *const ProcData;
mem::forget(self);
unsafe { unsafe {
((*pdata).vtable.tick)(ptr); ((*pdata).vtable.tick)(ptr);