Skip to content

Commit

Permalink
feat: add crossbeam-channel (#181)
Browse files Browse the repository at this point in the history
* Cargo fmt

* Fix change file

* Add crossbea-channel

* Fix build and add change file

* Update change file
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) committed Aug 12, 2021
1 parent 89daf45 commit 6122dc5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changes/channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
tao: patch
---

Fix missing `Sync` trait on EventLoopProxy. This commit also introduces `crossbeam-channel` crate which could also improve the performance.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ log = "0.4"
serde = { version = "1", optional = true, features = [ "serde_derive" ] }
raw-window-handle = "0.3"
bitflags = "1"
crossbeam-channel = "0.5"

[dev-dependencies]
image = "0.23"
Expand Down
7 changes: 4 additions & 3 deletions src/platform_impl/ios/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use std::{
fmt::{self, Debug},
marker::PhantomData,
mem, ptr,
sync::mpsc::{self, Receiver, Sender},
};

use crossbeam_channel::{self as channel, Receiver, Sender};

use crate::{
dpi::LogicalSize,
event::Event,
Expand Down Expand Up @@ -85,7 +86,7 @@ impl<T: 'static> EventLoop<T> {
view::create_delegate_class();
}

let (sender_to_clone, receiver) = mpsc::channel();
let (sender_to_clone, receiver) = channel::unbounded();

