Skip to content

Commit 85b6437

Browse files
committed
Swap out X11 error handling mutex for a RefCell
1 parent 0aae938 commit 85b6437

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/gl/x11/errors.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ use std::ffi::CStr;
22
use std::fmt::{Debug, Formatter};
33
use x11::xlib;
44

5+
use std::cell::RefCell;
56
use std::panic::AssertUnwindSafe;
6-
use std::sync::Mutex;
77

88
thread_local! {
99
/// Used as part of [`XerrorHandler::handle()`]. When an X11 error occurs during this function,
10-
/// the error gets copied to this mutex after which the program is allowed to resume. The error
11-
/// can then be converted to a regular Rust Result value afterwards.
12-
static CURRENT_X11_ERROR: Mutex<Option<xlib::XErrorEvent>> = Mutex::new(None);
10+
/// the error gets copied to this RefCell after which the program is allowed to resume. The
11+
/// error can then be converted to a regular Rust Result value afterwards.
12+
static CURRENT_X11_ERROR: RefCell<Option<xlib::XErrorEvent>> = RefCell::new(None);
1313
}
1414

1515
/// A helper struct for safe X11 error handling
1616
pub struct XErrorHandler<'a> {
1717
display: *mut xlib::Display,
18-
mutex: &'a Mutex<Option<xlib::XErrorEvent>>,
18+
error: &'a RefCell<Option<xlib::XErrorEvent>>,
1919
}
2020

2121
impl<'a> XErrorHandler<'a> {
@@ -25,7 +25,7 @@ impl<'a> XErrorHandler<'a> {
2525
unsafe {
2626
xlib::XSync(self.display, 0);
2727
}
28-
let error = self.mutex.lock().unwrap().take();
28+
let error = self.error.borrow_mut().take();
2929

3030
match error {
3131
None => Ok(()),
@@ -44,20 +44,16 @@ impl<'a> XErrorHandler<'a> {
4444
// SAFETY: the error pointer should be safe to copy
4545
let err = *err;
4646

47-
CURRENT_X11_ERROR.with(|mutex| match mutex.lock() {
48-
// If multiple errors occur, keep the first one since that's likely going to be the
49-
// cause of the other errors
50-
Ok(mut current_error) if current_error.is_none() => {
51-
*current_error = Some(err);
52-
0
53-
}
54-
Ok(_) => 0,
55-
Err(e) => {
56-
eprintln!(
57-
"[FATAL] raw-gl-context: Failed to lock for X11 Error Handler: {:?}",
58-
e
59-
);
60-
1
47+
CURRENT_X11_ERROR.with(|error| {
48+
let mut error = error.borrow_mut();
49+
match error.as_mut() {
50+
// If multiple errors occur, keep the first one since that's likely going to be the
51+
// cause of the other errors
52+
Some(_) => 1,
53+
None => {
54+
*error = Some(err);
55+
0
56+
}
6157
}
6258
})
6359
}
@@ -67,13 +63,13 @@ impl<'a> XErrorHandler<'a> {
6763
xlib::XSync(display, 0);
6864
}
6965

70-
CURRENT_X11_ERROR.with(|mutex| {
66+
CURRENT_X11_ERROR.with(|error| {
7167
// Make sure to clear any errors from the last call to this function
72-
*mutex.lock().unwrap() = None;
68+
*error.borrow_mut() = None;
7369

7470
let old_handler = unsafe { xlib::XSetErrorHandler(Some(error_handler)) };
7571
let panic_result = std::panic::catch_unwind(AssertUnwindSafe(|| {
76-
let mut h = XErrorHandler { display, mutex: &mutex };
72+
let mut h = XErrorHandler { display, error };
7773
handler(&mut h)
7874
}));
7975
// Whatever happened, restore old error handler

0 commit comments

Comments
 (0)