mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-18 15:27:50 +00:00
Refactor: Move interrupt wakers/operations into separate modules
This commit is contained in:
@@ -1,14 +1,30 @@
|
||||
use crate::{interrupts, print};
|
||||
use crate::{print, task::interrupt_wake};
|
||||
use core::future::Future;
|
||||
use core::{
|
||||
pin::Pin,
|
||||
sync::atomic::Ordering,
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use futures_util::task::AtomicWaker;
|
||||
|
||||
static TICKS: AtomicU64 = AtomicU64::new(0);
|
||||
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||
|
||||
/// Called by the timer interrupt handler
|
||||
///
|
||||
/// Must not block (including spinlocks).
|
||||
pub(crate) fn tick() {
|
||||
TICKS.fetch_add(1, Ordering::Release);
|
||||
if let Some(waker) = WAKER.take() {
|
||||
interrupt_wake(waker);
|
||||
}
|
||||
}
|
||||
|
||||
fn next_tick() -> impl Future<Output = u64> {
|
||||
static NEXT_TICK: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
fn next_tick(current_tick: u64) -> impl Future<Output = u64> {
|
||||
NextTick {
|
||||
ticks: current_tick,
|
||||
ticks: NEXT_TICK.fetch_add(1, Ordering::Release),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,15 +36,8 @@ impl Future for NextTick {
|
||||
type Output = u64;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<u64> {
|
||||
let current_ticks = interrupts::TIMER_TICKS.load(Ordering::Acquire);
|
||||
|
||||
if self.ticks < current_ticks {
|
||||
self.ticks += 1;
|
||||
return Poll::Ready(self.ticks);
|
||||
}
|
||||
|
||||
interrupts::TIMER_INTERRUPT_WAKER.register(&cx.waker());
|
||||
let current_ticks = interrupts::TIMER_TICKS.load(Ordering::Acquire);
|
||||
WAKER.register(&cx.waker());
|
||||
let current_ticks = TICKS.load(Ordering::Acquire);
|
||||
if self.ticks < current_ticks {
|
||||
self.ticks += 1;
|
||||
Poll::Ready(self.ticks)
|
||||
@@ -38,11 +47,9 @@ impl Future for NextTick {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn print_ticks() {
|
||||
let mut current_ticks = interrupts::TIMER_TICKS.load(Ordering::Acquire);
|
||||
|
||||
pub async fn timer_task() {
|
||||
loop {
|
||||
current_ticks = next_tick(current_ticks).await;
|
||||
next_tick().await;
|
||||
print!(".");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user