feat: integrate arc-swap for improved concurrency in CoreManager and Hotkey(onMac) management

This commit is contained in:
Tunglies
2025-11-03 12:32:36 +08:00
parent 54039c2448
commit 4babc72754
9 changed files with 84 additions and 55 deletions
+4 -4
View File
@@ -44,15 +44,15 @@ impl CoreManager {
fn should_update_config(&self) -> Result<bool> {
let now = Instant::now();
let mut last = self.last_update.lock();
let last = self.get_last_update();
if let Some(last_time) = *last
&& now.duration_since(last_time) < timing::CONFIG_UPDATE_DEBOUNCE
if let Some(last_time) = last
&& now.duration_since(*last_time) < timing::CONFIG_UPDATE_DEBOUNCE
{
return Ok(false);
}
*last = Some(now);
self.set_last_update(now);
Ok(true)
}
+30 -12
View File
@@ -3,7 +3,7 @@ mod lifecycle;
mod state;
use anyhow::Result;
use parking_lot::Mutex;
use arc_swap::{ArcSwap, ArcSwapOption};
use std::{fmt, sync::Arc, time::Instant};
use crate::process::CommandChildGuard;
@@ -28,21 +28,21 @@ impl fmt::Display for RunningMode {
#[derive(Debug)]
pub struct CoreManager {
state: Arc<Mutex<State>>,
last_update: Arc<Mutex<Option<Instant>>>,
state: ArcSwap<State>,
last_update: ArcSwapOption<Instant>,
}
#[derive(Debug)]
struct State {
running_mode: Arc<RunningMode>,
child_sidecar: Option<CommandChildGuard>,
running_mode: ArcSwap<RunningMode>,
child_sidecar: ArcSwapOption<CommandChildGuard>,
}
impl Default for State {
fn default() -> Self {
Self {
running_mode: Arc::new(RunningMode::NotRunning),
child_sidecar: None,
running_mode: ArcSwap::new(Arc::new(RunningMode::NotRunning)),
child_sidecar: ArcSwapOption::new(None),
}
}
}
@@ -50,23 +50,41 @@ impl Default for State {
impl Default for CoreManager {
fn default() -> Self {
Self {
state: Arc::new(Mutex::new(State::default())),
last_update: Arc::new(Mutex::new(None)),
state: ArcSwap::new(Arc::new(State::default())),
last_update: ArcSwapOption::new(None),
}
}
}
impl CoreManager {
pub fn get_running_mode(&self) -> Arc<RunningMode> {
Arc::clone(&self.state.lock().running_mode)
Arc::clone(&self.state.load().running_mode.load())
}
pub fn take_child_sidecar(&self) -> Option<CommandChildGuard> {
self.state
.load()
.child_sidecar
.swap(None)
.and_then(|arc| Arc::try_unwrap(arc).ok())
}
pub fn get_last_update(&self) -> Option<Arc<Instant>> {
self.last_update.load_full()
}
pub fn set_running_mode(&self, mode: RunningMode) {
self.state.lock().running_mode = Arc::new(mode);
let state = self.state.load();
state.running_mode.store(Arc::new(mode));
}
pub fn set_running_child_sidecar(&self, child: CommandChildGuard) {
self.state.lock().child_sidecar = Some(child);
let state = self.state.load();
state.child_sidecar.store(Some(Arc::new(child)));
}
pub fn set_last_update(&self, time: Instant) {
self.last_update.store(Some(Arc::new(time)));
}
pub async fn init(&self) -> Result<()> {
+1 -2
View File
@@ -93,8 +93,7 @@ impl CoreManager {
defer! {
self.set_running_mode(RunningMode::NotRunning);
}
let mut state = self.state.lock();
if let Some(child) = state.child_sidecar.take() {
if let Some(child) = self.take_child_sidecar() {
let pid = child.pid();
drop(child);
logging!(trace, Type::Core, "Sidecar stopped (PID: {:?})", pid);