Skip to content

Commit 658209f

Browse files
committed
Made mutexes finer for less deadlock risk
1 parent 8e6b986 commit 658209f

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

src/platform_impl/macos/app_state.rs

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use {
1212
use platform_impl::platform::{observer::EventLoopWaker, util::Never};
1313

1414
lazy_static! {
15-
static ref HANDLER: Mutex<Handler> = Default::default();
16-
static ref EVENTS: Mutex<VecDeque<Event<Never>>> = Default::default();
15+
static ref HANDLER: Handler = Default::default();
1716
}
1817

1918
impl Event<Never> {
@@ -26,7 +25,7 @@ impl Event<Never> {
2625
}
2726

2827
pub trait EventHandler: Debug {
29-
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: *mut ControlFlow);
28+
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow);
3029
//fn handle_user_events(&mut self, control_flow: &mut ControlFlow);
3130
}
3231

@@ -48,11 +47,11 @@ where
4847
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
4948
T: 'static,
5049
{
51-
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: *mut ControlFlow) {
50+
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow) {
5251
(self.callback)(
5352
event.userify(),
5453
&self.window_target,
55-
unsafe { &mut *control_flow },
54+
control_flow,
5655
);
5756
}
5857

@@ -69,67 +68,77 @@ where
6968

7069
#[derive(Default)]
7170
struct Handler {
72-
control_flow: ControlFlow,
73-
control_flow_prev: ControlFlow,
74-
callback: Option<Box<dyn EventHandler>>,
75-
waker: EventLoopWaker,
71+
control_flow: Mutex<ControlFlow>,
72+
control_flow_prev: Mutex<ControlFlow>,
73+
callback: Mutex<Option<Box<dyn EventHandler>>>,
74+
pending_events: Mutex<VecDeque<Event<Never>>>,
75+
waker: Mutex<EventLoopWaker>,
7676
}
7777

7878
unsafe impl Send for Handler {}
7979
unsafe impl Sync for Handler {}
8080

8181
impl Handler {
82-
fn handle_nonuser_event(&mut self, event: Event<Never>) {
83-
let control_flow = &mut self.control_flow;
84-
if let Some(ref mut callback) = self.callback {
85-
callback.handle_nonuser_event(event, control_flow);
86-
}
82+
fn events<'a>(&'a self) -> MutexGuard<'a, VecDeque<Event<Never>>> {
83+
self.pending_events.lock().unwrap()
8784
}
88-
}
8985

90-
pub enum AppState {}
86+
fn waker<'a>(&'a self) -> MutexGuard<'a, EventLoopWaker> {
87+
self.waker.lock().unwrap()
88+
}
9189

