Make EventLoopProxy Sync
Co-authored-by: daxpedda <daxpedda@gmail.com> Closes: #3448
This commit is contained in:
parent
e61a7320a2
commit
31f8b816bd
9 changed files with 26 additions and 13 deletions
|
|
@ -5,7 +5,7 @@ use std::{
|
|||
sync::{Arc, Condvar, Mutex},
|
||||
};
|
||||
|
||||
pub struct Dispatcher<T: 'static>(Wrapper<true, T, Sender<Closure<T>>, Closure<T>>);
|
||||
pub struct Dispatcher<T: 'static>(Wrapper<T, Sender<Closure<T>>, Closure<T>>);
|
||||
|
||||
struct Closure<T>(Box<dyn FnOnce(&T) + Send>);
|
||||
|
||||
|
|
@ -85,7 +85,7 @@ impl<T> Dispatcher<T> {
|
|||
}
|
||||
|
||||
pub struct DispatchRunner<T: 'static> {
|
||||
wrapper: Wrapper<true, T, Sender<Closure<T>>, Closure<T>>,
|
||||
wrapper: Wrapper<T, Sender<Closure<T>>, Closure<T>>,
|
||||
receiver: Receiver<Closure<T>>,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ use std::task::Poll;
|
|||
use super::super::main_thread::MainThreadMarker;
|
||||
use super::{AtomicWaker, Wrapper};
|
||||
|
||||
pub struct WakerSpawner<T: 'static>(Wrapper<false, Handler<T>, Sender, usize>);
|
||||
pub struct WakerSpawner<T: 'static>(Wrapper<Handler<T>, Sender, usize>);
|
||||
|
||||
pub struct Waker<T: 'static>(Wrapper<false, Handler<T>, Sender, usize>);
|
||||
pub struct Waker<T: 'static>(Wrapper<Handler<T>, Sender, usize>);
|
||||
|
||||
struct Handler<T> {
|
||||
value: T,
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ use std::sync::Arc;
|
|||
|
||||
// Unsafe wrapper type that allows us to use `T` when it's not `Send` from other threads.
|
||||
// `value` **must** only be accessed on the main thread.
|
||||
pub struct Wrapper<const SYNC: bool, V: 'static, S: Clone + Send, E> {
|
||||
value: Value<SYNC, V>,
|
||||
pub struct Wrapper<V: 'static, S: Clone + Send, E> {
|
||||
value: Value<V>,
|
||||
handler: fn(&RefCell<Option<V>>, E),
|
||||
sender_data: S,
|
||||
sender_handler: fn(&S, E),
|
||||
}
|
||||
|
||||
struct Value<const SYNC: bool, V> {
|
||||
struct Value<V> {
|
||||
// SAFETY:
|
||||
// This value must not be accessed if not on the main thread.
|
||||
//
|
||||
|
|
@ -28,11 +28,11 @@ struct Value<const SYNC: bool, V> {
|
|||
}
|
||||
|
||||
// SAFETY: See `Self::value`.
|
||||
unsafe impl<const SYNC: bool, V> Send for Value<SYNC, V> {}
|
||||
unsafe impl<V> Send for Value<V> {}
|
||||
// SAFETY: See `Self::value`.
|
||||
unsafe impl<V> Sync for Value<true, V> {}
|
||||
unsafe impl<V> Sync for Value<V> {}
|
||||
|
||||
impl<const SYNC: bool, V, S: Clone + Send, E> Wrapper<SYNC, V, S, E> {
|
||||
impl<V, S: Clone + Send, E> Wrapper<V, S, E> {
|
||||
#[track_caller]
|
||||
pub fn new<R: Future<Output = ()>>(
|
||||
_: MainThreadMarker,
|
||||
|
|
@ -81,7 +81,7 @@ impl<const SYNC: bool, V, S: Clone + Send, E> Wrapper<SYNC, V, S, E> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<const SYNC: bool, V, S: Clone + Send, E> Clone for Wrapper<SYNC, V, S, E> {
|
||||
impl<V, S: Clone + Send, E> Clone for Wrapper<V, S, E> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
value: Value {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue