Skip to content

Commit

Permalink
Remove parking_lot dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 11, 2022
1 parent b1c9e4a commit 1d42aa2
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 170 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre

# Unreleased

- Removed `parking_lot` dependency.
- On macOS, fixed touch phase reporting when scrolling.
- On X11, fix min, max and resize increment hints not persisting for resizable windows (e.g. on DPI change).
- On Windows, respect min/max inner sizes when creating the window.
Expand Down
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ targets = [

[features]
default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
x11 = ["x11-dl", "mio", "percent-encoding", "parking_lot"]
x11 = ["x11-dl", "mio", "percent-encoding"]
wayland = ["wayland-client", "wayland-protocols", "sctk"]
wayland-dlopen = ["sctk/dlopen", "wayland-client/dlopen"]
wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/title"]
Expand Down Expand Up @@ -68,9 +68,6 @@ core-foundation = "0.9"
core-graphics = "0.22"
dispatch = "0.2.0"

[target.'cfg(target_os = "windows")'.dependencies]
parking_lot = "0.12"

[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
version = "0.36"
features = [
Expand Down Expand Up @@ -108,7 +105,6 @@ sctk-adwaita = { version = "0.4.1", optional = true }
mio = { version = "0.8", features = ["os-ext"], optional = true }
x11-dl = { version = "2.18.5", optional = true }
percent-encoding = { version = "2.0", optional = true }
parking_lot = { version = "0.12.0", optional = true }
libc = "0.2.64"

[target.'cfg(target_arch = "wasm32")'.dependencies.web_sys]
Expand Down
2 changes: 1 addition & 1 deletion src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub type XlibErrorHook =
pub fn register_xlib_error_hook(hook: XlibErrorHook) {
// Append new hook.
unsafe {
XLIB_ERROR_HOOKS.lock().push(hook);
XLIB_ERROR_HOOKS.lock().unwrap().push(hook);
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ use std::error::Error;

use std::{collections::VecDeque, env, fmt};
#[cfg(feature = "x11")]
use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Arc};
use std::{
ffi::CStr,
mem::MaybeUninit,
os::raw::*,
sync::{Arc, Mutex},
};

#[cfg(feature = "x11")]
use once_cell::sync::Lazy;
#[cfg(feature = "x11")]
use parking_lot::Mutex;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};

#[cfg(feature = "x11")]
Expand Down Expand Up @@ -595,11 +598,11 @@ unsafe extern "C" fn x_error_callback(
display: *mut x11::ffi::Display,
event: *mut x11::ffi::XErrorEvent,
) -> c_int {
let xconn_lock = X11_BACKEND.lock();
let xconn_lock = X11_BACKEND.lock().unwrap();
if let Ok(ref xconn) = *xconn_lock {
// Call all the hooks.
let mut error_handled = false;
for hook in XLIB_ERROR_HOOKS.lock().iter() {
for hook in XLIB_ERROR_HOOKS.lock().unwrap().iter() {
error_handled |= hook(display as *mut _, event as *mut _);
}

Expand All @@ -626,7 +629,7 @@ unsafe extern "C" fn x_error_callback(
error!("X11 error: {:#?}", error);
}

*xconn.latest_error.lock() = Some(error);
*xconn.latest_error.lock().unwrap() = Some(error);
}
// Fun fact: this return value is completely ignored.
0
Expand Down Expand Up @@ -729,7 +732,7 @@ impl<T: 'static> EventLoop<T> {

#[cfg(feature = "x11")]
fn new_x11_any_thread() -> Result<EventLoop<T>, XNotSupported> {
let xconn = match X11_BACKEND.lock().as_ref() {
let xconn = match X11_BACKEND.lock().unwrap().as_ref() {
Ok(xconn) => xconn.clone(),
Err(err) => return Err(err.clone()),
};
Expand Down
61 changes: 34 additions & 27 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc, slice, sync::Arc};

use libc::{c_char, c_int, c_long, c_uint, c_ulong};

use parking_lot::MutexGuard;

use super::{
events, ffi, get_xtarget, mkdid, mkwid, monitor, util, Device, DeviceId, DeviceInfo, Dnd,
DndState, GenericEventCookie, ImeReceiver, ScrollOrientation, UnownedWindow, WindowId,
Expand Down Expand Up @@ -351,9 +349,9 @@ impl<T: 'static> EventProcessor<T> {
let new_inner_size = (xev.width as u32, xev.height as u32);
let new_inner_position = (xev.x as i32, xev.y as i32);

let mut shared_state_lock = window.shared_state.lock();

let (mut resized, moved) = {
let mut shared_state_lock = window.shared_state_lock();

let resized =
util::maybe_change(&mut shared_state_lock.size, new_inner_size);
let moved = if is_synthetic {
Expand All @@ -380,7 +378,11 @@ impl<T: 'static> EventProcessor<T> {
(resized, moved)
};

let new_outer_position = if moved || shared_state_lock.position.is_none() {
let position = window.shared_state_lock().position;

let new_outer_position = if moved || position.is_none() {
let mut shared_state_lock = window.shared_state_lock();

// We need to convert client area position to window position.
let frame_extents = shared_state_lock
.frame_extents
Expand All @@ -395,21 +397,23 @@ impl<T: 'static> EventProcessor<T> {
let outer = frame_extents
.inner_pos_to_outer(new_inner_position.0, new_inner_position.1);
shared_state_lock.position = Some(outer);

// Unlock shared state to prevent deadlock in callback below
drop(shared_state_lock);

if moved {
// Temporarily unlock shared state to prevent deadlock
MutexGuard::unlocked(&mut shared_state_lock, || {
callback(Event::WindowEvent {
window_id,
event: WindowEvent::Moved(outer.into()),
});
callback(Event::WindowEvent {
window_id,
event: WindowEvent::Moved(outer.into()),
});
}
outer
} else {
shared_state_lock.position.unwrap()
position.unwrap()
};

if is_synthetic {
let mut shared_state_lock = window.shared_state_lock();
// If we don't use the existing adjusted value when available, then the user can screw up the
// resizing by dragging across monitors *without* dropping the window.
let (width, height) = shared_state_lock
Expand Down Expand Up @@ -441,30 +445,33 @@ impl<T: 'static> EventProcessor<T> {
let old_inner_size = PhysicalSize::new(width, height);
let mut new_inner_size = PhysicalSize::new(new_width, new_height);

// Temporarily unlock shared state to prevent deadlock
MutexGuard::unlocked(&mut shared_state_lock, || {
callback(Event::WindowEvent {
window_id,
event: WindowEvent::ScaleFactorChanged {
scale_factor: new_scale_factor,
new_inner_size: &mut new_inner_size,
},
});
// Unlock shared state to prevent deadlock in callback below
drop(shared_state_lock);

callback(Event::WindowEvent {
window_id,
event: WindowEvent::ScaleFactorChanged {
scale_factor: new_scale_factor,
new_inner_size: &mut new_inner_size,
},
});

if new_inner_size != old_inner_size {
window.set_inner_size_physical(
new_inner_size.width,
new_inner_size.height,
);
shared_state_lock.dpi_adjusted = Some(new_inner_size.into());
window.shared_state_lock().dpi_adjusted =
Some(new_inner_size.into());
// if the DPI factor changed, force a resize event to ensure the logical
// size is computed with the right DPI factor
resized = true;
}
}
}

let mut shared_state_lock = window.shared_state_lock();

// This is a hack to ensure that the DPI adjusted resize is actually applied on all WMs. KWin
// doesn't need this, but Xfwm does. The hack should not be run on other WMs, since tiling
// WMs constrain the window size, making the resize fail. This would cause an endless stream of
Expand All @@ -478,10 +485,10 @@ impl<T: 'static> EventProcessor<T> {
}
}

if resized {
// Drop the shared state lock to prevent deadlock
drop(shared_state_lock);
// Unlock shared state to prevent deadlock in callback below
drop(shared_state_lock);

if resized {
callback(Event::WindowEvent {
window_id,
event: WindowEvent::Resized(new_inner_size.into()),
Expand Down Expand Up @@ -747,7 +754,7 @@ impl<T: 'static> EventProcessor<T> {
update_modifiers!(modifiers, None);

let cursor_moved = self.with_window(xev.event, |window| {
let mut shared_state_lock = window.shared_state.lock();
let mut shared_state_lock = window.shared_state_lock();
util::maybe_change(&mut shared_state_lock.cursor_pos, new_cursor_pos)
});
if cursor_moved == Some(true) {
Expand Down Expand Up @@ -1215,7 +1222,7 @@ impl<T: 'static> EventProcessor<T> {
new_monitor.scale_factor,
width,
height,
&*window.shared_state.lock(),
&*window.shared_state_lock(),
);

let window_id = crate::window::WindowId(*window_id);
Expand Down
3 changes: 1 addition & 2 deletions src/platform_impl/linux/x11/ime/input_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use std::{
fmt,
os::raw::c_char,
ptr,
sync::Arc,
sync::{Arc, Mutex},
};

use once_cell::sync::Lazy;
use parking_lot::Mutex;

use super::{ffi, util, XConnection, XError};

Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/linux/x11/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::os::raw::*;
use std::slice;
use std::sync::Mutex;

use once_cell::sync::Lazy;
use parking_lot::Mutex;

use super::{
ffi::{
Expand All @@ -24,7 +24,7 @@ static MONITORS: Lazy<Mutex<Option<Vec<MonitorHandle>>>> = Lazy::new(Mutex::defa

pub fn invalidate_cached_monitor_list() -> Option<Vec<MonitorHandle>> {
// We update this lazily.
(*MONITORS.lock()).take()
(*MONITORS.lock().unwrap()).take()
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -293,7 +293,7 @@ impl XConnection {
}

pub fn available_monitors(&self) -> Vec<MonitorHandle> {
let mut monitors_lock = MONITORS.lock();
let mut monitors_lock = MONITORS.lock().unwrap();
(*monitors_lock)
.as_ref()
.cloned()
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/linux/x11/util/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::{
ffi::{CStr, CString},
fmt::Debug,
os::raw::*,
sync::Mutex,
};

use once_cell::sync::Lazy;
use parking_lot::Mutex;

use super::*;

Expand All @@ -17,7 +17,7 @@ static ATOM_CACHE: Lazy<Mutex<AtomCache>> = Lazy::new(|| Mutex::new(HashMap::wit
impl XConnection {
pub fn get_atom<T: AsRef<CStr> + Debug>(&self, name: T) -> ffi::Atom {
let name = name.as_ref();
let mut atom_cache_lock = ATOM_CACHE.lock();
let mut atom_cache_lock = ATOM_CACHE.lock().unwrap();
let cached_atom = (*atom_cache_lock).get(name).cloned();
if let Some(atom) = cached_atom {
atom
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/linux/x11/util/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ impl XConnection {
let cursor = *self
.cursor_cache
.lock()
.unwrap()
.entry(cursor)
.or_insert_with(|| self.get_cursor(cursor));

Expand Down
11 changes: 6 additions & 5 deletions src/platform_impl/linux/x11/util/wm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Mutex;

use once_cell::sync::Lazy;
use parking_lot::Mutex;

use super::*;

Expand All @@ -9,11 +10,11 @@ static SUPPORTED_HINTS: Lazy<Mutex<Vec<ffi::Atom>>> =
static WM_NAME: Lazy<Mutex<Option<String>>> = Lazy::new(|| Mutex::new(None));

pub fn hint_is_supported(hint: ffi::Atom) -> bool {
(*SUPPORTED_HINTS.lock()).contains(&hint)
(*SUPPORTED_HINTS.lock().unwrap()).contains(&hint)
}

pub fn wm_name_is_one_of(names: &[&str]) -> bool {
if let Some(ref name) = *WM_NAME.lock() {
if let Some(ref name) = *WM_NAME.lock().unwrap() {
names.contains(&name.as_str())
} else {
false
Expand All @@ -22,8 +23,8 @@ pub fn wm_name_is_one_of(names: &[&str]) -> bool {

impl XConnection {
pub fn update_cached_wm_info(&self, root: ffi::Window) {
*SUPPORTED_HINTS.lock() = self.get_supported_hints(root);
*WM_NAME.lock() = self.get_wm_name(root);
*SUPPORTED_HINTS.lock().unwrap() = self.get_supported_hints(root);
*WM_NAME.lock().unwrap() = self.get_wm_name(root);
}

fn get_supported_hints(&self, root: ffi::Window) -> Vec<ffi::Atom> {
Expand Down
Loading

0 comments on commit 1d42aa2

Please sign in to comment.