-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possibly remove a lot of lazy_static
#1132
Comments
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 |
once_cell depends on libstd for locking, unlike lazy_static which works with spin locks too. |
With matklad/once_cell#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. |
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 |
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 :)
The text was updated successfully, but these errors were encountered: