Skip to content

Commit

Permalink
Fix some race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
shiro committed Oct 29, 2024
1 parent 32f41ee commit b101c60
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 49 deletions.
2 changes: 1 addition & 1 deletion evdev-rs/evdev-sys/libevdev
Submodule libevdev updated from d447a7 to 2342d8
18 changes: 8 additions & 10 deletions src/mapper/chord_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ async fn handle(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {

// fired after the chord timeout has passed, submits the keys held on the stack
async fn handle_cb(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {
let mut state = _state.lock().await;
let state = &mut *state;
let mut _state = _state.lock().await;
let state = &mut *_state;
let ev = match raw_ev {
InputEvent::Raw(ev) => ev,
};
Expand Down Expand Up @@ -393,14 +393,12 @@ async fn handle_cb(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {
}
}

run_python_handler(
handler.clone(),
None,
ev.clone(),
state.transformer.clone(),
state.next.values().cloned().collect(),
)
.await;
let handler = handler.clone();
let transformer = state.transformer.clone();
let next = state.next.values().cloned().collect();
drop(state);
drop(_state);
run_python_handler(handler, None, ev.clone(), transformer, next).await;
}
RuntimeAction::NOP => {}
}
Expand Down
42 changes: 18 additions & 24 deletions src/mapper/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,12 @@ async fn handle(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {
let ev = match raw_ev {
InputEvent::Raw(ev) => ev,
};
run_python_handler(
handler.clone(),
None,
ev,
state.transformer.clone(),
state.next.values().cloned().collect(),
)
.await;

let handler = handler.clone();
let transformer = state.transformer.clone();
let next = state.next.values().cloned().collect();
drop(state);
run_python_handler(handler, None, ev, transformer, next).await;
}
RuntimeAction::NOP => {}
}
Expand Down Expand Up @@ -493,14 +491,12 @@ async fn handle(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {
let ev = match raw_ev {
InputEvent::Raw(ev) => ev,
};
run_python_handler(
handler.clone(),
Some(args),
ev,
state.transformer.clone(),
state.next.values().cloned().collect(),
)
.await;

let handler = handler.clone();
let transformer = state.transformer.clone();
let next = state.next.values().cloned().collect();
drop(state);
run_python_handler(handler, Some(args), ev, transformer, next).await;
return;
}
}
Expand All @@ -523,14 +519,12 @@ async fn handle(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {
let ev = match raw_ev {
InputEvent::Raw(ev) => ev,
};
run_python_handler(
handler.clone(),
Some(args),
ev,
state.transformer.clone(),
state.next.values().cloned().collect(),
)
.await;

let handler = handler.clone();
let transformer = state.transformer.clone();
let next = state.next.values().cloned().collect();
drop(state);
run_python_handler(handler, Some(args), ev, transformer, next).await;
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mapper/mapper_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn run_python_handler(
// next: &HashMap<Uuid, Arc<dyn LinkDst>>,
next: Vec<Arc<dyn LinkDst>>,
) -> Result<()> {
// println!("--> 1");
println!("--> 1");
tokio::task::spawn_blocking(move || {
let ret = Python::with_gil(|py| -> Result<()> {
let asyncio =
Expand Down Expand Up @@ -79,7 +79,7 @@ pub async fn run_python_handler(
}
_ => {}
};
// println!("--> 2");
println!("--> 2");
Ok(())
}
});
Expand Down
19 changes: 9 additions & 10 deletions src/mapper/text_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ fn _map(from: &KeyClickActionWithMods, to: Vec<ParsedKeyAction>) -> Vec<RuntimeK
}

async fn handle(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {
let mut state = _state.lock().await;
let mut state = &mut *state;
let mut _state = _state.lock().await;
let mut state = &mut *_state;
if !state.next.is_empty() {
state.next.send_all(raw_ev.clone());
}
Expand Down Expand Up @@ -324,14 +324,13 @@ async fn handle(_state: Arc<Mutex<State>>, raw_ev: InputEvent) {
}
// delay the callback until the backspace events are processed
tokio::time::sleep(Duration::from_millis(10 * from_len as u64)).await;
run_python_handler(
handler.clone(),
None,
ev,
state.transformer.clone(),
state.next.values().cloned().collect(),
)
.await;

let handler = handler.clone();
let transformer = state.transformer.clone();
let next = state.next.values().cloned().collect();
drop(state);
drop(_state);
run_python_handler(handler, None, ev, transformer, next).await;
}
RuntimeAction::NOP => {}
}
Expand Down
7 changes: 5 additions & 2 deletions src/window/hyprland_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub fn hyprland_window_handler() -> WindowHandler {
move |info: ActiveWindowInfo| {
tokio::task::spawn_blocking(move || {
Python::with_gil(|py| {
for callback in subscriptions.lock().unwrap().values() {
let subscriptions = { subscriptions.lock().unwrap().values().cloned().collect::<Vec<_>>() };
for callback in subscriptions {
let is_callable = callback.as_ref(py).is_callable();
if !is_callable {
continue;
Expand Down Expand Up @@ -81,17 +82,19 @@ pub fn hyprland_window_handler() -> WindowHandler {
subscriptions.lock().unwrap().insert(id, callback.clone());

if let Ok(Some(info)) = Client::get_active_async().await {
println!("info!");
println!(" --> w1");
//if !is_callable { continue; }

tokio::task::spawn_blocking(move || {
Python::with_gil(|py| {
println!(" --> w1 start");
let is_callable = callback.as_ref(py).is_callable();
let ret = callback.call(py, (info.class.clone(),), None);
if let Err(err) = ret {
eprintln!("{err}");
std::process::exit(1);
}
println!(" --> w1 done");
});
});
}
Expand Down

0 comments on commit b101c60

Please sign in to comment.