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::sync::Arc;
use std::time::Duration;
use tracing::field::FieldSet;
use tracing::metadata::Kind;
use tracing::{Level, Span};
#[derive(Debug)]
struct Spooler<'a> {
@ -45,12 +48,21 @@ impl Spooler<'_> {
/// Global executor
pub struct Executor<'a> {
spooler: Arc<Spooler<'a>>,
span: Span,
}
impl<'a, 'executor: 'a> Executor<'executor> {
pub fn new() -> Self {
Executor {
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,
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());
tracing::trace!("spawning sendable task");
task.schedule();
handle
}
@ -109,7 +129,14 @@ impl<'a, 'executor: 'a> Executor<'executor> {
F: Future<Output = R> + '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());
tracing::trace!("spawning sendable task");
task.schedule();
handle
}

View File

@ -33,7 +33,7 @@ use crate::raw_proc::RawProc;
use crate::recoverable_handle::RecoverableHandle;
use std::fmt::{self, Debug, Formatter};
use std::future::Future;
use std::mem;
use std::mem::ManuallyDrop;
use std::panic::AssertUnwindSafe;
use std::ptr::NonNull;
@ -130,9 +130,9 @@ impl LightProc {
///
/// Schedule the lightweight process with passed `schedule` function at the build time.
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;
mem::forget(self);
unsafe {
((*pdata).vtable.schedule)(ptr);
@ -144,9 +144,9 @@ impl LightProc {
/// "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.
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;
mem::forget(self);
unsafe {
((*pdata).vtable.tick)(ptr);