Skip to content

Remove unsound Send & Sync impls #2068

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion druid-shell/src/backend/gtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ pub mod error;
pub mod keycodes;
pub mod menu;
pub mod screen;
pub mod util;
pub mod window;
19 changes: 0 additions & 19 deletions druid-shell/src/backend/gtk/util.rs

This file was deleted.

25 changes: 8 additions & 17 deletions druid-shell/src/backend/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ use super::application::Application;
use super::dialog;
use super::keycodes;
use super::menu::Menu;
use super::util;

/// The backend target DPI.
///
Expand Down Expand Up @@ -1145,7 +1144,7 @@ impl WindowHandle {
let token = TimerToken::next();

if let Some(state) = self.state.upgrade() {
gtk::glib::timeout_add(interval, move || {
gtk::glib::timeout_add_local(interval, move || {
if state.with_handler(|h| h.timer(token)).is_some() {
return Continue(false);
}
Expand Down Expand Up @@ -1264,13 +1263,6 @@ impl WindowHandle {
}
}

// WindowState needs to be Send + Sync so it can be passed into glib closures.
// TODO: can we localize the unsafety more? Glib's idle loop always runs on the main thread,
// and we always construct the WindowState on the main thread, so it should be ok (and also
// WindowState isn't a public type).
unsafe impl Send for WindowState {}
unsafe impl Sync for WindowState {}

impl IdleHandle {
/// Add an idle handler, which is called (once) when the message loop
/// is empty. The idle handler will be run from the main UI thread, and
Expand All @@ -1287,7 +1279,7 @@ impl IdleHandle {
#[allow(clippy::branches_sharing_code)]
if queue.is_empty() {
queue.push(IdleKind::Callback(Box::new(callback)));
gtk::glib::idle_add(move || run_idle(&state));
gtk::glib::idle_add_local_once(move || run_idle(&state));
} else {
queue.push(IdleKind::Callback(Box::new(callback)));
}
Expand All @@ -1300,16 +1292,15 @@ impl IdleHandle {
#[allow(clippy::branches_sharing_code)]
if queue.is_empty() {
queue.push(IdleKind::Token(token));
gtk::glib::idle_add(move || run_idle(&state));
gtk::glib::idle_add_local_once(move || run_idle(&state));
} else {
queue.push(IdleKind::Token(token));
}
}
}
}

fn run_idle(state: &Arc<WindowState>) -> Continue {
util::assert_main_thread();
fn run_idle(state: &Arc<WindowState>) {
let result = state.with_handler(|handler| {
let queue: Vec<_> = std::mem::take(&mut state.idle_queue.lock().unwrap());

Expand All @@ -1324,13 +1315,13 @@ fn run_idle(state: &Arc<WindowState>) -> Continue {
if result.is_none() {
warn!("Delaying idle callbacks because the handler is borrowed.");
// Keep trying to reschedule this idle callback, because we haven't had a chance
// to empty the idle queue. Returning Continue(true) achieves this but
// causes 100% CPU usage, apparently because glib likes to call us back very quickly.
// to empty the idle queue. Using non-`_once` idle_add and returning `Continue(true)`
// achieves this but causes 100% CPU usage, apparently because glib likes to call us
// back very quickly.
let state = Arc::clone(state);
let timeout = Duration::from_millis(16);
gtk::glib::timeout_add(timeout, move || run_idle(&state));
gtk::glib::timeout_add_local_once(timeout, move || run_idle(&state));
}
Continue(false)
}

fn make_gdk_cursor(cursor: &Cursor, gdk_window: &Window) -> Option<gtk::gdk::Cursor> {
Expand Down