Skip to content

Commit 8b8a767

Browse files
committed
Replace windows Mutex with parking_lot Mutex
1 parent 9feada2 commit 8b8a767

File tree

4 files changed

+59
-46
lines changed

4 files changed

+59
-46
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,7 @@ features = [
6363
wayland-client = { version = "0.21", features = [ "dlopen", "egl", "cursor"] }
6464
smithay-client-toolkit = "0.4"
6565
x11-dl = "2.18.3"
66-
parking_lot = "0.6"
6766
percent-encoding = "1.0"
67+
68+
[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "windows"))'.dependencies.parking_lot]
69+
version = "0.6"

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extern crate core_foundation;
109109
extern crate core_graphics;
110110
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
111111
extern crate x11_dl;
112-
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
112+
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "windows"))]
113113
extern crate parking_lot;
114114
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
115115
extern crate percent_encoding;

src/platform/windows/events_loop.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use winapi::shared::basetsd::UINT_PTR;
1717
use std::rc::Rc;
1818
use std::{mem, ptr};
1919
use std::cell::RefCell;
20-
use std::sync::{Arc, Mutex};
20+
use std::sync::Arc;
2121
use std::collections::VecDeque;
22+
use parking_lot::Mutex;
2223

2324
use winapi::ctypes::c_int;
2425
use winapi::shared::minwindef::{
@@ -463,7 +464,7 @@ pub unsafe extern "system" fn callback(
463464
winuser::WM_MOUSEMOVE => {
464465
use events::WindowEvent::{CursorEntered, CursorMoved};
465466
let mouse_outside_window = {
466-
let mut window = subclass_input.window_state.lock().unwrap();
467+
let mut window = subclass_input.window_state.lock();
467468
if !window.mouse_in_window {
468469
window.mouse_in_window = true;
469470
true
@@ -503,7 +504,7 @@ pub unsafe extern "system" fn callback(
503504
winuser::WM_MOUSELEAVE => {
504505
use events::WindowEvent::CursorLeft;
505506
let mouse_in_window = {
506-
let mut window = subclass_input.window_state.lock().unwrap();
507+
let mut window = subclass_input.window_state.lock();
507508
if window.mouse_in_window {
508509
window.mouse_in_window = false;
509510
true
@@ -594,7 +595,7 @@ pub unsafe extern "system" fn callback(
594595
use events::MouseButton::Left;
595596
use events::ElementState::Pressed;
596597

597-
capture_mouse(window, &mut *subclass_input.window_state.lock().unwrap());
598+
capture_mouse(window, &mut *subclass_input.window_state.lock());
598599

599600
subclass_input.send_event(Event::WindowEvent {
600601
window_id: SuperWindowId(WindowId(window)),
@@ -608,7 +609,7 @@ pub unsafe extern "system" fn callback(
608609
use events::MouseButton::Left;
609610
use events::ElementState::Released;
610611

611-
release_mouse(&mut *subclass_input.window_state.lock().unwrap());
612+
release_mouse(&mut *subclass_input.window_state.lock());
612613

613614
subclass_input.send_event(Event::WindowEvent {
614615
window_id: SuperWindowId(WindowId(window)),
@@ -622,7 +623,7 @@ pub unsafe extern "system" fn callback(
622623
use events::MouseButton::Right;
623624
use events::ElementState::Pressed;
624625

625-
capture_mouse(window, &mut *subclass_input.window_state.lock().unwrap());
626+
capture_mouse(window, &mut *subclass_input.window_state.lock());
626627

627628
subclass_input.send_event(Event::WindowEvent {
628629
window_id: SuperWindowId(WindowId(window)),
@@ -636,7 +637,7 @@ pub unsafe extern "system" fn callback(
636637
use events::MouseButton::Right;
637638
use events::ElementState::Released;
638639

639-
release_mouse(&mut *subclass_input.window_state.lock().unwrap());
640+
release_mouse(&mut *subclass_input.window_state.lock());
640641

641642
subclass_input.send_event(Event::WindowEvent {
642643
window_id: SuperWindowId(WindowId(window)),
@@ -650,7 +651,7 @@ pub unsafe extern "system" fn callback(
650651
use events::MouseButton::Middle;
651652
use events::ElementState::Pressed;
652653

653-
capture_mouse(window, &mut *subclass_input.window_state.lock().unwrap());
654+
capture_mouse(window, &mut *subclass_input.window_state.lock());
654655

655656
subclass_input.send_event(Event::WindowEvent {
656657
window_id: SuperWindowId(WindowId(window)),
@@ -664,7 +665,7 @@ pub unsafe extern "system" fn callback(
664665
use events::MouseButton::Middle;
665666
use events::ElementState::Released;
666667

667-
release_mouse(&mut *subclass_input.window_state.lock().unwrap());
668+
release_mouse(&mut *subclass_input.window_state.lock());
668669

669670
subclass_input.send_event(Event::WindowEvent {
670671
window_id: SuperWindowId(WindowId(window)),
@@ -679,7 +680,7 @@ pub unsafe extern "system" fn callback(
679680
use events::ElementState::Pressed;
680681
let xbutton = winuser::GET_XBUTTON_WPARAM(wparam);
681682

682-
capture_mouse(window, &mut *subclass_input.window_state.lock().unwrap());
683+
capture_mouse(window, &mut *subclass_input.window_state.lock());
683684

684685
subclass_input.send_event(Event::WindowEvent {
685686
window_id: SuperWindowId(WindowId(window)),
@@ -694,7 +695,7 @@ pub unsafe extern "system" fn callback(
694695
use events::ElementState::Released;
695696
let xbutton = winuser::GET_XBUTTON_WPARAM(wparam);
696697

697-
release_mouse(&mut *subclass_input.window_state.lock().unwrap());
698+
release_mouse(&mut *subclass_input.window_state.lock());
698699

699700
subclass_input.send_event(Event::WindowEvent {
700701
window_id: SuperWindowId(WindowId(window)),
@@ -893,7 +894,7 @@ pub unsafe extern "system" fn callback(
893894

894895
winuser::WM_SETCURSOR => {
895896
let call_def_window_proc = {
896-
let window_state = subclass_input.window_state.lock().unwrap();
897+
let window_state = subclass_input.window_state.lock();
897898
if window_state.mouse_in_window {
898899
let cursor = winuser::LoadCursorW(
899900
ptr::null_mut(),
@@ -921,7 +922,7 @@ pub unsafe extern "system" fn callback(
921922
winuser::WM_GETMINMAXINFO => {
922923
let mmi = lparam as *mut winuser::MINMAXINFO;
923924

924-
let window_state = subclass_input.window_state.lock().unwrap();
925+
let window_state = subclass_input.window_state.lock();
925926

926927
if window_state.min_size.is_some() || window_state.max_size.is_some() {
927928
let style = winuser::GetWindowLongA(window, winuser::GWL_STYLE) as DWORD;
@@ -952,7 +953,7 @@ pub unsafe extern "system" fn callback(
952953
let new_dpi_factor = dpi_to_scale_factor(new_dpi_x);
953954

954955
let suppress_resize = {
955-
let mut window_state = subclass_input.window_state.lock().unwrap();
956+
let mut window_state = subclass_input.window_state.lock();
956957
let suppress_resize = window_state.saved_window_info
957958
.as_mut()
958959
.map(|saved_window_info| {

src/platform/windows/window.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use std::{io, mem, ptr};
44
use std::cell::Cell;
55
use std::ffi::OsStr;
66
use std::os::windows::ffi::OsStrExt;
7-
use std::sync::{Arc, Mutex};
7+
use std::sync::Arc;
88
use std::sync::mpsc::channel;
9+
use parking_lot::{Mutex, MutexGuard};
910

1011
use winapi::ctypes::c_int;
1112
use winapi::shared::minwindef::{BOOL, DWORD, FALSE, LPARAM, TRUE, UINT, WORD, WPARAM};
1213
use winapi::shared::windef::{HWND, LPPOINT, POINT, RECT};
1314
use winapi::um::{combaseapi, dwmapi, libloaderapi, winuser, ole2};
14-
use winapi::um::objbase::COINIT_MULTITHREADED;
15+
use winapi::um::objbase::COINIT_APARTMENTTHREADED;
1516
use winapi::um::shobjidl_core::{CLSID_TaskbarList, ITaskbarList2};
1617
use winapi::um::wingdi::{CreateRectRgn, DeleteObject};
1718
use winapi::um::oleidl::LPDROPTARGET;
@@ -237,7 +238,7 @@ impl Window {
237238
}
238239

239240
pub(crate) fn set_min_dimensions_physical(&self, dimensions: Option<(u32, u32)>) {
240-
self.window_state.lock().unwrap().min_size = dimensions.map(Into::into);
241+
self.window_state.lock().min_size = dimensions.map(Into::into);
241242
// Make windows re-check the window size bounds.
242243
self.get_inner_size_physical()
243244
.map(|(width, height)| self.set_inner_size_physical(width, height));
@@ -253,7 +254,7 @@ impl Window {
253254
}
254255

255256
pub fn set_max_dimensions_physical(&self, dimensions: Option<(u32, u32)>) {
256-
self.window_state.lock().unwrap().max_size = dimensions.map(Into::into);
257+
self.window_state.lock().max_size = dimensions.map(Into::into);
257258
// Make windows re-check the window size bounds.
258259
self.get_inner_size_physical()
259260
.map(|(width, height)| self.set_inner_size_physical(width, height));
@@ -270,7 +271,7 @@ impl Window {
270271

271272
#[inline]
272273
pub fn set_resizable(&self, resizable: bool) {
273-
let mut window_state = self.window_state.lock().unwrap();
274+
let mut window_state = self.window_state.lock();
274275
if mem::replace(&mut window_state.resizable, resizable) != resizable {
275276
// If we're in fullscreen, update stored configuration but don't apply anything.
276277
if window_state.fullscreen.is_none() {
@@ -320,7 +321,7 @@ impl Window {
320321
MouseCursor::Help => winuser::IDC_HELP,
321322
_ => winuser::IDC_ARROW, // use arrow for the missing cases.
322323
});
323-
self.window_state.lock().unwrap().cursor = cursor_id;
324+
self.window_state.lock().cursor = cursor_id;
324325
self.events_loop_proxy.execute_in_thread(move || unsafe {
325326
let cursor = winuser::LoadCursorW(
326327
ptr::null_mut(),
@@ -376,7 +377,7 @@ impl Window {
376377
#[inline]
377378
pub fn grab_cursor(&self, grab: bool) -> Result<(), String> {
378379
let currently_grabbed = unsafe { self.cursor_is_grabbed() }?;
379-
let window_state_lock = self.window_state.lock().unwrap();
380+
let window_state_lock = self.window_state.lock();
380381
if currently_grabbed == grab && grab == window_state_lock.cursor_grabbed {
381382
return Ok(());
382383
}
@@ -386,7 +387,7 @@ impl Window {
386387
self.events_loop_proxy.execute_in_thread(move || {
387388
let result = unsafe { Self::grab_cursor_inner(&window, grab) };
388389
if result.is_ok() {
389-
window_state.lock().unwrap().cursor_grabbed = grab;
390+
window_state.lock().cursor_grabbed = grab;
390391
}
391392
let _ = tx.send(result);
392393
});
@@ -404,14 +405,14 @@ impl Window {
404405

405406
#[inline]
406407
pub fn hide_cursor(&self, hide: bool) {
407-
let window_state_lock = self.window_state.lock().unwrap();
408+
let window_state_lock = self.window_state.lock();
408409
// We don't want to increment/decrement the display count more than once!
409410
if hide == window_state_lock.cursor_hidden { return; }
410411
let (tx, rx) = channel();
411412
let window_state = Arc::clone(&self.window_state);
412413
self.events_loop_proxy.execute_in_thread(move || {
413414
unsafe { Self::hide_cursor_inner(hide) };
414-
window_state.lock().unwrap().cursor_hidden = hide;
415+
window_state.lock().cursor_hidden = hide;
415416
let _ = tx.send(());
416417
});
417418
drop(window_state_lock);
@@ -420,7 +421,7 @@ impl Window {
420421

421422
#[inline]
422423
pub fn get_hidpi_factor(&self) -> f64 {
423-
self.window_state.lock().unwrap().dpi_factor
424+
self.window_state.lock().dpi_factor
424425
}
425426

426427
fn set_cursor_position_physical(&self, x: i32, y: i32) -> Result<(), String> {
@@ -450,7 +451,7 @@ impl Window {
450451

451452
#[inline]
452453
pub fn set_maximized(&self, maximized: bool) {
453-
let mut window_state = self.window_state.lock().unwrap();
454+
let mut window_state = self.window_state.lock();
454455
if mem::replace(&mut window_state.maximized, maximized) != maximized {
455456
// We only maximize if we're not in fullscreen.
456457
if window_state.fullscreen.is_none() {
@@ -495,7 +496,7 @@ impl Window {
495496
(saved_window_info.style, saved_window_info.ex_style)
496497
}
497498

498-
unsafe fn restore_saved_window(&self, window_state_lock: &mut WindowState) {
499+
unsafe fn restore_saved_window(&self, mut window_state_lock: MutexGuard<WindowState>) {
499500
let (rect, mut style, ex_style) = {
500501
// 'saved_window_info' can be None if the window has never been
501502
// in fullscreen mode before this method gets called.
@@ -520,6 +521,7 @@ impl Window {
520521
let resizable = window_state_lock.resizable;
521522
let maximized = window_state_lock.maximized;
522523

524+
drop(window_state_lock);
523525
// We're restoring the window to its size and position from before being fullscreened.
524526
// `ShowWindow` resizes the window, so it must be called from the main thread.
525527
self.events_loop_proxy.execute_in_thread(move || {
@@ -558,23 +560,31 @@ impl Window {
558560

559561
mark_fullscreen(window.0, false);
560562

561-
let window_state_lock = window_state.lock().unwrap();
563+
let window_state_lock = window_state.lock();
562564
let _ = Self::grab_cursor_inner(&window, window_state_lock.cursor_grabbed);
563565
});
564566
}
565567

566568
#[inline]
567569
pub fn set_fullscreen(&self, monitor: Option<RootMonitorId>) {
568-
let mut window_state_lock = self.window_state.lock().unwrap();
570+
let mut window_state_lock = self.window_state.lock();
569571
unsafe {
570-
match &monitor {
571-
&Some(RootMonitorId { ref inner }) => {
572+
let monitor_rect = monitor.as_ref()
573+
.map(|RootMonitorId{ ref inner }| {
572574
let (x, y): (i32, i32) = inner.get_position().into();
573575
let (width, height): (u32, u32) = inner.get_dimensions().into();
576+
(x, y, width, height)
577+
});
578+
579+
match monitor_rect {
580+
Some((x, y, width, height)) => {
574581
let window = self.window.clone();
575582
let window_state = Arc::clone(&self.window_state);
576583

577584
let (style, ex_style) = self.set_fullscreen_style(&mut window_state_lock);
585+
window_state_lock.fullscreen = monitor;
586+
drop(window_state_lock);
587+
578588
self.events_loop_proxy.execute_in_thread(move || {
579589
let _ = Self::grab_cursor_inner(&window, false);
580590

@@ -609,25 +619,24 @@ impl Window {
609619

610620
mark_fullscreen(window.0, true);
611621

612-
let window_state_lock = window_state.lock().unwrap();
622+
let window_state_lock = window_state.lock();
613623
let _ = Self::grab_cursor_inner(&window, window_state_lock.cursor_grabbed);
614624
});
615-
}
616-
&None => {
617-
self.restore_saved_window(&mut window_state_lock);
625+
},
626+
None => {
627+
window_state_lock.fullscreen = None;
628+
self.restore_saved_window(window_state_lock)
618629
}
619630
}
620631
}
621-
622-
window_state_lock.fullscreen = monitor;
623632
}
624633

625634
#[inline]
626635
pub fn set_decorations(&self, decorations: bool) {
627-
let mut window_state = self.window_state.lock().unwrap();
636+
let mut window_state = self.window_state.lock();
628637
if mem::replace(&mut window_state.decorations, decorations) != decorations {
629-
let style_flags = (winuser::WS_CAPTION | winuser::WS_THICKFRAME) as LONG;
630-
let ex_style_flags = (winuser::WS_EX_WINDOWEDGE) as LONG;
638+
let style_flags = (winuser::WS_CAPTION | winuser::WS_THICKFRAME) as LONG;
639+
let ex_style_flags = (winuser::WS_EX_WINDOWEDGE) as LONG;
631640

632641
// if we are in fullscreen mode, we only change the saved window info
633642
if window_state.fullscreen.is_some() {
@@ -697,7 +706,7 @@ impl Window {
697706

698707
#[inline]
699708
pub fn set_always_on_top(&self, always_on_top: bool) {
700-
let mut window_state = self.window_state.lock().unwrap();
709+
let mut window_state = self.window_state.lock();
701710
if mem::replace(&mut window_state.always_on_top, always_on_top) != always_on_top {
702711
let window = self.window.clone();
703712
self.events_loop_proxy.execute_in_thread(move || {
@@ -739,7 +748,7 @@ impl Window {
739748
} else {
740749
icon::unset_for_window(self.window.0, IconType::Small);
741750
}
742-
self.window_state.lock().unwrap().window_icon = window_icon;
751+
self.window_state.lock().window_icon = window_icon;
743752
}
744753

745754
#[inline]
@@ -752,7 +761,7 @@ impl Window {
752761
} else {
753762
icon::unset_for_window(self.window.0, IconType::Big);
754763
}
755-
self.window_state.lock().unwrap().taskbar_icon = taskbar_icon;
764+
self.window_state.lock().taskbar_icon = taskbar_icon;
756765
}
757766

758767
#[inline]
@@ -1057,6 +1066,7 @@ unsafe fn init(
10571066

10581067
let file_drop_handler = {
10591068
use winapi::shared::winerror::{OLE_E_WRONGCOMPOBJ, RPC_E_CHANGED_MODE, S_OK};
1069+
10601070
let ole_init_result = ole2::OleInitialize(ptr::null_mut());
10611071
// It is ok if the initialize result is `S_FALSE` because it might happen that
10621072
// multiple windows are created on the same thread.
@@ -1136,7 +1146,7 @@ impl Drop for ComInitialized {
11361146
thread_local!{
11371147
static COM_INITIALIZED: ComInitialized = {
11381148
unsafe {
1139-
combaseapi::CoInitializeEx(ptr::null_mut(), COINIT_MULTITHREADED);
1149+
combaseapi::CoInitializeEx(ptr::null_mut(), COINIT_APARTMENTTHREADED);
11401150
ComInitialized(ptr::null_mut())
11411151
}
11421152
};

0 commit comments

Comments
 (0)