From e65cacbc86499c40aa5b676d9d47bc10727e4953 Mon Sep 17 00:00:00 2001 From: tomaka Date: Thu, 31 Aug 2017 19:45:17 +0200 Subject: [PATCH] Use a sync::Barrier on win32 to sync threads instead of a channel (#271) --- src/platform/windows/events_loop.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/platform/windows/events_loop.rs b/src/platform/windows/events_loop.rs index 7587c1a7..40f0bb7f 100644 --- a/src/platform/windows/events_loop.rs +++ b/src/platform/windows/events_loop.rs @@ -21,6 +21,7 @@ use std::os::windows::io::AsRawHandle; use std::ptr; use std::sync::mpsc; use std::sync::Arc; +use std::sync::Barrier; use std::sync::Mutex; use std::sync::Condvar; use std::thread; @@ -92,9 +93,10 @@ impl EventsLoop { let win32_block_loop = Arc::new((Mutex::new(false), Condvar::new())); let win32_block_loop_child = win32_block_loop.clone(); - // Local channel in order to block the `new()` function until the background thread has + // Local barrier in order to block the `new()` function until the background thread has // an events queue. - let (local_block_tx, local_block_rx) = mpsc::channel(); + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); let thread = thread::spawn(move || { CONTEXT_STASH.with(|context_stash| { @@ -112,7 +114,8 @@ impl EventsLoop { user32::IsGUIThread(1); // Then only we unblock the `new()` function. We are sure that we don't call // `PostThreadMessageA()` before `new()` returns. - local_block_tx.send(()).unwrap(); + barrier_clone.wait(); + drop(barrier_clone); let mut msg = mem::uninitialized(); @@ -142,7 +145,7 @@ impl EventsLoop { }); // Blocks this function until the background thread has an events loop. See other comments. - local_block_rx.recv().unwrap(); + barrier.wait(); EventsLoop { thread_id: unsafe { kernel32::GetThreadId(thread.as_raw_handle()) },