Skip to content

Commit 89f9f38

Browse files
authored
Merge pull request #36 from compio-rs/dev/advanced-pollable
feat(pollable): move common used code
2 parents ca97049 + 2463bc4 commit 89f9f38

8 files changed

Lines changed: 53 additions & 96 deletions

File tree

winio-pollable/src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#[cfg(target_os = "linux")]
99
use std::os::fd::OwnedFd;
1010
use std::{
11+
future::Future,
1112
io,
1213
ops::{Deref, DerefMut},
14+
time::Duration,
1315
};
1416

1517
use compio::driver::{AsRawFd, RawFd};
@@ -44,7 +46,7 @@ impl Runtime {
4446
}
4547

4648
/// Clear the eventfd, if possible.
47-
pub fn clear(&self) -> io::Result<()> {
49+
pub(crate) fn clear(&self) -> io::Result<()> {
4850
if let Some(efd) = &self.efd {
4951
let mut buf = [0u8; 8];
5052
rustix::io::read(efd, &mut buf)?;
@@ -63,11 +65,42 @@ impl Runtime {
6365
}
6466

6567
/// Clear the eventfd, if possible.
66-
pub fn clear(&self) -> io::Result<()> {
68+
pub(crate) fn clear(&self) -> io::Result<()> {
6769
Ok(())
6870
}
6971
}
7072

73+
impl Runtime {
74+
/// Block on the future till it completes. Users should enter the runtime
75+
/// before calling this function, and poll the runtime themselves.
76+
pub fn block_on<F: Future>(&self, future: F, poll: impl Fn(Option<Duration>)) -> F::Output {
77+
let mut result = None;
78+
unsafe {
79+
self.runtime
80+
.spawn_unchecked(async { result = Some(future.await) })
81+
}
82+
.detach();
83+
loop {
84+
self.runtime.poll_with(Some(Duration::ZERO));
85+
86+
let remaining_tasks = self.runtime.run();
87+
if let Some(result) = result.take() {
88+
break result;
89+
}
90+
91+
let timeout = if remaining_tasks {
92+
Some(Duration::ZERO)
93+
} else {
94+
self.runtime.current_timeout()
95+
};
96+
97+
poll(timeout);
98+
99+
self.clear().ok();
100+
}
101+
}
102+
}
103+
71104
impl Deref for Runtime {
72105
type Target = compio::runtime::Runtime;
73106

winio-ui-app-kit/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ repository = { workspace = true }
1212
winio-primitive = { workspace = true }
1313
winio-handle = { workspace = true }
1414
winio-callback = { workspace = true }
15+
winio-pollable = { workspace = true }
1516

16-
compio = { workspace = true }
17+
compio = { workspace = true, features = ["arrayvec"] }
1718

1819
inherit-methods-macro = { workspace = true }
1920
image = { workspace = true, default-features = false }

winio-ui-app-kit/src/runtime.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use objc2_core_foundation::{
1010
use objc2_foundation::{MainThreadMarker, NSDate, NSDefaultRunLoopMode};
1111

1212
pub struct Runtime {
13-
runtime: compio::runtime::Runtime,
13+
runtime: winio_pollable::Runtime,
1414
fd_source: CFRetained<CFFileDescriptor>,
1515
ns_app: Retained<NSApplication>,
1616
}
@@ -23,7 +23,7 @@ impl Default for Runtime {
2323

2424
impl Runtime {
2525
pub fn new() -> Self {
26-
let runtime = compio::runtime::Runtime::new().unwrap();
26+
let runtime = winio_pollable::Runtime::new().unwrap();
2727

2828
unsafe extern "C-unwind" fn callback(
2929
_fdref: *mut CFFileDescriptor,
@@ -73,25 +73,7 @@ impl Runtime {
7373

7474
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
7575
self.enter(|| {
76-
let mut result = None;
77-
unsafe {
78-
self.runtime
79-
.spawn_unchecked(async { result = Some(future.await) })
80-
}
81-
.detach();
82-
loop {
83-
self.runtime.poll_with(Some(Duration::ZERO));
84-
85-
let remaining_tasks = self.runtime.run();
86-
if let Some(result) = result.take() {
87-
break result;
88-
}
89-
90-
let timeout = if remaining_tasks {
91-
Some(Duration::ZERO)
92-
} else {
93-
self.runtime.current_timeout()
94-
};
76+
self.runtime.block_on(future, |timeout| {
9577
self.fd_source
9678
.enable_call_backs(kCFFileDescriptorReadCallBack);
9779
CFRunLoop::run_in_mode(
@@ -114,7 +96,7 @@ impl Runtime {
11496
}
11597
}
11698
}
117-
}
99+
})
118100
})
119101
}
120102
}

winio-ui-app-kit/src/ui/msgbox.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{cell::Cell, rc::Rc};
22

33
use block2::StackBlock;
4-
use compio::buf::arrayvec::ArrayVec;
4+
use compio::arrayvec::ArrayVec;
55
use objc2::rc::Retained;
66
use objc2_app_kit::{
77
NSAlert, NSAlertFirstButtonReturn, NSAlertStyle, NSImage, NSImageNameCaution, NSImageNameInfo,

winio-ui-gtk/src/runtime.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{future::Future, os::fd::AsRawFd, time::Duration};
1+
use std::{future::Future, os::fd::AsRawFd};
22

33
use gtk4::{
44
gio::{self, prelude::ApplicationExt},
@@ -45,25 +45,7 @@ impl Runtime {
4545

4646
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
4747
self.enter(|| {
48-
let mut result = None;
49-
unsafe {
50-
self.runtime
51-
.spawn_unchecked(async { result = Some(future.await) })
52-
}
53-
.detach();
54-
loop {
55-
self.runtime.poll_with(Some(Duration::ZERO));
56-
57-
let remaining_tasks = self.runtime.run();
58-
if let Some(result) = result.take() {
59-
break result;
60-
}
61-
62-
let timeout = if remaining_tasks {
63-
Some(Duration::ZERO)
64-
} else {
65-
self.runtime.current_timeout()
66-
};
48+
self.runtime.block_on(future, |timeout| {
6749
let source_id = timeout.map(|timeout| timeout_add_local_once(timeout, || {}));
6850

6951
self.ctx.iteration(true);
@@ -73,9 +55,7 @@ impl Runtime {
7355
source_id.remove();
7456
}
7557
}
76-
77-
self.runtime.clear().ok();
78-
}
58+
})
7959
})
8060
}
8161
}

winio-ui-qt/src/runtime/qt.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, future::Future, os::fd::AsRawFd, time::Duration};
1+
use std::{cell::RefCell, future::Future, os::fd::AsRawFd};
22

33
use cxx::UniquePtr;
44

@@ -57,26 +57,7 @@ impl Runtime {
5757

5858
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
5959
self.enter(|| {
60-
let mut result = None;
61-
unsafe {
62-
self.runtime
63-
.spawn_unchecked(async { result = Some(future.await) })
64-
}
65-
.detach();
66-
loop {
67-
self.runtime.poll_with(Some(Duration::ZERO));
68-
69-
let remaining_tasks = self.runtime.run();
70-
if let Some(result) = result.take() {
71-
break result;
72-
}
73-
74-
let timeout = if remaining_tasks {
75-
Some(Duration::ZERO)
76-
} else {
77-
self.runtime.current_timeout()
78-
};
79-
60+
self.runtime.block_on(future, |timeout| {
8061
if let Some(timeout) = timeout {
8162
self.event_loop
8263
.borrow_mut()
@@ -85,9 +66,7 @@ impl Runtime {
8566
} else {
8667
self.event_loop.borrow_mut().pin_mut().process();
8768
}
88-
89-
self.runtime.clear().ok();
90-
}
69+
})
9170
})
9271
}
9372
}

winio-ui-win32/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repository = { workspace = true }
1111
[target.'cfg(windows)'.dependencies]
1212
winio-primitive = { workspace = true }
1313
winio-handle = { workspace = true }
14+
winio-pollable = { workspace = true }
1415

1516
compio = { workspace = true, features = ["arrayvec"] }
1617
compio-log = { workspace = true }

winio-ui-win32/src/runtime.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{
66
pin::Pin,
77
ptr::{null, null_mut},
88
task::{Context, Poll, Waker},
9-
time::Duration,
109
};
1110

1211
use compio::driver::AsRawFd;
@@ -80,7 +79,7 @@ impl Default for FutureState {
8079
}
8180

8281
pub struct Runtime {
83-
runtime: compio::runtime::Runtime,
82+
runtime: winio_pollable::Runtime,
8483
d2d1: OnceCell<ID2D1Factory>,
8584
registry: RefCell<HashMap<(HWND, u32), Slab<FutureState>>>,
8685
}
@@ -97,7 +96,7 @@ impl Runtime {
9796
init_dark();
9897
}
9998

100-
let runtime = compio::runtime::Runtime::new().unwrap();
99+
let runtime = winio_pollable::Runtime::new().unwrap();
101100

102101
Self {
103102
runtime,
@@ -118,25 +117,7 @@ impl Runtime {
118117

119118
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
120119
self.enter(|| {
121-
let mut result = None;
122-
unsafe {
123-
self.runtime
124-
.spawn_unchecked(async { result = Some(future.await) })
125-
}
126-
.detach();
127-
loop {
128-
self.runtime.poll_with(Some(Duration::ZERO));
129-
130-
let remaining_tasks = self.runtime.run();
131-
if let Some(result) = result.take() {
132-
break result;
133-
}
134-
135-
let timeout = if remaining_tasks {
136-
Some(Duration::ZERO)
137-
} else {
138-
self.runtime.current_timeout()
139-
};
120+
self.runtime.block_on(future, |timeout| {
140121
let timeout = match timeout {
141122
Some(timeout) => timeout.as_millis() as u32,
142123
None => INFINITE,
@@ -171,7 +152,7 @@ impl Runtime {
171152
break;
172153
}
173154
}
174-
}
155+
})
175156
})
176157
}
177158

0 commit comments

Comments
 (0)