@@ -2,20 +2,20 @@ use std::ffi::CStr;
2
2
use std:: fmt:: { Debug , Formatter } ;
3
3
use x11:: xlib;
4
4
5
+ use std:: cell:: RefCell ;
5
6
use std:: panic:: AssertUnwindSafe ;
6
- use std:: sync:: Mutex ;
7
7
8
8
thread_local ! {
9
9
/// 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 ) ;
13
13
}
14
14
15
15
/// A helper struct for safe X11 error handling
16
16
pub struct XErrorHandler < ' a > {
17
17
display : * mut xlib:: Display ,
18
- mutex : & ' a Mutex < Option < xlib:: XErrorEvent > > ,
18
+ error : & ' a RefCell < Option < xlib:: XErrorEvent > > ,
19
19
}
20
20
21
21
impl < ' a > XErrorHandler < ' a > {
@@ -25,7 +25,7 @@ impl<'a> XErrorHandler<'a> {
25
25
unsafe {
26
26
xlib:: XSync ( self . display , 0 ) ;
27
27
}
28
- let error = self . mutex . lock ( ) . unwrap ( ) . take ( ) ;
28
+ let error = self . error . borrow_mut ( ) . take ( ) ;
29
29
30
30
match error {
31
31
None => Ok ( ( ) ) ,
@@ -44,20 +44,16 @@ impl<'a> XErrorHandler<'a> {
44
44
// SAFETY: the error pointer should be safe to copy
45
45
let err = * err;
46
46
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
+ }
61
57
}
62
58
} )
63
59
}
@@ -67,13 +63,13 @@ impl<'a> XErrorHandler<'a> {
67
63
xlib:: XSync ( display, 0 ) ;
68
64
}
69
65
70
- CURRENT_X11_ERROR . with ( |mutex | {
66
+ CURRENT_X11_ERROR . with ( |error | {
71
67
// Make sure to clear any errors from the last call to this function
72
- * mutex . lock ( ) . unwrap ( ) = None ;
68
+ * error . borrow_mut ( ) = None ;
73
69
74
70
let old_handler = unsafe { xlib:: XSetErrorHandler ( Some ( error_handler) ) } ;
75
71
let panic_result = std:: panic:: catch_unwind ( AssertUnwindSafe ( || {
76
- let mut h = XErrorHandler { display, mutex : & mutex } ;
72
+ let mut h = XErrorHandler { display, error } ;
77
73
handler ( & mut h)
78
74
} ) ) ;
79
75
// Whatever happened, restore old error handler
0 commit comments