Move PIT_TICKS to context::switch.

This commit is contained in:
4lDO2
2023-07-11 15:30:54 +02:00
parent 0c90802ae0
commit bdd5c954dc
7 changed files with 26 additions and 38 deletions
+2 -7
View File
@@ -9,9 +9,6 @@ use crate::time;
use crate::{exception_stack};
//resets to 0 in context::switch()
pub static PIT_TICKS: AtomicUsize = ATOMIC_USIZE_INIT;
exception_stack!(irq_at_el0, |stack| {
match gic::irq_ack() {
30 => irq_handler_gentimer(30),
@@ -56,10 +53,8 @@ pub unsafe fn irq_handler_gentimer(irq: u32) {
timeout::trigger();
// Switch after 3 ticks (about 6.75 ms)
if PIT_TICKS.fetch_add(1, Ordering::SeqCst) >= 2 {
let _ = context::switch();
}
context::switch::tick();
trigger(irq);
GENTIMER.reload_count();
}
+2 -6
View File
@@ -1,9 +1,7 @@
use core::sync::atomic::Ordering;
use x86::tlb;
use crate::context;
use crate::device::local_apic::LOCAL_APIC;
use super::irq::PIT_TICKS;
interrupt!(wakeup, || {
LOCAL_APIC.eoi();
@@ -24,8 +22,6 @@ interrupt!(switch, || {
interrupt!(pit, || {
LOCAL_APIC.eoi();
// Switch after 3 ticks (about 6.75 ms)
if PIT_TICKS.fetch_add(1, Ordering::SeqCst) >= 2 {
let _ = context::switch();
}
// Switch after a sufficent amount of time since the last switch.
context::switch::tick();
});
+2 -8
View File
@@ -11,10 +11,6 @@ use crate::scheme::debug::{debug_input, debug_notify};
use crate::scheme::serio::serio_input;
use crate::{context, time};
//resets to 0 in context::switch()
#[thread_local]
pub static PIT_TICKS: AtomicUsize = AtomicUsize::new(0);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IrqMethod {
@@ -149,10 +145,8 @@ interrupt_stack!(pit_stack, |_stack| {
// Any better way of doing this?
timeout::trigger();
// Switch after 3 ticks (about 6.75 ms)
if PIT_TICKS.fetch_add(1, Ordering::SeqCst) >= 2 {
let _ = context::switch();
}
// Switch after a sufficent amount of time since the last switch.
context::switch::tick();
});
interrupt!(keyboard, || {
+2 -6
View File
@@ -1,9 +1,7 @@
use core::sync::atomic::Ordering;
use x86::tlb;
use crate::context;
use crate::device::local_apic::LOCAL_APIC;
use super::irq::PIT_TICKS;
interrupt!(wakeup, || {
LOCAL_APIC.eoi();
@@ -24,8 +22,6 @@ interrupt!(switch, || {
interrupt!(pit, || {
LOCAL_APIC.eoi();
// Switch after 3 ticks (about 6.75 ms)
if PIT_TICKS.fetch_add(1, Ordering::SeqCst) >= 2 {
let _ = context::switch();
}
// Switch after a sufficient amount of time since the last switch.
context::switch::tick();
});
+2 -8
View File
@@ -11,10 +11,6 @@ use crate::scheme::debug::{debug_input, debug_notify};
use crate::scheme::serio::serio_input;
use crate::{context, time};
//resets to 0 in context::switch()
#[thread_local]
pub static PIT_TICKS: AtomicUsize = AtomicUsize::new(0);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IrqMethod {
@@ -149,10 +145,8 @@ interrupt_stack!(pit_stack, |_stack| {
// Any better way of doing this?
timeout::trigger();
// Switch after 3 ticks (about 6.75 ms)
if PIT_TICKS.fetch_add(1, Ordering::SeqCst) >= 2 {
let _ = context::switch();
}
// Switch after a sufficient amount of time since the last switch.
context::switch::tick();
});
interrupt!(keyboard, || {
+1 -1
View File
@@ -34,7 +34,7 @@ pub mod context;
mod list;
/// Context switch function
mod switch;
pub mod switch;
/// File struct - defines a scheme and a file number
pub mod file;
+15 -2
View File
@@ -10,7 +10,6 @@ use crate::context::signal::signal_handler;
use crate::context::{arch, contexts, Context, CONTEXT_ID};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::gdt;
use crate::interrupt::irq::PIT_TICKS;
use crate::interrupt;
use crate::ptrace;
use crate::time;
@@ -91,6 +90,20 @@ struct SwitchResult {
#[thread_local]
static SWITCH_RESULT: Cell<Option<SwitchResult>> = Cell::new(None);
//resets to 0 in context::switch()
#[thread_local]
pub static PIT_TICKS: Cell<usize> = Cell::new(0);
pub fn tick() {
let new_ticks = PIT_TICKS.get() + 1;
PIT_TICKS.set(new_ticks);
// Switch after 3 ticks (about 6.75 ms)
if new_ticks >= 3 {
let _ = unsafe { switch() };
}
}
pub unsafe extern "C" fn switch_finish_hook() {
if let Some(SwitchResult { prev_lock, next_lock }) = SWITCH_RESULT.take() {
prev_lock.force_write_unlock();
@@ -110,7 +123,7 @@ pub unsafe extern "C" fn switch_finish_hook() {
pub unsafe fn switch() -> bool {
// TODO: Better memory orderings?
//set PIT Interrupt counter to 0, giving each process same amount of PIT ticks
let _ticks = PIT_TICKS.swap(0, Ordering::SeqCst);
PIT_TICKS.set(0);
// Set the global lock to avoid the unsafe operations below from causing issues
while arch::CONTEXT_SWITCH_LOCK.compare_exchange_weak(false, true, Ordering::SeqCst, Ordering::Relaxed).is_err() {