blog_os
blog_os copied to clipboard
Possibly remove a lot of `lazy_static`
as 1.63.0 enables const mutex: https://blog.rust-lang.org/2022/08/11/Rust-1.63.0.html#const-mutex-rwlock-condvar-initialization edit: and spin already has support for this anyways :)
Blog os doesn't use std mutexes, so this change doesn't affect blog os in any way. The spin crate which provides the Mutex blog os uses already had support for this. Some cases could already have been rewritten to not use lazy_static, but in other cases the value to put in the mutex can't be constructed using const.
Should we rewrite the remaining one to use Lazy from once_cell? It seems there is a consensus in ecosystem to move away from lazy_static! since it is going to be in standard library and works better with rustfmt and rustdoc. Since this blog is an educational content, I think it is better to use once_cell so that people can get to know once_cell from the beginning instead of learn lazy_static and then drop it and learn once_cell. @phil-opp if you would accept a PR for this, I can write it.
once_cell depends on libstd for locking, unlike lazy_static which works with spin locks too.
With https://github.com/matklad/once_cell/pull/195 being merged, is it now possible to replace lazy_static?
critical_section (used by once_cell for no_std) requires you to inplement a trait and then set it as global impl. spin (used by lazy_static for no_std) on the other hand just works without needing any configuration.
IMO it is not a problem. It adds something to learn, and solves a valid problem: using a lazy_static value inside and outside of an interrupt handler can cause a deadlock, critical section can solve that by disabling interrupts before spin-locking. And we can go even further and use a critical section based mutex, which eliminate the need for disabling interrupts manually.
@bjorn3 having seen you in many places around the rust ecosystem, I would like to thank you for all the information you're giving even on rather small issues like this. I'm always learning some new things from it.
once_cell depends on libstd for locking, unlike lazy_static which works with spin locks too.
critical_section (used by once_cell for no_std) requires you to inplement a trait and then set it as global impl. spin (used by lazy_static for no_std) on the other hand just works without needing any configuration.
generic_once_cell could be used. (bring your own mutex)
use generic_once_cell::Lazy;
use spin::Mutex;
use uart_16550::SerialPort;
pub static SERIAL0: Lazy<Mutex<()>, Mutex<SerialPort>> = Lazy::new(|| {
let mut serial_port = unsafe { SerialPort::new(0x3F8) };
serial_port.init();
Mutex::new(serial_port)
});
I found an implementation for this directly in the spin library since 0.7.0 as spin::lazy::Lazy