Skip to content

Commit b9763a4

Browse files
authored
Merge pull request #142 from lvgl/safe-init
Make `init()` safe again, remove papercuts re: (re/de)initialization
2 parents a5dde98 + 2d47307 commit b9763a4

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

lvgl/src/input_device/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,6 @@ mod test {
221221
EncoderInputData::Press.pressed().once()
222222
}
223223

224-
let _encoder = Encoder::register(|| read_encoder_device(), &display).unwrap();
224+
let _encoder = Encoder::register(read_encoder_device, &display).unwrap();
225225
}
226226
}

lvgl/src/input_device/pointer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,6 @@ mod test {
228228
.once()
229229
}
230230

231-
let _touch_screen = Pointer::register(|| read_touchpad_device(), &display).unwrap();
231+
let _touch_screen = Pointer::register(read_touchpad_device, &display).unwrap();
232232
}
233233
}

lvgl/src/lib.rs

+23-42
Original file line numberDiff line numberDiff line change
@@ -72,72 +72,53 @@ pub mod widgets;
7272
#[cfg(feature = "rust_timer")]
7373
pub mod timer;
7474

75-
/*
76-
struct RunOnce(AtomicBool);
77-
78-
impl RunOnce {
79-
const fn new() -> Self {
80-
Self(AtomicBool::new(false))
81-
}
82-
83-
fn swap_and_check(&self) -> bool {
84-
self.0
85-
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
86-
.is_ok()
87-
}
88-
}
89-
*/
90-
9175
#[cfg(feature = "unsafe_no_autoinit")]
92-
static LVGL_INITIALIZED: RunOnce = RunOnce::new();
76+
static mut IS_INIT: bool = false;
77+
#[cfg(not(feature = "unsafe_no_autoinit"))]
78+
static mut IS_INIT: bool = true;
9379

94-
/// Initializes LVGL. Call at the start of the program.
95-
#[cfg(feature = "unsafe_no_autoinit")]
80+
/// Initializes LVGL. Call at the start of the program, or after safely
81+
/// deinitializing with `deinit()`.
9682
pub fn init() {
97-
if LVGL_INITIALIZED.swap_and_check() {
98-
unsafe {
83+
unsafe {
84+
if !IS_INIT {
9985
lvgl_sys::lv_init();
86+
IS_INIT = true;
10087
}
10188
}
10289
}
10390

104-
#[cfg(not(feature = "unsafe_no_autoinit"))]
105-
#[ctor::ctor]
106-
fn once_init() {
91+
/// Uninitializes LVGL. Make sure to reinitialize LVGL with `init()` before
92+
/// accessing its functionality
93+
///
94+
/// # Safety
95+
///
96+
/// After calling, ensure existing LVGL-related values are not accessed even if
97+
/// LVGL is reinitialized.
98+
pub unsafe fn deinit() {
10799
unsafe {
108-
lvgl_sys::lv_init();
100+
if IS_INIT {
101+
lvgl_sys::lv_deinit();
102+
IS_INIT = false;
103+
}
109104
}
110105
}
111106

112-
/// Initializes LVGL.
113-
///
114-
/// # Safety
115-
///
116-
/// Unless `unsafe_no_autoinit` is enabled, do not call this function without
117-
/// first calling `deinit()` and dropping all old values.
118107
#[cfg(not(feature = "unsafe_no_autoinit"))]
119-
pub unsafe fn init() {
108+
#[ctor::ctor]
109+
fn once_init() {
120110
unsafe {
121111
lvgl_sys::lv_init();
122112
}
123113
}
124114

125-
/// Uninitializes LVGL. Make sure to reinitialize it before reusing it.
126-
///
127-
/// # Safety
128-
///
129-
/// This function should not be called if LVGL is already uninitialized.
130-
pub unsafe fn deinit() {
131-
unsafe { lvgl_sys::lv_deinit() }
132-
}
133-
134115
#[cfg(test)]
135116
pub(crate) mod tests {
136117
use crate::display::{Display, DrawBuffer};
137118

138119
pub(crate) fn initialize_test(buf: bool) {
139120
unsafe { crate::deinit() };
140-
unsafe { crate::init() };
121+
crate::init();
141122
if buf {
142123
const REFRESH_BUFFER_SIZE: usize = 240 * 240 / 10;
143124
let buffer = DrawBuffer::<REFRESH_BUFFER_SIZE>::default();

0 commit comments

Comments
 (0)