92-
impl AppState {
93-
fn handler() -> MutexGuard<'static, Handler> {
94-
HANDLER.lock().unwrap()
90+
fn get_control_flow_and_update_prev(&self) -> ControlFlow {
91+
let control_flow = self.control_flow.lock().unwrap();
92+
*self.control_flow_prev.lock().unwrap() = *control_flow;
93+
*control_flow
9594
}
9695

97-
fn events() -> MutexGuard<'static, VecDeque<Event<Never>>> {
98-
EVENTS.lock().unwrap()
96+
fn get_old_and_new_control_flow(&self) -> (ControlFlow, ControlFlow) {
97+
let old = *self.control_flow_prev.lock().unwrap();
98+
let new = *self.control_flow.lock().unwrap();
99+
(old, new)
99100
}
100101

102+
fn take_events(&self) -> VecDeque<Event<Never>> {
103+
mem::replace(&mut *self.events(), Default::default())
104+
}
105+
106+
fn handle_nonuser_event(&self, event: Event<Never>) {
107+
if let Some(ref mut callback) = *self.callback.lock().unwrap() {
108+
callback.handle_nonuser_event(
109+
event,
110+
&mut *self.control_flow.lock().unwrap(),
111+
);
112+
}
113+
}
114+
}
115+
116+
pub enum AppState {}
117+
118+
impl AppState {
101119
pub fn set_callback<F, T>(callback: F, window_target: RootWindowTarget<T>)
102120
where
103121
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
104122
T: 'static,
105123
{
106-
Self::handler().callback = Some(Box::new(EventLoopHandler {
124+
*HANDLER.callback.lock().unwrap() = Some(Box::new(EventLoopHandler {
107125
callback,
108126
window_target,
109127
}));
110128
}
111129

112130
pub fn exit() -> ! {
113-
let mut handler = Self::handler();
114-
if let Some(mut callback) = handler.callback.take() {
115-
callback.handle_nonuser_event(
116-
Event::LoopDestroyed,
117-
&mut handler.control_flow,
118-
);
119-
}
131+
HANDLER.handle_nonuser_event(Event::LoopDestroyed);
120132
std::process::exit(0)
121133
}
122134

123135
pub fn launched() {
124-
let mut handler = Self::handler();
125-
handler.waker.start();
126-
handler.handle_nonuser_event(Event::NewEvents(StartCause::Init));
136+
HANDLER.waker().start();
137+
HANDLER.handle_nonuser_event(Event::NewEvents(StartCause::Init));
127138
}
128139

129140
pub fn wakeup() {
130-
let mut handler = Self::handler();
131-
handler.control_flow_prev = handler.control_flow;
132-
let cause = match handler.control_flow {
141+
let cause = match HANDLER.get_control_flow_and_update_prev() {
133142
ControlFlow::Poll => StartCause::Poll,
134143
/*ControlFlow::Wait => StartCause::WaitCancelled {
135144
start,
@@ -151,33 +160,29 @@ impl AppState {
151160
ControlFlow::Exit => StartCause::Poll,//panic!("unexpected `ControlFlow::Exit`"),
152161
_ => unimplemented!(),
153162
};
154-
handler.handle_nonuser_event(Event::NewEvents(cause));
163+
HANDLER.handle_nonuser_event(Event::NewEvents(cause));
155164
}
156165

157166
pub fn queue_event(event: Event<Never>) {
158-
Self::events().push_back(event);
167+
HANDLER.events().push_back(event);
159168
}
160169

161170
pub fn queue_events(mut events: VecDeque<Event<Never>>) {
162-
Self::events().append(&mut events);
171+
HANDLER.events().append(&mut events);
163172
}
164173

165174
pub fn cleared() {
166-
let mut handler = Self::handler();
167-
handler.handle_nonuser_event(Event::EventsCleared);
168-
let events = mem::replace(&mut *Self::events(), Default::default());
169-
for event in events {
170-
handler.handle_nonuser_event(event);
175+
HANDLER.handle_nonuser_event(Event::EventsCleared);
176+
for event in HANDLER.take_events() {
177+
HANDLER.handle_nonuser_event(event);
171178
}
172-
let old = handler.control_flow_prev;
173-
let new = handler.control_flow;
174-
match (old, new) {
179+
match HANDLER.get_old_and_new_control_flow() {
175180
(ControlFlow::Poll, ControlFlow::Poll) => (),
176181
(ControlFlow::Wait, ControlFlow::Wait) => (),
177182
(ControlFlow::WaitUntil(old_instant), ControlFlow::WaitUntil(new_instant)) if old_instant == new_instant => (),
178-
(_, ControlFlow::Wait) => handler.waker.stop(),
179-
(_, ControlFlow::WaitUntil(new_instant)) => handler.waker.start_at(new_instant),
180-
(_, ControlFlow::Poll) => handler.waker.start(),
183+
(_, ControlFlow::Wait) => HANDLER.waker().stop(),
184+
(_, ControlFlow::WaitUntil(new_instant)) => HANDLER.waker().start_at(new_instant),
185+
(_, ControlFlow::Poll) => HANDLER.waker().start(),
181186
(_, ControlFlow::Exit) => {
182187
let _: () = unsafe { msg_send![NSApp(), stop:nil] };
183188
},

0 commit comments

Comments
 (0)