Explorar o código

runtime: remove unused ExecutionContext and Stack

Simplify code. No functional changes.

Signed-off-by: greatbridf <greatbridf@icloud.com>
greatbridf hai 1 semana
pai
achega
19821e5aaa

+ 0 - 51
crates/eonix_runtime/src/context.rs

@@ -1,51 +0,0 @@
-use core::{cell::UnsafeCell, mem::transmute};
-use eonix_hal::context::TaskContext;
-use eonix_hal::traits::context::RawTaskContext;
-
-#[derive(Debug)]
-pub struct ExecutionContext(UnsafeCell<TaskContext>);
-
-unsafe impl Sync for ExecutionContext {}
-
-impl ExecutionContext {
-    pub const fn new() -> Self {
-        Self(UnsafeCell::new(TaskContext::new()))
-    }
-
-    pub fn set_ip(&mut self, ip: usize) {
-        let Self(context) = self;
-        context.get_mut().set_program_counter(ip);
-    }
-
-    pub fn set_sp(&mut self, sp: usize) {
-        let Self(context) = self;
-        context.get_mut().set_stack_pointer(sp);
-    }
-
-    pub fn set_interrupt(&mut self, is_enabled: bool) {
-        let Self(context) = self;
-        context.get_mut().set_interrupt_enabled(is_enabled);
-    }
-
-    pub fn call1<T>(&mut self, func: unsafe extern "C" fn(T) -> !, arg: usize) {
-        let Self(context) = self;
-        context
-            .get_mut()
-            .call(unsafe { transmute(func as *mut ()) }, arg);
-    }
-
-    pub fn switch_to(&self, to: &Self) {
-        let Self(from_ctx) = self;
-        let Self(to_ctx) = to;
-        unsafe {
-            TaskContext::switch(&mut *from_ctx.get(), &mut *to_ctx.get());
-        }
-    }
-
-    pub fn switch_noreturn(&self) -> ! {
-        let Self(to_ctx) = self;
-        unsafe {
-            TaskContext::switch_to_noreturn(&mut *to_ctx.get());
-        }
-    }
-}

+ 70 - 18
crates/eonix_runtime/src/executor.rs

@@ -1,27 +1,34 @@
-// mod builder;
-mod output_handle;
-mod stack;
-
-use alloc::{
-    boxed::Box,
-    sync::{Arc, Weak},
-};
-use core::{
-    marker::PhantomData,
-    pin::Pin,
-    task::{Context, Poll},
-};
-use eonix_sync::Spin;
+use alloc::boxed::Box;
+use alloc::sync::{Arc, Weak};
+use core::marker::PhantomData;
+use core::pin::Pin;
+use core::task::{Context, Poll, Waker};
 
-pub use output_handle::OutputHandle;
-pub use stack::Stack;
+use eonix_sync::Spin;
 
 /// An `Executor` executes a Future object in a separate thread of execution.
 ///
-/// When the Future is finished, the `Executor` will call the `OutputHandle` to commit the output.
-/// Then the `Executor` will release the resources associated with the Future.
+/// When the Future is finished, the `Executor` will call the `OutputHandle` to
+/// commit the output.  Then the `Executor` will release the resources
+/// associated with the Future.
 pub struct Executor(Option<Pin<Box<dyn TypeErasedExecutor>>>);
 
+enum OutputState<Output>
+where
+    Output: Send,
+{
+    Waiting(Option<Waker>),
+    Finished(Option<Output>),
+    TakenOut,
+}
+
+pub struct OutputHandle<Output>
+where
+    Output: Send,
+{
+    inner: OutputState<Output>,
+}
+
 trait TypeErasedExecutor: Send {
     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>;
 }
@@ -92,3 +99,48 @@ impl Executor {
         }
     }
 }
+
+impl<Output> OutputHandle<Output>
+where
+    Output: Send,
+{
+    pub fn new() -> Arc<Spin<Self>> {
+        Arc::new(Spin::new(Self {
+            inner: OutputState::Waiting(None),
+        }))
+    }
+
+    pub fn try_resolve(&mut self) -> Option<Output> {
+        let output = match &mut self.inner {
+            OutputState::Waiting(_) => return None,
+            OutputState::Finished(output) => output.take(),
+            OutputState::TakenOut => panic!("Output already taken out"),
+        };
+
+        self.inner = OutputState::TakenOut;
+        if let Some(output) = output {
+            Some(output)
+        } else {
+            unreachable!("Output should be present")
+        }
+    }
+
+    pub fn register_waiter(&mut self, waker: Waker) {
+        if let OutputState::Waiting(inner_waker) = &mut self.inner {
+            inner_waker.replace(waker);
+        } else {
+            panic!("Output is not waiting");
+        }
+    }
+
+    pub fn commit_output(&mut self, output: Output) {
+        if let OutputState::Waiting(inner_waker) = &mut self.inner {
+            if let Some(waker) = inner_waker.take() {
+                waker.wake();
+            }
+            self.inner = OutputState::Finished(Some(output));
+        } else {
+            panic!("Output is not waiting");
+        }
+    }
+}

+ 0 - 63
crates/eonix_runtime/src/executor/builder.rs