// this line sets up the main run loop before `UIApplicationMain`
setup_control_flow_observers();
Expand Down Expand Up @@ -192,7 +193,7 @@ impl<T> EventLoopProxy<T> {
self
.sender
.send(event)
.map_err(|::std::sync::mpsc::SendError(x)| EventLoopClosed(x))?;
.map_err(|channel::SendError(x)| EventLoopClosed(x))?;
unsafe {
// let the main thread know there's a new event
CFRunLoopSourceSignal(self.source);
Expand Down
13 changes: 7 additions & 6 deletions src/platform_impl/linux/global_shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::{
global_shortcut::{GlobalShortcut as RootGlobalShortcut, ShortcutManagerError},
keyboard::KeyCode,
};
use crossbeam_channel::{self as channel, Receiver, Sender, TryRecvError};
use std::{
collections::HashMap,
ptr,
sync::{mpsc, Arc, Mutex},
sync::{Arc, Mutex},
};
use x11_dl::{keysym, xlib};

Expand All @@ -24,8 +25,8 @@ enum HotkeyMessage {
#[derive(Debug)]
pub struct ShortcutManager {
shortcuts: ListenerMap,
method_sender: mpsc::Sender<HotkeyMessage>,
method_receiver: mpsc::Receiver<HotkeyMessage>,
method_sender: Sender<HotkeyMessage>,
method_receiver: Receiver<HotkeyMessage>,
}

impl ShortcutManager {
Expand All @@ -36,8 +37,8 @@ impl ShortcutManager {

let event_loop_channel = _window_target.p.window_requests_tx.clone();

let (method_sender, thread_receiver) = mpsc::channel();
let (thread_sender, method_receiver) = mpsc::channel();
let (method_sender, thread_receiver) = channel::unbounded();
let (thread_sender, method_receiver) = channel::unbounded();

std::thread::spawn(move || {
let event_loop_channel = event_loop_channel.clone();
Expand Down Expand Up @@ -121,7 +122,7 @@ impl ShortcutManager {
return;
}
Err(err) => {
if let std::sync::mpsc::TryRecvError::Disconnected = err {
if let TryRecvError::Disconnected = err {
eprintln!("hotkey: try_recv error {}", err);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ use std::{
panic::{catch_unwind, resume_unwind, RefUnwindSafe, UnwindSafe},
process, ptr,
rc::{Rc, Weak},
sync::mpsc,
};

use cocoa::{
appkit::{NSApp, NSEventType::NSApplicationDefined},
base::{id, nil, YES},
foundation::{NSAutoreleasePool, NSPoint},
};

use crossbeam_channel::{self as channel, Receiver, Sender};
use scopeguard::defer;

use crate::{
Expand Down Expand Up @@ -65,13 +64,13 @@ impl PanicInfo {
}

pub struct EventLoopWindowTarget<T: 'static> {
pub sender: mpsc::Sender<T>, // this is only here to be cloned elsewhere
pub receiver: mpsc::Receiver<T>,
pub sender: Sender<T>, // this is only here to be cloned elsewhere
pub receiver: Receiver<T>,
}

impl<T> Default for EventLoopWindowTarget<T> {
fn default() -> Self {
let (sender, receiver) = mpsc::channel();
let (sender, receiver) = channel::unbounded();
EventLoopWindowTarget { sender, receiver }
}
}
Expand Down Expand Up @@ -244,11 +243,12 @@ pub fn stop_app_on_panic<F: FnOnce() -> R + UnwindSafe, R>(
}

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

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

impl<T> Drop for Proxy<T> {
fn drop(&mut self) {
Expand All @@ -265,7 +265,7 @@ impl<T> Clone for Proxy<T> {
}

impl<T> Proxy<T> {
fn new(sender: mpsc::Sender<T>) -> Self {
fn new(sender: Sender<T>) -> Self {
unsafe {
// just wake up the eventloop
extern "C" fn event_loop_proxy_handler(_: *mut c_void) {}
Expand All @@ -287,7 +287,7 @@ impl<T> Proxy<T> {
self
.sender
.send(event)
.map_err(|mpsc::SendError(x)| EventLoopClosed(x))?;
.map_err(|channel::SendError(x)| EventLoopClosed(x))?;
unsafe {
// let the main thread know there's a new event
CFRunLoopSourceSignal(self.source);
Expand Down
12 changes: 5 additions & 7 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@

mod runner;

use crossbeam_channel::{self as channel, Receiver, Sender};
use parking_lot::Mutex;
use std::{
cell::Cell,
collections::VecDeque,
marker::PhantomData,
mem, panic, ptr,
rc::Rc,
sync::{
mpsc::{self, Receiver, Sender},
Arc,
},
sync::Arc,
thread,
time::{Duration, Instant},
};
use winapi::shared::basetsd::{DWORD_PTR, UINT_PTR};

use winapi::{
ctypes::c_int,
shared::{
basetsd::{DWORD_PTR, UINT_PTR},
minwindef::{BOOL, DWORD, HIWORD, INT, LOWORD, LPARAM, LRESULT, UINT, WORD, WPARAM},
windef::{HWND, POINT, RECT},
windowsx, winerror,
Expand Down Expand Up @@ -493,6 +490,7 @@ pub struct EventLoopProxy<T: 'static> {
event_send: Sender<T>,
}
unsafe impl<T: Send + 'static> Send for EventLoopProxy<T> {}
unsafe impl<T: Send + 'static> Sync for EventLoopProxy<T> {}

impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
Expand Down Expand Up @@ -627,7 +625,7 @@ fn subclass_event_target_window<T>(
event_loop_runner: EventLoopRunnerShared<T>,
) -> Sender<T> {
unsafe {
let (tx, rx) = mpsc::channel();
let (tx, rx) = channel::unbounded();

let subclass_input = ThreadMsgTargetSubclassInput {
event_loop_runner,
Expand Down
14 changes: 4 additions & 10 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@
use mem::MaybeUninit;
use parking_lot::Mutex;
use raw_window_handle::{windows::WindowsHandle, RawWindowHandle};
use std::{
cell::Cell,
ffi::OsStr,
io, mem,
os::windows::ffi::OsStrExt,
ptr,
sync::{mpsc::channel, Arc},
};
use std::{cell::Cell, ffi::OsStr, io, mem, os::windows::ffi::OsStrExt, ptr, sync::Arc};

use crossbeam_channel as channel;
use winapi::{
ctypes::c_int,
shared::{
Expand Down Expand Up @@ -346,7 +340,7 @@ impl Window {
pub fn set_cursor_grab(&self, grab: bool) -> Result<(), ExternalError> {
let window = self.window.clone();
let window_state = Arc::clone(&self.window_state);
let (tx, rx) = channel();
let (tx, rx) = channel::unbounded();

self.thread_executor.execute_in_thread(move || {
let result = window_state
Expand All @@ -363,7 +357,7 @@ impl Window {
pub fn set_cursor_visible(&self, visible: bool) {
let window = self.window.clone();
let window_state = Arc::clone(&self.window_state);
let (tx, rx) = channel();
let (tx, rx) = channel::unbounded();

self.thread_executor.execute_in_thread(move || {
let result = window_state
Expand Down

0 comments on commit 6122dc5

Please sign in to comment.