Remove parking_lot dependency (#2423)

This commit is contained in:
Mads Marquart 2022-08-31 18:32:19 +02:00 committed by GitHub
parent ec7e935248
commit 8729119536
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 197 additions and 175 deletions

View file

@ -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")]
@ -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 _);
}
@ -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
@ -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()),
};

View file

@ -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,
@ -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 {
@ -380,7 +378,13 @@ 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 let (Some(position), false) = (position, moved) {
position
} else {
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
@ -395,21 +399,21 @@ 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()
};
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
@ -441,15 +445,15 @@ 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 {
@ -457,7 +461,8 @@ impl<T: 'static> EventProcessor<T> {
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;
@ -465,6 +470,8 @@ impl<T: 'static> EventProcessor<T> {
}
}
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
@ -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()),
@ -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) {
@ -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);

View file

@ -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};

View file

@ -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::{
@ -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)]
@ -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()

View file

@ -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::*;
@ -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

View file

@ -7,6 +7,7 @@ impl XConnection {
let cursor = *self
.cursor_cache
.lock()
.unwrap()
.entry(cursor)
.or_insert_with(|| self.get_cursor(cursor));

View file

@ -1,5 +1,6 @@
use std::sync::Mutex;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use super::*;
@ -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
@ -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> {

View file

@ -5,11 +5,10 @@ use std::{
os::raw::*,
path::Path,
ptr, slice,
sync::Arc,
sync::{Arc, Mutex, MutexGuard},
};
use libc;
use parking_lot::Mutex;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XlibDisplayHandle, XlibWindowHandle};
use x11_dl::xlib::TrueColor;
@ -107,6 +106,7 @@ pub struct UnownedWindow {
screen_id: i32, // never changes
cursor: Mutex<CursorIcon>,
cursor_grabbed_mode: Mutex<CursorGrabMode>,
#[allow(clippy::mutex_atomic)]
cursor_visible: Mutex<bool>,
ime_sender: Mutex<ImeSender>,
pub shared_state: Mutex<SharedState>,
@ -270,6 +270,7 @@ impl UnownedWindow {
)
};
#[allow(clippy::mutex_atomic)]
let mut window = UnownedWindow {
xconn: Arc::clone(xconn),
xwindow,
@ -374,7 +375,7 @@ impl UnownedWindow {
}
}
let mut shared_state = window.shared_state.get_mut();
let mut shared_state = window.shared_state.get_mut().unwrap();
shared_state.min_inner_size = min_inner_size.map(Into::into);
shared_state.max_inner_size = max_inner_size.map(Into::into);
shared_state.resize_increments = pl_attribs.resize_increments;
@ -480,7 +481,7 @@ impl UnownedWindow {
}
if let Some(PhysicalPosition { x, y }) = position {
let shared_state = window.shared_state.get_mut();
let shared_state = window.shared_state.get_mut().unwrap();
shared_state.restore_position = Some((x, y));
}
@ -499,6 +500,10 @@ impl UnownedWindow {
.map_err(|x_err| os_error!(OsError::XError(x_err)))
}
pub(super) fn shared_state_lock(&self) -> MutexGuard<'_, SharedState> {
self.shared_state.lock().unwrap()
}
fn set_pid(&self) -> Option<util::Flusher<'_>> {
let pid_atom = unsafe { self.xconn.get_atom_unchecked(b"_NET_WM_PID\0") };
let client_machine_atom = unsafe { self.xconn.get_atom_unchecked(b"WM_CLIENT_MACHINE\0") };
@ -611,7 +616,7 @@ impl UnownedWindow {
}
fn set_fullscreen_inner(&self, fullscreen: Option<Fullscreen>) -> Option<util::Flusher<'_>> {
let mut shared_state_lock = self.shared_state.lock();
let mut shared_state_lock = self.shared_state_lock();
match shared_state_lock.visibility {
// Setting fullscreen on a window that is not visible will generate an error.
@ -665,7 +670,7 @@ impl UnownedWindow {
match fullscreen {
None => {
let flusher = self.set_fullscreen_hint(false);
let mut shared_state_lock = self.shared_state.lock();
let mut shared_state_lock = self.shared_state_lock();
if let Some(position) = shared_state_lock.restore_position.take() {
drop(shared_state_lock);
self.set_position_inner(position.0, position.1).queue();
@ -722,7 +727,7 @@ impl UnownedWindow {
}
let window_position = self.outer_position_physical();
self.shared_state.lock().restore_position = Some(window_position);
self.shared_state_lock().restore_position = Some(window_position);
let monitor_origin: (i32, i32) = monitor.position().into();
self.set_position_inner(monitor_origin.0, monitor_origin.1)
.queue();
@ -733,7 +738,7 @@ impl UnownedWindow {
#[inline]
pub fn fullscreen(&self) -> Option<Fullscreen> {
let shared_state = self.shared_state.lock();
let shared_state = self.shared_state_lock();
shared_state
.desired_fullscreen
@ -753,7 +758,7 @@ impl UnownedWindow {
// Called by EventProcessor when a VisibilityNotify event is received
pub(crate) fn visibility_notify(&self) {
let mut shared_state = self.shared_state.lock();
let mut shared_state = self.shared_state_lock();
match shared_state.visibility {
Visibility::No => unsafe {
@ -773,7 +778,7 @@ impl UnownedWindow {
#[inline]
pub fn current_monitor(&self) -> X11MonitorHandle {
self.shared_state.lock().last_monitor.clone()
self.shared_state_lock().last_monitor.clone()
}
pub fn available_monitors(&self) -> Vec<X11MonitorHandle> {
@ -888,7 +893,7 @@ impl UnownedWindow {
}
fn set_decorations_inner(&self, decorations: bool) -> util::Flusher<'_> {
self.shared_state.lock().is_decorated = decorations;
self.shared_state_lock().is_decorated = decorations;
let mut hints = self.xconn.get_motif_hints(self.xwindow);
hints.set_decorations(decorations);
@ -906,7 +911,7 @@ impl UnownedWindow {
#[inline]
pub fn is_decorated(&self) -> bool {
self.shared_state.lock().is_decorated
self.shared_state_lock().is_decorated
}
fn set_maximizable_inner(&self, maximizable: bool) -> util::Flusher<'_> {
@ -965,7 +970,7 @@ impl UnownedWindow {
#[inline]
pub fn set_visible(&self, visible: bool) {
let mut shared_state = self.shared_state.lock();
let mut shared_state = self.shared_state_lock();
match (visible, shared_state.visibility) {
(true, Visibility::Yes) | (true, Visibility::YesWait) | (false, Visibility::No) => {
@ -995,22 +1000,22 @@ impl UnownedWindow {
#[inline]
pub fn is_visible(&self) -> Option<bool> {
Some(self.shared_state.lock().visibility == Visibility::Yes)
Some(self.shared_state_lock().visibility == Visibility::Yes)
}
fn update_cached_frame_extents(&self) {
let extents = self
.xconn
.get_frame_extents_heuristic(self.xwindow, self.root);
(*self.shared_state.lock()).frame_extents = Some(extents);
(*self.shared_state_lock()).frame_extents = Some(extents);
}
pub(crate) fn invalidate_cached_frame_extents(&self) {
(*self.shared_state.lock()).frame_extents.take();
(*self.shared_state_lock()).frame_extents.take();
}
pub(crate) fn outer_position_physical(&self) -> (i32, i32) {
let extents = (*self.shared_state.lock()).frame_extents.clone();
let extents = (*self.shared_state_lock()).frame_extents.clone();
if let Some(extents) = extents {
let (x, y) = self.inner_position_physical();
extents.inner_pos_to_outer(x, y)
@ -1022,7 +1027,7 @@ impl UnownedWindow {
#[inline]
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
let extents = (*self.shared_state.lock()).frame_extents.clone();
let extents = (*self.shared_state_lock()).frame_extents.clone();
if let Some(extents) = extents {
let (x, y) = self.inner_position_physical();
Ok(extents.inner_pos_to_outer(x, y).into())
@ -1050,7 +1055,7 @@ impl UnownedWindow {
// There are a few WMs that set client area position rather than window position, so
// we'll translate for consistency.
if util::wm_name_is_one_of(&["Enlightenment", "FVWM"]) {
let extents = (*self.shared_state.lock()).frame_extents.clone();
let extents = (*self.shared_state_lock()).frame_extents.clone();
if let Some(extents) = extents {
x += extents.frame_extents.left as i32;
y += extents.frame_extents.top as i32;
@ -1093,7 +1098,7 @@ impl UnownedWindow {
#[inline]
pub fn outer_size(&self) -> PhysicalSize<u32> {
let extents = self.shared_state.lock().frame_extents.clone();
let extents = self.shared_state_lock().frame_extents.clone();
if let Some(extents) = extents {
let (width, height) = self.inner_size_physical();
extents.inner_size_to_outer(width, height).into()
@ -1120,7 +1125,7 @@ impl UnownedWindow {
pub fn set_inner_size(&self, size: Size) {
let scale_factor = self.scale_factor();
let size = size.to_physical::<u32>(scale_factor).into();
if !self.shared_state.lock().is_resizable {
if !self.shared_state_lock().is_resizable {
self.update_normal_hints(|normal_hints| {
normal_hints.set_min_size(Some(size));
normal_hints.set_max_size(Some(size));
@ -1148,7 +1153,7 @@ impl UnownedWindow {
#[inline]
pub fn set_min_inner_size(&self, dimensions: Option<Size>) {
self.shared_state.lock().min_inner_size = dimensions;
self.shared_state_lock().min_inner_size = dimensions;
let physical_dimensions =
dimensions.map(|dimensions| dimensions.to_physical::<u32>(self.scale_factor()).into());
self.set_min_inner_size_physical(physical_dimensions);
@ -1161,7 +1166,7 @@ impl UnownedWindow {
#[inline]
pub fn set_max_inner_size(&self, dimensions: Option<Size>) {
self.shared_state.lock().max_inner_size = dimensions;
self.shared_state_lock().max_inner_size = dimensions;
let physical_dimensions =
dimensions.map(|dimensions| dimensions.to_physical::<u32>(self.scale_factor()).into());
self.set_max_inner_size_physical(physical_dimensions);
@ -1206,7 +1211,7 @@ impl UnownedWindow {
}
let (min_size, max_size) = if resizable {
let shared_state_lock = self.shared_state.lock();
let shared_state_lock = self.shared_state_lock();
(
shared_state_lock.min_inner_size,
shared_state_lock.max_inner_size,
@ -1215,7 +1220,7 @@ impl UnownedWindow {
let window_size = Some(Size::from(self.inner_size()));
(window_size, window_size)
};
self.shared_state.lock().is_resizable = resizable;
self.shared_state_lock().is_resizable = resizable;
self.set_maximizable_inner(resizable).queue();
@ -1235,7 +1240,7 @@ impl UnownedWindow {
#[inline]
pub fn is_resizable(&self) -> bool {
self.shared_state.lock().is_resizable
self.shared_state_lock().is_resizable
}
#[inline]
@ -1265,15 +1270,16 @@ impl UnownedWindow {
#[inline]
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
let old_cursor = replace(&mut *self.cursor.lock(), cursor);
if cursor != old_cursor && *self.cursor_visible.lock() {
let old_cursor = replace(&mut *self.cursor.lock().unwrap(), cursor);
#[allow(clippy::mutex_atomic)]
if cursor != old_cursor && *self.cursor_visible.lock().unwrap() {
self.xconn.set_cursor_icon(self.xwindow, Some(cursor));
}
}
#[inline]
pub fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
let mut grabbed_lock = self.cursor_grabbed_mode.lock();
let mut grabbed_lock = self.cursor_grabbed_mode.lock().unwrap();
if mode == *grabbed_lock {
return Ok(());
}
@ -1346,12 +1352,13 @@ impl UnownedWindow {
#[inline]
pub fn set_cursor_visible(&self, visible: bool) {
let mut visible_lock = self.cursor_visible.lock();
#[allow(clippy::mutex_atomic)]
let mut visible_lock = self.cursor_visible.lock().unwrap();
if visible == *visible_lock {
return;
}
let cursor = if visible {
Some(*self.cursor.lock())
Some(*self.cursor.lock().unwrap())
} else {
None
};
@ -1397,7 +1404,7 @@ impl UnownedWindow {
// we can't use `set_cursor_grab(false)` here because it doesn't run `XUngrabPointer`
// if the cursor isn't currently grabbed
let mut grabbed_lock = self.cursor_grabbed_mode.lock();
let mut grabbed_lock = self.cursor_grabbed_mode.lock().unwrap();
unsafe {
(self.xconn.xlib.XUngrabPointer)(self.xconn.display, ffi::CurrentTime);
}
@ -1431,6 +1438,7 @@ impl UnownedWindow {
let _ = self
.ime_sender
.lock()
.unwrap()
.send(ImeRequest::Position(self.xwindow, x, y));
}
@ -1439,6 +1447,7 @@ impl UnownedWindow {
let _ = self
.ime_sender
.lock()
.unwrap()
.send(ImeRequest::Allow(self.xwindow, allowed));
}
@ -1454,7 +1463,7 @@ impl UnownedWindow {
} else {
false
};
let is_visible = match self.shared_state.lock().visibility {
let is_visible = match self.shared_state_lock().visibility {
Visibility::Yes => true,
Visibility::YesWait | Visibility::No => false,
};

View file

@ -1,7 +1,4 @@
use std::{collections::HashMap, error::Error, fmt, os::raw::c_int, ptr};
use libc;
use parking_lot::Mutex;
use std::{collections::HashMap, error::Error, fmt, os::raw::c_int, ptr, sync::Mutex};
use crate::window::CursorIcon;
@ -74,7 +71,7 @@ impl XConnection {
/// Checks whether an error has been triggered by the previous function calls.
#[inline]
pub fn check_errors(&self) -> Result<(), XError> {
let error = self.latest_error.lock().take();
let error = self.latest_error.lock().unwrap().take();
if let Some(error) = error {
Err(error)
} else {
@ -85,7 +82,7 @@ impl XConnection {
/// Ignores any previous error.
#[inline]
pub fn ignore_error(&self) {
*self.latest_error.lock() = None;
*self.latest_error.lock().unwrap() = None;
}
}