Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sync for EventLoopProxy (except on Web) #2749

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- Bump MSRV from `1.60` to `1.64`.
- On macOS, fixed potential panic when getting refresh rate.
- On macOS, fix crash when calling `Window::set_ime_position` from another thread.
- On all platforms except Web, implement `Sync` for `EventLoopProxy`.

# 0.28.3

Expand Down
10 changes: 6 additions & 4 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
hash::Hash,
sync::{
atomic::{AtomicBool, Ordering},
mpsc, Arc, RwLock,
mpsc, Arc, Mutex, RwLock,
},
time::{Duration, Instant},
};
Expand Down Expand Up @@ -788,21 +788,21 @@ impl<T: 'static> EventLoop<T> {

pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy {
user_events_sender: self.user_events_sender.clone(),
user_events_sender: Mutex::new(self.user_events_sender.clone()),
waker: self.android_app.create_waker(),
}
}
}

pub struct EventLoopProxy<T: 'static> {
user_events_sender: mpsc::Sender<T>,
user_events_sender: Mutex<mpsc::Sender<T>>,
waker: AndroidAppWaker,
}

impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
EventLoopProxy {
user_events_sender: self.user_events_sender.clone(),
user_events_sender: Mutex::new(self.user_events_sender.lock().unwrap().clone()),
waker: self.waker.clone(),
}
}
Expand All @@ -811,6 +811,8 @@ impl<T: 'static> Clone for EventLoopProxy<T> {
impl<T> EventLoopProxy<T> {
pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed<T>> {
self.user_events_sender
.lock()
.unwrap()
.send(event)
.map_err(|err| event_loop::EventLoopClosed(err.0))?;
self.waker.wake();
Expand Down
13 changes: 10 additions & 3 deletions src/platform_impl/ios/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
marker::PhantomData,
ptr,
sync::mpsc::{self, Receiver, Sender},
sync::Mutex,
};

use core_foundation::base::{CFIndex, CFRelease};
Expand Down Expand Up @@ -153,15 +154,16 @@ impl<T: 'static> EventLoop<T> {
}

pub struct EventLoopProxy<T> {
sender: Sender<T>,
sender: Mutex<Sender<T>>,
source: CFRunLoopSourceRef,
}

unsafe impl<T: Send> Send for EventLoopProxy<T> {}
unsafe impl<T: Send> Sync for EventLoopProxy<T> {}

impl<T> Clone for EventLoopProxy<T> {
fn clone(&self) -> EventLoopProxy<T> {
EventLoopProxy::new(self.sender.clone())
EventLoopProxy::new(self.sender.lock().unwrap().clone())
}
}

Expand Down Expand Up @@ -200,12 +202,17 @@ impl<T> EventLoopProxy<T> {
CFRunLoopAddSource(rl, source, kCFRunLoopCommonModes);
CFRunLoopWakeUp(rl);

EventLoopProxy { sender, source }
EventLoopProxy {
sender: Mutex::new(sender),
source,
}
}
}

pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.sender
.lock()
.unwrap()
.send(event)
.map_err(|::std::sync::mpsc::SendError(x)| EventLoopClosed(x))?;
unsafe {
Expand Down
11 changes: 8 additions & 3 deletions src/platform_impl/linux/wayland/event_loop/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
//! An event loop proxy.

use std::sync::mpsc::SendError;
use std::sync::Mutex;

use sctk::reexports::calloop::channel::Sender;

use crate::event_loop::EventLoopClosed;

/// A handle that can be sent across the threads and used to wake up the `EventLoop`.
pub struct EventLoopProxy<T: 'static> {
user_events_sender: Sender<T>,
user_events_sender: Mutex<Sender<T>>,
}

impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
EventLoopProxy {
user_events_sender: self.user_events_sender.clone(),
user_events_sender: Mutex::new(self.user_events_sender.lock().unwrap().clone()),
}
}
}

impl<T: 'static> EventLoopProxy<T> {
pub fn new(user_events_sender: Sender<T>) -> Self {
Self { user_events_sender }
Self {
user_events_sender: Mutex::new(user_events_sender),
}
}

pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.user_events_sender
.lock()
.unwrap()
.send(event)
.map_err(|SendError(error)| EventLoopClosed(error))
}
Expand Down
13 changes: 9 additions & 4 deletions src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ use std::{
ptr,
rc::Rc,
slice,
sync::mpsc::{Receiver, Sender, TryRecvError},
sync::{mpsc, Arc, Weak},
sync::{
mpsc::{Receiver, Sender, TryRecvError},
Mutex,
},
time::{Duration, Instant},
};

Expand Down Expand Up @@ -123,14 +126,14 @@ pub struct EventLoop<T: 'static> {
}

pub struct EventLoopProxy<T: 'static> {
user_sender: Sender<T>,
user_sender: Mutex<Sender<T>>,
waker: Arc<Waker>,
}

impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
EventLoopProxy {
user_sender: self.user_sender.clone(),
user_sender: Mutex::new(self.user_sender.lock().unwrap().clone()),
waker: self.waker.clone(),
}
}
Expand Down Expand Up @@ -294,7 +297,7 @@ impl<T: 'static> EventLoop<T> {

pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy {
user_sender: self.user_sender.clone(),
user_sender: Mutex::new(self.user_sender.clone()),
waker: self.waker.clone(),
}
}
Expand Down Expand Up @@ -578,6 +581,8 @@ impl<T> EventLoopWindowTarget<T> {
impl<T: 'static> EventLoopProxy<T> {
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.user_sender
.lock()
.unwrap()
.send(event)
.map_err(|e| EventLoopClosed(e.0))
.map(|_| self.waker.wake().unwrap())
Expand Down
14 changes: 10 additions & 4 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
panic::{catch_unwind, resume_unwind, RefUnwindSafe, UnwindSafe},
process, ptr,
rc::{Rc, Weak},
sync::mpsc,
sync::{mpsc, Mutex},
};

use core_foundation::base::{CFIndex, CFRelease};
Expand Down Expand Up @@ -264,11 +264,12 @@ pub fn stop_app_on_panic<F: FnOnce() -> R + UnwindSafe, R>(
}

pub struct EventLoopProxy<T> {
sender: mpsc::Sender<T>,
sender: Mutex<mpsc::Sender<T>>,
source: CFRunLoopSourceRef,
}

unsafe impl<T: Send> Send for EventLoopProxy<T> {}
unsafe impl<T: Send> Sync for EventLoopProxy<T> {}

impl<T> Drop for EventLoopProxy<T> {
fn drop(&mut self) {
Expand All @@ -280,7 +281,7 @@ impl<T> Drop for EventLoopProxy<T> {

impl<T> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
EventLoopProxy::new(self.sender.clone())
EventLoopProxy::new(self.sender.lock().unwrap().clone())
}
}

Expand Down Expand Up @@ -310,12 +311,17 @@ impl<T> EventLoopProxy<T> {
CFRunLoopAddSource(rl, source, kCFRunLoopCommonModes);
CFRunLoopWakeUp(rl);

EventLoopProxy { sender, source }
EventLoopProxy {
sender: Mutex::new(sender),
source,
}
}
}

pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.sender
.lock()
.unwrap()
.send(event)
.map_err(|mpsc::SendError(x)| EventLoopClosed(x))?;
unsafe {
Expand Down
8 changes: 5 additions & 3 deletions src/platform_impl/orbital/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,20 +648,22 @@ impl<T: 'static> EventLoop<T> {

pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy {
user_events_sender: self.window_target.p.user_events_sender.clone(),
user_events_sender: Mutex::new(self.window_target.p.user_events_sender.clone()),
wake_socket: self.window_target.p.wake_socket.clone(),
}
}
}

pub struct EventLoopProxy<T: 'static> {
user_events_sender: mpsc::Sender<T>,
user_events_sender: Mutex<mpsc::Sender<T>>,
wake_socket: Arc<TimeSocket>,
}

impl<T> EventLoopProxy<T> {
pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed<T>> {
self.user_events_sender
.lock()
.unwrap()
.send(event)
.map_err(|mpsc::SendError(x)| event_loop::EventLoopClosed(x))?;

Expand All @@ -674,7 +676,7 @@ impl<T> EventLoopProxy<T> {
impl<T> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
Self {
user_events_sender: self.user_events_sender.clone(),
user_events_sender: Mutex::new(self.user_events_sender.lock().unwrap().clone()),
wake_socket: self.wake_socket.clone(),
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl<T: 'static> EventLoop<T> {
pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy {
target_window: self.window_target.p.thread_msg_target,
event_send: self.thread_msg_sender.clone(),
event_send: Mutex::new(self.thread_msg_sender.clone()),
}
}
}
Expand Down Expand Up @@ -575,15 +575,15 @@ type ThreadExecFn = Box<Box<dyn FnMut()>>;

pub struct EventLoopProxy<T: 'static> {
target_window: HWND,
event_send: Sender<T>,
event_send: Mutex<Sender<T>>,
}
unsafe impl<T: Send + 'static> Send for EventLoopProxy<T> {}

impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
Self {
target_window: self.target_window,
event_send: self.event_send.clone(),
event_send: Mutex::new(self.event_send.lock().unwrap().clone()),
}
}
}
Expand All @@ -592,7 +592,7 @@ impl<T: 'static> EventLoopProxy<T> {
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
unsafe {
if PostMessageW(self.target_window, USER_EVENT_MSG_ID.get(), 0, 0) != false.into() {
self.event_send.send(event).ok();
self.event_send.lock().unwrap().send(event).ok();
Ok(())
} else {
Err(EventLoopClosed(event))
Expand Down
10 changes: 10 additions & 0 deletions tests/sync_object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#[allow(dead_code)]
fn needs_sync<T: Sync>() {}

#[cfg(not(wasm_platform))]
#[test]
fn event_loop_proxy_send() {
#[allow(dead_code)]
fn is_send<T: 'static + Send>() {
// ensures that `winit::EventLoopProxy` implements `Sync`
needs_sync::<winit::event_loop::EventLoopProxy<T>>();
}
}

#[cfg(not(wasm_platform))]
#[test]
fn window_sync() {
Expand Down