blog_os icon indicating copy to clipboard operation
blog_os copied to clipboard

Possibly remove a lot of `lazy_static`

Open hecatia-elegua opened this issue 3 years ago • 9 comments
trafficstars

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 :)

hecatia-elegua avatar Aug 21 '22 09:08 hecatia-elegua

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.

bjorn3 avatar Aug 21 '22 10:08 bjorn3

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.

HKalbasi avatar Sep 23 '22 10:09 HKalbasi

once_cell depends on libstd for locking, unlike lazy_static which works with spin locks too.

bjorn3 avatar Sep 23 '22 10:09 bjorn3

With https://github.com/matklad/once_cell/pull/195 being merged, is it now possible to replace lazy_static?

HKalbasi avatar Oct 22 '22 21:10 HKalbasi

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.

bjorn3 avatar Oct 22 '22 21:10 bjorn3

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.

HKalbasi avatar Oct 23 '22 07:10 HKalbasi

@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.

hecatia-elegua avatar Oct 27 '22 16:10 hecatia-elegua

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)
});

zefr0x avatar Jan 28 '24 15:01 zefr0x

I found an implementation for this directly in the spin library since 0.7.0 as spin::lazy::Lazy

zefr0x avatar Feb 12 '24 21:02 zefr0x