@@ -1,63 +0,0 @@
-use super::{Executor, OutputHandle, RealExecutor, Stack};
-use crate::context::ExecutionContext;
-use alloc::{boxed::Box, sync::Arc};
-use core::{pin::Pin, sync::atomic::AtomicBool};
-use eonix_sync::Spin;
-
-pub struct ExecutorBuilder<S, R> {
-    stack: Option<S>,
-    runnable: Option<R>,
-}
-
-impl<S, R> ExecutorBuilder<S, R>
-where
-    S: Stack,
-    R::Output: Send,
-{
-    pub fn new() -> Self {
-        Self {
-            stack: None,
-            runnable: None,
-        }
-    }
-
-    pub fn stack(mut self, stack: S) -> Self {
-        self.stack.replace(stack);
-        self
-    }
-
-    pub fn runnable(mut self, runnable: R) -> Self {
-        self.runnable.replace(runnable);
-        self
-    }
-
-    pub fn build(
-        mut self,
-    ) -> (
-        Pin<Box<impl Executor>>,
-        ExecutionContext,
-        Arc<Spin<OutputHandle<R::Output>>>,
-    ) {
-        let stack = self.stack.take().expect("Stack is required");
-        let runnable = self.runnable.take().expect("Runnable is required");
-
-        let mut execution_context = ExecutionContext::new();
-        let output_handle = OutputHandle::new();
-
-        execution_context.set_sp(stack.get_bottom().addr().get() as _);
-
-        let executor = Box::pin(RealExecutor {
-            _stack: stack,
-            runnable,
-            output_handle: Arc::downgrade(&output_handle),
-            finished: AtomicBool::new(false),
-        });
-
-        execution_context.call1(
-            RealExecutor::<S, R>::execute,
-            executor.as_ref().get_ref() as *const _ as usize,
-        );
-
-        (executor, execution_context, output_handle)
-    }
-}

+ 0 - 64
crates/eonix_runtime/src/executor/output_handle.rs

@@ -1,64 +0,0 @@
-use alloc::sync::Arc;
-use core::task::Waker;
-use eonix_sync::Spin;
-
-enum OutputState<Output>
-where
-    Output: Send,
-{
-    Waiting(Option<Waker>),
-    Finished(Option<Output>),
-    TakenOut,
-}
-
-pub struct OutputHandle<Output>
-where
-    Output: Send,
-{
-    inner: OutputState<Output>,
-}
-
-impl<Output> OutputHandle<Output>
-where
-    Output: Send,
-{
-    pub fn new() -> Arc<Spin<Self>> {
-        Arc::new(Spin::new(Self {
-            inner: OutputState::Waiting(None),
-        }))
-    }
-
-    pub fn try_resolve(&mut self) -> Option<Output> {
-        let output = match &mut self.inner {
-            OutputState::Waiting(_) => return None,
-            OutputState::Finished(output) => output.take(),
-            OutputState::TakenOut => panic!("Output already taken out"),
-        };
-
-        self.inner = OutputState::TakenOut;
-        if let Some(output) = output {
-            Some(output)
-        } else {
-            unreachable!("Output should be present")
-        }
-    }
-
-    pub fn register_waiter(&mut self, waker: Waker) {
-        if let OutputState::Waiting(inner_waker) = &mut self.inner {
-            inner_waker.replace(waker);
-        } else {
-            panic!("Output is not waiting");
-        }
-    }
-
-    pub fn commit_output(&mut self, output: Output) {
-        if let OutputState::Waiting(inner_waker) = &mut self.inner {
-            if let Some(waker) = inner_waker.take() {
-                waker.wake();
-            }
-            self.inner = OutputState::Finished(Some(output));
-        } else {
-            panic!("Output is not waiting");
-        }
-    }
-}

+ 0 - 6
crates/eonix_runtime/src/executor/stack.rs

@@ -1,6 +0,0 @@
-use core::ptr::NonNull;
-
-pub trait Stack: Sized + Send {
-    fn new() -> Self;
-    fn get_bottom(&self) -> NonNull<()>;
-}

+ 0 - 1
crates/eonix_runtime/src/lib.rs

@@ -1,6 +1,5 @@
 #![no_std]
 
-pub mod context;
 pub mod executor;
 mod ready_queue;
 pub mod scheduler;

+ 0 - 1
src/kernel/task.rs

@@ -99,7 +99,6 @@ where
     use eonix_hal::traits::trap::{RawTrapContext, TrapReturn, TrapType};
     use eonix_hal::trap::TrapContext;
     use eonix_preempt::assert_preempt_enabled;
-    use eonix_runtime::executor::Stack;
     use eonix_runtime::task::Task;
     use thread::wait_for_wakeups;
 

+ 1 - 9
src/kernel/task/kernel_stack.rs

@@ -1,7 +1,5 @@
 use core::ptr::NonNull;
 
-use eonix_runtime::executor::Stack;
-
 use crate::kernel::mem::FolioOwned;
 
 #[derive(Debug)]
@@ -19,14 +17,8 @@ impl KernelStack {
             folio: FolioOwned::alloc_order(Self::KERNEL_STACK_ORDER),
         }
     }
-}
-
-impl Stack for KernelStack {
-    fn new() -> Self {
-        Self::new()
-    }
 
-    fn get_bottom(&self) -> NonNull<()> {
+    pub fn get_bottom(&self) -> NonNull<()> {
         let ptr = self.folio.get_bytes_ptr();
         let len = ptr.len();
 

+ 0 - 1
src/lib.rs

@@ -37,7 +37,6 @@ use eonix_hal::traits::context::RawTaskContext;
 use eonix_hal::traits::trap::IrqState;
 use eonix_hal::trap::disable_irqs_save;
 use eonix_mm::address::PRange;
-use eonix_runtime::executor::Stack;
 use eonix_runtime::scheduler::RUNTIME;
 use kernel::mem::GlobalPageAlloc;
 use kernel::task::{KernelStack, ProcessList, ProgramLoader};