Skip to content

Commit 19d5108

Browse files
committed
refactor(thread): 使用标准库方法优化栈分配计算
1 parent 732b1e0 commit 19d5108

File tree

5 files changed

+11
-19
lines changed

5 files changed

+11
-19
lines changed

kernel/src/drivers/device.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ impl OldDevice {
5252
}
5353
}
5454

55+
#[cfg(test)]
56+
pub fn null() -> Self {
57+
Self::new_auto_assign(
58+
String::from("null"),
59+
DeviceInner::Char(Arc::new(crate::drivers::TestDevice)),
60+
)
61+
}
62+
5563
pub fn device_type(&self) -> DeviceType {
5664
self.shared_ops().device_type()
5765
}

kernel/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use proka_kernel::{libs::time::rtc, output::console::CONSOLE, BASE_REVISION};
2525
/* The Kernel main code */
2626
// The normal one
2727
#[unsafe(no_mangle)]
28-
2928
pub extern "C" fn kernel_main() -> ! {
3029
// Check is limine version supported
3130
assert!(BASE_REVISION.is_supported(), "Limine version not supported");
@@ -64,7 +63,7 @@ pub extern "C" fn kernel_main() -> ! {
6463
use proka_kernel::output::console::BITFONT_CURSOR_VISIBLE;
6564
static TICKS: AtomicU64 = AtomicU64::new(0);
6665
let t = TICKS.fetch_add(1, Ordering::Relaxed);
67-
if t > 0 && t % 20 == 0 {
66+
if t > 0 && t.is_multiple_of(20) {
6867
unsafe {
6968
let current = BITFONT_CURSOR_VISIBLE.load(Ordering::Relaxed);
7069
BITFONT_CURSOR_VISIBLE.store(!current, Ordering::Relaxed);

kernel/src/output/dual.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ macro_rules! print {
88
{
99
$crate::output::dual::_dual_print_serial(format_args!($($arg)*));
1010
// Always print to console
11-
$crate::output::dual::_dual_print_console(format_args!($($arg)*))
11+
$crate::output::dual::_dual_print_console(format_args!($($arg)*));
1212
}
1313
};
1414
}

kernel/src/process/scheduler.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//! This module provides the integration between the thread scheduler
44
//! and the kernel's interrupt system.
55
6-
use crate::println;
7-
86
use super::thread::{self, Context, Tid};
97
use spin::Mutex;
108

kernel/src/process/thread.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use alloc::vec::Vec;
1111
use x86_64::PhysAddr;
1212

13-
use crate::println;
14-
1513
/// Thread ID type
1614
1715
pub type Tid = u16;
@@ -603,7 +601,7 @@ fn allocate_kernel_stack(size: usize) -> (usize, PhysAddr, usize) {
603601
use crate::memory::frame::FRAME_ALLOCATOR;
604602
use crate::memory::paging::phys_to_virt;
605603

606-
let pages = (size + 4095) / 4096;
604+
let pages = size.div_ceil(4096);
607605

608606
let frame = FRAME_ALLOCATOR
609607
.allocate_contiguous(pages)
@@ -647,15 +645,4 @@ mod tests {
647645
assert_eq!(queue.dequeue(), Some(3)); // priority 20
648646
assert_eq!(queue.dequeue(), None);
649647
}
650-
651-
#[test_case]
652-
fn test_thread_state() {
653-
let stack_info = (0xFFFF800000000000usize, PhysAddr::new(0), 1usize);
654-
let tcb = ThreadControlBlock::new_kernel(1, 10, idle_thread, stack_info);
655-
656-
assert_eq!(tcb.tid, 1);
657-
assert_eq!(tcb.priority, 10);
658-
assert!(tcb.is_runnable());
659-
assert!(!tcb.is_blocked());
660-
}
661648
}

0 commit comments

Comments
 (0)