Use a sync::Barrier on win32 to sync threads instead of a channel (#271)

This commit is contained in:
tomaka 2017-08-31 19:45:17 +02:00 committed by GitHub
parent 1b22e39fb2
commit e65cacbc86

View file

@ -21,6 +21,7 @@ use std::os::windows::io::AsRawHandle;
use std::ptr; use std::ptr;
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Barrier;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::Condvar; use std::sync::Condvar;
use std::thread; use std::thread;
@ -92,9 +93,10 @@ impl EventsLoop {
let win32_block_loop = Arc::new((Mutex::new(false), Condvar::new())); let win32_block_loop = Arc::new((Mutex::new(false), Condvar::new()));
let win32_block_loop_child = win32_block_loop.clone(); 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. // 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 || { let thread = thread::spawn(move || {
CONTEXT_STASH.with(|context_stash| { CONTEXT_STASH.with(|context_stash| {
@ -112,7 +114,8 @@ impl EventsLoop {
user32::IsGUIThread(1); user32::IsGUIThread(1);
// Then only we unblock the `new()` function. We are sure that we don't call // Then only we unblock the `new()` function. We are sure that we don't call
// `PostThreadMessageA()` before `new()` returns. // `PostThreadMessageA()` before `new()` returns.
local_block_tx.send(()).unwrap(); barrier_clone.wait();
drop(barrier_clone);
let mut msg = mem::uninitialized(); 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. // Blocks this function until the background thread has an events loop. See other comments.
local_block_rx.recv().unwrap(); barrier.wait();
EventsLoop { EventsLoop {
thread_id: unsafe { kernel32::GetThreadId(thread.as_raw_handle()) }, thread_id: unsafe { kernel32::GetThreadId(thread.as_raw_handle()) },