From 33660904545160fd7418c236fdbf673205da9c2c Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Wed, 7 Dec 2022 15:05:20 +0000 Subject: [PATCH] popover: Use thread-safe reference This reference will be modified from the screensaver thread. --- src/actors/popover.rs | 27 +++++++++++++++++++++------ src/main.rs | 4 ++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/actors/popover.rs b/src/actors/popover.rs index b2f06129..7e8616ab 100644 --- a/src/actors/popover.rs +++ b/src/actors/popover.rs @@ -11,16 +11,21 @@ but it cannot get the user-selected overlay, because it's stored in state. To solve this, overlay will be cached in the popover actor, and updated by main state every time it changes. */ +use crate::logging; +use std::borrow::BorrowMut; use super::Destination; pub mod c { use super::*; - use crate::util::c::Wrapped; - /// The mutable instance of state - pub type Actor = Wrapped; + use crate::util::c::ArcWrapped; + /// The mutable instance of state. + /// Thread-safe because this actor does not get its own event loop, + /// and therefore can't have a channel to receive messages, + /// so instead messages will be passed directly to the mutexed actor. + pub type Actor = ArcWrapped; /// It's the same because the state is a simple mutex-protected type. /// There are no channels involved. - pub type Destination = Wrapped; + pub type Destination = ArcWrapped; } pub enum Event { @@ -32,8 +37,18 @@ impl Destination for c::Destination { type Event = Event; fn send(&self, event: Self::Event) { let actor = self.clone_ref(); - let mut actor = actor.borrow_mut(); - *actor = actor.clone().handle_event(event); + let actor = actor.lock(); + match actor { + Ok(mut actor) => { + let actor = actor.borrow_mut(); + **actor = actor.clone().handle_event(event); + }, + Err(e) => log_print!( + logging::Level::Bug, + "Cannot lock popover state: {:?}", + e, + ), + } } } diff --git a/src/main.rs b/src/main.rs index 6c97c7cf..b51a6114 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ mod c { use crate::outputs::Outputs; use crate::state; use crate::submission::Submission; - use crate::util::c::Wrapped; + use crate::util::c::{ArcWrapped, Wrapped}; use crate::vkeyboard::c::ZwpVirtualKeyboardV1; /// DbusHandler* @@ -132,7 +132,7 @@ mod c { state_manager: Wrapped::new(state_manager), receiver: Wrapped::new(receiver), wayland: Box::into_raw(wayland), - popover: Wrapped::new(actors::popover::State::new(true)), + popover: ArcWrapped::new(actors::popover::State::new(true)), } }