diff --git a/Cargo.lock b/Cargo.lock index 6b3701347..80f5ddb89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -290,7 +290,7 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" dependencies = [ - "spin 0.9.8", + "spin 0.9.9", ] [[package]] @@ -656,9 +656,9 @@ dependencies = [ [[package]] name = "spin" -version = "0.9.8" +version = "0.9.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" [[package]] name = "spin" diff --git a/src/sys/ata.rs b/src/sys/ata.rs index 96763f895..c143c734a 100644 --- a/src/sys/ata.rs +++ b/src/sys/ata.rs @@ -10,7 +10,6 @@ use bit_field::BitField; use core::convert::TryInto; use core::fmt; use core::hint::spin_loop; -use lazy_static::lazy_static; use spin::Mutex; // Information Technology @@ -308,9 +307,7 @@ impl Bus { } } -lazy_static! { - pub static ref BUSES: Mutex> = Mutex::new(Vec::new()); -} +pub static BUSES: Mutex> = Mutex::new(Vec::new()); pub fn init() { { diff --git a/src/sys/gdt.rs b/src/sys/gdt.rs index 48f01f102..2609c6ed7 100644 --- a/src/sys/gdt.rs +++ b/src/sys/gdt.rs @@ -6,11 +6,11 @@ use crate::sys::x86::reg; use crate::sys::x86::seg::SegmentSelector; use bit_field::BitField; -use lazy_static::lazy_static; +use spin::LazyLock; -lazy_static! { - static ref GDT: GlobalDescriptorTable = GlobalDescriptorTable::new(&tss::TSS); -} +static GDT: LazyLock = LazyLock::new(|| + GlobalDescriptorTable::new(&tss::TSS) +); pub const SYS_CODE: SegmentSelector = SegmentSelector::new(1, 0); pub const SYS_DATA: SegmentSelector = SegmentSelector::new(2, 0); diff --git a/src/sys/idt.rs b/src/sys/idt.rs index 4b08027b6..10f391369 100644 --- a/src/sys/idt.rs +++ b/src/sys/idt.rs @@ -11,8 +11,7 @@ use crate::sys::x86; use bit_field::BitField; use core::arch::asm; -use lazy_static::lazy_static; -use spin::Mutex; +use spin::{LazyLock, Mutex}; const LEN: usize = 256; @@ -137,15 +136,13 @@ impl Entry { fn default_handler() {} -lazy_static! { - static ref IRQ_HANDLERS: Mutex<[fn(); 16]> = { - Mutex::new([default_handler; 16]) - }; +static IRQ_HANDLERS: Mutex<[fn(); 16]> = { + Mutex::new([default_handler; 16]) +}; - static ref IDT: InterruptDescriptorTable = { - InterruptDescriptorTable::new() - }; -} +static IDT: LazyLock = LazyLock::new(|| + InterruptDescriptorTable::new() +); macro_rules! irq_handler { ($handler:ident, $irq:expr) => { diff --git a/src/sys/keyboard.rs b/src/sys/keyboard.rs index c2904b19e..44d1ff2f2 100644 --- a/src/sys/keyboard.rs +++ b/src/sys/keyboard.rs @@ -9,16 +9,13 @@ use alloc::collections::vec_deque::VecDeque; use alloc::format; use alloc::string::String; use core::sync::atomic::{AtomicBool, Ordering}; -use lazy_static::lazy_static; use pc_keyboard::{ layouts, DecodedKey, Error, HandleControl, KeyCode, KeyEvent, KeyState, Keyboard, ScancodeSet1, }; use spin::Mutex; -lazy_static! { - pub static ref BUF: Mutex> = Mutex::new(VecDeque::new()); -} +pub static BUF: Mutex> = Mutex::new(VecDeque::new()); pub static KEYBOARD: Mutex> = Mutex::new(None); diff --git a/src/sys/log.rs b/src/sys/log.rs index 8ac7e60e1..934177750 100644 --- a/src/sys/log.rs +++ b/src/sys/log.rs @@ -3,12 +3,9 @@ use crate::sys::x86::int; use alloc::string::String; use core::fmt; use core::fmt::Write; -use lazy_static::lazy_static; use spin::Mutex; -lazy_static! { - static ref LOG: Mutex = Mutex::new(LogBuffer::new()); -} +static LOG: Mutex = Mutex::new(LogBuffer::new()); const LOG_SIZE: usize = 10 << 10; // 10 KB diff --git a/src/sys/net/socket/mod.rs b/src/sys/net/socket/mod.rs index a63fb34a9..5097f71aa 100644 --- a/src/sys/net/socket/mod.rs +++ b/src/sys/net/socket/mod.rs @@ -4,16 +4,13 @@ pub mod tcp; pub mod udp; use alloc::vec; -use lazy_static::lazy_static; use smoltcp::iface::SocketSet; use smoltcp::time::Duration; -use spin::Mutex; +use spin::{LazyLock, Mutex}; -lazy_static! { - pub static ref SOCKETS: Mutex> = { - Mutex::new(SocketSet::new(vec![])) - }; -} +pub static SOCKETS: LazyLock>> = LazyLock::new(|| + Mutex::new(SocketSet::new(vec![])) +); fn random_port() -> u16 { 49152 + sys::rng::get_u16() % 16384 diff --git a/src/sys/pci.rs b/src/sys/pci.rs index 7aaf9c3cd..032c0cf1c 100644 --- a/src/sys/pci.rs +++ b/src/sys/pci.rs @@ -4,7 +4,6 @@ use crate::sys::x86::port::*; use alloc::vec; use alloc::vec::Vec; use bit_field::BitField; -use lazy_static::lazy_static; use spin::Mutex; #[derive(Debug, Clone, Copy)] @@ -133,9 +132,7 @@ impl DeviceConfig { } } -lazy_static! { - pub static ref PCI_DEVICES: Mutex> = Mutex::new(vec![]); -} +pub static PCI_DEVICES: Mutex> = Mutex::new(vec![]); pub fn find_device(vendor_id: u16, device_id: u16) -> Option { for &device in PCI_DEVICES.lock().iter() { diff --git a/src/sys/process/table.rs b/src/sys/process/table.rs index 6d81fd09c..f4216a23c 100644 --- a/src/sys/process/table.rs +++ b/src/sys/process/table.rs @@ -10,7 +10,6 @@ use alloc::collections::btree_map::BTreeMap; use alloc::string::String; use core::alloc::{GlobalAlloc, Layout}; use core::sync::atomic::{AtomicUsize, Ordering}; -use lazy_static::lazy_static; use spin::RwLock; pub const MAX_PROCS: usize = 32; @@ -18,11 +17,9 @@ static PID: AtomicUsize = AtomicUsize::new(0); type ProcessTable = [Option>; MAX_PROCS]; -lazy_static! { - pub static ref PROCESS_TABLE: RwLock = { - RwLock::new([(); MAX_PROCS].map(|_| None)) - }; -} +pub static PROCESS_TABLE: RwLock = RwLock::new( + [const { None }; MAX_PROCS] +); pub fn init() { let mut table = PROCESS_TABLE.write(); diff --git a/src/sys/rng.rs b/src/sys/rng.rs index dda4dca01..ae5fb4c09 100644 --- a/src/sys/rng.rs +++ b/src/sys/rng.rs @@ -2,15 +2,12 @@ use crate::sys; use crate::sys::fs::{FileIO, IO}; -use lazy_static::lazy_static; use rand::{RngCore, SeedableRng}; use rand_hc::Hc128Rng; use sha2::{Digest, Sha256}; -use spin::Mutex; +use spin::{Once, Mutex}; -lazy_static! { - static ref RNG: Mutex = Mutex::new(Hc128Rng::from_seed([0; 32])); -} +static RNG: Once> = Once::new(); #[derive(Debug, Clone)] pub struct Random; @@ -47,7 +44,7 @@ impl FileIO for Random { } pub fn get_u64() -> u64 { - RNG.lock().next_u64() + RNG.get().expect("RNG not initialized").lock().next_u64() } pub fn get_u32() -> u32 { @@ -87,5 +84,5 @@ pub fn init() { seed = hasher.finalize().into(); } - *RNG.lock() = Hc128Rng::from_seed(seed); + RNG.call_once(|| Mutex::new(Hc128Rng::from_seed(seed))); } diff --git a/src/sys/serial.rs b/src/sys/serial.rs index a0d21235d..19818ff3c 100644 --- a/src/sys/serial.rs +++ b/src/sys/serial.rs @@ -3,22 +3,19 @@ use crate::sys::x86::int; use core::fmt; use core::fmt::Write; -use lazy_static::lazy_static; -use spin::Mutex; +use spin::{LazyLock, Mutex}; use uart_16550::SerialPort; use vte::{Params, Parser, Perform}; -lazy_static! { - pub static ref SERIAL: Mutex = Mutex::new(Serial::new(0x3F8)); - pub static ref PARSER: Mutex = Mutex::new(Parser::new()); -} +pub static SERIAL: Mutex = Mutex::new(Serial::new(0x3F8)); +pub static PARSER: LazyLock> = LazyLock::new(|| Mutex::new(Parser::new())); pub struct Serial { port: SerialPort, } impl Serial { - fn new(addr: u16) -> Self { + const fn new(addr: u16) -> Self { Self { port: unsafe { SerialPort::new(addr) } } diff --git a/src/sys/tss.rs b/src/sys/tss.rs index 21ed4a708..24afeb7b1 100644 --- a/src/sys/tss.rs +++ b/src/sys/tss.rs @@ -1,50 +1,48 @@ use crate::STACK_SIZE; use core::ptr::addr_of; -use lazy_static::lazy_static; +use spin::LazyLock; pub const DF: usize = 0; // Double fault pub const PF: usize = 1; // Page fault pub const GP: usize = 2; // General protection -lazy_static! { - pub static ref TSS: TaskStateSegment = { - let mut tss = TaskStateSegment::new(); +pub static TSS: LazyLock = LazyLock::new(|| { + let mut tss = TaskStateSegment::new(); + let addr = { + static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; + STACK_SIZE + addr_of!(STACK) as usize + }; + tss.set_kernel_stack(addr); + + #[cfg(target_arch = "x86")] + { + tss.stack[0].ss = crate::sys::gdt::SYS_DATA.bits; + } + + #[cfg(target_arch = "x86_64")] + { let addr = { static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; STACK_SIZE + addr_of!(STACK) as usize }; - tss.set_kernel_stack(addr); - - #[cfg(target_arch = "x86")] - { - tss.stack[0].ss = crate::sys::gdt::SYS_DATA.bits; - } - - #[cfg(target_arch = "x86_64")] - { - let addr = { - static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; - STACK_SIZE + addr_of!(STACK) as usize - }; - tss.set_interrupt_stack(DF, addr); - - let addr = { - static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; - STACK_SIZE + addr_of!(STACK) as usize - }; - tss.set_interrupt_stack(PF, addr); - - let addr = { - static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; - STACK_SIZE + addr_of!(STACK) as usize - }; - tss.set_interrupt_stack(GP, addr); - } - tss - }; -} + tss.set_interrupt_stack(DF, addr); + + let addr = { + static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; + STACK_SIZE + addr_of!(STACK) as usize + }; + tss.set_interrupt_stack(PF, addr); + + let addr = { + static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; + STACK_SIZE + addr_of!(STACK) as usize + }; + tss.set_interrupt_stack(GP, addr); + } + tss +}); #[cfg(target_arch = "x86")] #[repr(C)] diff --git a/src/sys/vga/writer.rs b/src/sys/vga/writer.rs index 463afb605..bcabed8c3 100644 --- a/src/sys/vga/writer.rs +++ b/src/sys/vga/writer.rs @@ -10,8 +10,7 @@ use crate::api::font::Font; use crate::sys; -use lazy_static::lazy_static; -use spin::Mutex; +use spin::{LazyLock, Mutex}; use vte::{Params, Parser, Perform}; const FG: Color = Color::DarkWhite; @@ -64,9 +63,12 @@ struct ScreenBuffer { static mut SCROLL_BUFFER: [[ScreenChar; SCREEN_WIDTH]; SCROLL_HEIGHT] = [[ScreenChar::zeroed(); SCREEN_WIDTH]; SCROLL_HEIGHT]; -lazy_static! { - pub static ref PARSER: Mutex = Mutex::new(Parser::new()); - pub static ref WRITER: Mutex = Mutex::new(Writer { +pub static PARSER: LazyLock> = LazyLock::new(|| + Mutex::new(Parser::new()) +); + +pub static WRITER: LazyLock> = LazyLock::new(|| + Mutex::new(Writer { cursor: [0; 2], writer: [0; 2], color_code: ColorCode::new(FG, BG), @@ -74,8 +76,8 @@ lazy_static! { scroll_buffer: unsafe { &mut *core::ptr::addr_of_mut!(SCROLL_BUFFER) }, scroll_reader: 0, scroll_bottom: SCREEN_HEIGHT, - }); -} + }) +); pub struct Writer { cursor: [usize; 2], // x, y diff --git a/src/usr/chess.rs b/src/usr/chess.rs index bada6c8db..4694bb8aa 100644 --- a/src/usr/chess.rs +++ b/src/usr/chess.rs @@ -11,14 +11,11 @@ use alloc::string::{String,ToString}; use alloc::sync::Arc; use alloc::vec::Vec; -use lazy_static::lazy_static; use littlewing::chess::*; use littlewing::color::*; use spin::Mutex; -lazy_static! { - static ref MOVES: Mutex> = Mutex::new(Vec::new()); -} +static MOVES: Mutex> = Mutex::new(Vec::new()); const FEN: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; const COMMANDS: [&str; 11] = [ diff --git a/src/usr/lisp/mod.rs b/src/usr/lisp/mod.rs index 39c5edfcd..cd2fd5b08 100644 --- a/src/usr/lisp/mod.rs +++ b/src/usr/lisp/mod.rs @@ -30,7 +30,6 @@ use core::cell::RefCell; use core::cmp; use core::convert::TryInto; use core::fmt; -use lazy_static::lazy_static; use spin::Mutex; // MOROS Lisp is a lisp-1 like Scheme and Clojure @@ -151,9 +150,7 @@ pub enum Err { Reason(String), } -lazy_static! { - pub static ref FUNCTIONS: Mutex> = Mutex::new(Vec::new()); -} +pub static FUNCTIONS: Mutex> = Mutex::new(Vec::new()); #[macro_export] macro_rules! ensure_length_eq {