1#![cfg_attr(docsrs, feature(doc_cfg))]
12#![cfg_attr(feature = "current_thread_id", feature(current_thread_id))]
13#![allow(unused_features)]
14#![warn(missing_docs)]
15#![deny(rustdoc::broken_intra_doc_links)]
16#![doc(
17 html_logo_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg"
18)]
19#![doc(
20 html_favicon_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg"
21)]
22
23mod affinity;
24mod attacher;
25mod cancel;
26mod future;
27mod waker;
28
29pub mod fd;
30
31#[cfg(feature = "time")]
32pub mod time;
33
34use std::{
35 cell::RefCell,
36 collections::HashSet,
37 fmt::Debug,
38 future::Future,
39 io,
40 ops::Deref,
41 rc::Rc,
42 task::{Context, Poll, Waker},
43 time::Duration,
44};
45
46use compio_buf::{BufResult, IntoInner};
47use compio_driver::{
48 AsRawFd, Cancel, DriverType, Extra, Key, OpCode, Proactor, ProactorBuilder, PushEntry, RawFd,
49 op::Asyncify,
50};
51pub use compio_driver::{BufferPool, ErrorExt};
52use compio_executor::{Executor, ExecutorConfig};
53pub use compio_executor::{JoinHandle, ResumeUnwind};
54use compio_log::{debug, instrument};
55
56use crate::affinity::bind_to_cpu_set;
57#[cfg(feature = "time")]
58use crate::time::{TimerFuture, TimerKey, TimerRuntime};
59pub use crate::{attacher::*, cancel::CancelToken, future::*};
60
61scoped_tls::scoped_thread_local!(static CURRENT_RUNTIME: Runtime);
62
63#[cold]
64fn not_in_compio_runtime() -> ! {
65 panic!("not in a compio runtime")
66}
67
68pub struct RuntimeInner {
70 executor: Executor,
71 driver: RefCell<Proactor>,
72 #[cfg(feature = "time")]
73 timer_runtime: RefCell<TimerRuntime>,
74}
75
76#[derive(Clone)]
80pub struct Runtime(Rc<RuntimeInner>);
81
82impl Debug for Runtime {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 let mut s = f.debug_struct("Runtime");
85 s.field("executor", &self.0.executor)
86 .field("driver", &"...")
87 .field("scheduler", &"...");
88 #[cfg(feature = "time")]
89 s.field("timer_runtime", &"...");
90 s.finish()
91 }
92}
93
94impl Deref for Runtime {
95 type Target = RuntimeInner;
96
97 fn deref(&self) -> &Self::Target {
98 &self.0
99 }
100}
101
102impl Runtime {
103 pub fn new() -> io::Result<Self> {
105 Self::builder().build()
106 }
107
108 pub fn builder() -> RuntimeBuilder {
110 RuntimeBuilder::new()
111 }
112
113 pub fn driver_type(&self) -> DriverType {
115 self.driver.borrow().driver_type()
116 }
117
118 pub fn try_with_current<T, F: FnOnce(&Self) -> T>(f: F) -> Result<T, F> {
121 if CURRENT_RUNTIME.is_set() {
122 Ok(CURRENT_RUNTIME.with(f))
123 } else {
124 Err(f)
125 }
126 }
127
128 pub fn with_current<T, F: FnOnce(&Self) -> T>(f: F) -> T {
134 if CURRENT_RUNTIME.is_set() {
135 CURRENT_RUNTIME.with(f)
136 } else {
137 not_in_compio_runtime()
138 }
139 }
140
141 pub fn try_current() -> Option<Self> {
144 if CURRENT_RUNTIME.is_set() {
145 Some(CURRENT_RUNTIME.with(|r| r.clone()))
146 } else {
147 None
148 }
149 }
150
151 pub fn current() -> Self {
157 if CURRENT_RUNTIME.is_set() {
158 CURRENT_RUNTIME.with(|r| r.clone())
159 } else {
160 not_in_compio_runtime()
161 }
162 }
163
164 pub fn enter<T, F: FnOnce() -> T>(&self, f: F) -> T {
167 CURRENT_RUNTIME.set(self, f)
168 }
169
170 pub fn run(&self) -> bool {
176 self.executor.tick()
177 }
178
179 pub fn waker(&self) -> Waker {
183 self.driver.borrow().waker()
184 }
185
186 pub fn block_on<F: Future>(&self, future: F) -> F::Output {
188 self.enter(|| {
189 let waker = self.waker();
190 let mut context = Context::from_waker(&waker);
191 let mut future = std::pin::pin!(future);
192 loop {
193 if let Poll::Ready(result) = future.as_mut().poll(&mut context) {
194 self.run();
195 return result;
196 }
197 let remaining_tasks = self.run();
198 if remaining_tasks {
199 self.poll_with(Some(Duration::ZERO));
200 } else {
201 self.poll();
202 }
203 }
204 })
205 }
206
207 pub fn spawn<F: Future + 'static>(&self, future: F) -> JoinHandle<F::Output> {
212 self.0.executor.spawn(future)
213 }
214
215 pub fn spawn_blocking<T: Send + 'static>(
219 &self,
220 f: impl (FnOnce() -> T) + Send + 'static,
221 ) -> JoinHandle<T> {
222 let op = Asyncify::new(move || {
223 let res = f();
226 BufResult(Ok(0), res)
227 });
228 let submit = self.submit(op);
229 self.spawn(async move { submit.await.1.into_inner() })
230 }
231
232 pub fn attach(&self, fd: RawFd) -> io::Result<()> {
237 self.driver.borrow_mut().attach(fd)
238 }
239
240 fn submit_raw<T: OpCode + 'static>(
241 &self,
242 op: T,
243 extra: Option<Extra>,
244 ) -> PushEntry<Key<T>, BufResult<usize, T>> {
245 let mut this = self.driver.borrow_mut();
246 match extra {
247 Some(e) => this.push_with_extra(op, e),
248 None => this.push(op),
249 }
250 }
251
252 fn default_extra(&self) -> Extra {
253 self.driver.borrow().default_extra()
254 }
255
256 pub fn submit<T: OpCode + 'static>(&self, op: T) -> Submit<T> {
260 Submit::new(self.clone(), op)
261 }
262
263 pub fn submit_multi<T: OpCode + 'static>(&self, op: T) -> SubmitMulti<T> {
267 SubmitMulti::new(self.clone(), op)
268 }
269
270 pub fn flush(&self) -> bool {
274 self.driver.borrow_mut().flush()
275 }
276
277 pub(crate) fn cancel<T: OpCode>(&self, key: Key<T>) {
278 self.driver.borrow_mut().cancel(key);
279 }
280
281 pub(crate) fn register_cancel<T: OpCode>(&self, key: &Key<T>) -> Cancel {
282 self.driver.borrow_mut().register_cancel(key)
283 }
284
285 pub(crate) fn cancel_token(&self, token: Cancel) -> bool {
286 self.driver.borrow_mut().cancel_token(token)
287 }
288
289 #[cfg(feature = "time")]
290 pub(crate) fn cancel_timer(&self, key: &TimerKey) {
291 self.timer_runtime.borrow_mut().cancel(key);
292 }
293
294 pub(crate) fn poll_task<T: OpCode>(
295 &self,
296 waker: &Waker,
297 key: Key<T>,
298 ) -> PushEntry<Key<T>, BufResult<usize, T>> {
299 instrument!(compio_log::Level::DEBUG, "poll_task", ?key);
300 let mut driver = self.driver.borrow_mut();
301 driver.pop(key).map_pending(|k| {
302 driver.update_waker(&k, waker);
303 k
304 })
305 }
306
307 pub(crate) fn poll_task_with_extra<T: OpCode>(
308 &self,
309 waker: &Waker,
310 key: Key<T>,
311 ) -> PushEntry<Key<T>, (BufResult<usize, T>, Extra)> {
312 instrument!(compio_log::Level::DEBUG, "poll_task_with_extra", ?key);
313 let mut driver = self.driver.borrow_mut();
314 driver.pop_with_extra(key).map_pending(|k| {
315 driver.update_waker(&k, waker);
316 k
317 })
318 }
319
320 pub(crate) fn poll_multishot<T: OpCode>(
321 &self,
322 waker: &Waker,
323 key: &Key<T>,
324 ) -> Option<BufResult<usize, Extra>> {
325 instrument!(compio_log::Level::DEBUG, "poll_multishot", ?key);
326 let mut driver = self.driver.borrow_mut();
327 if let Some(res) = driver.pop_multishot(key) {
328 return Some(res);
329 }
330 driver.update_waker(key, waker);
331 None
332 }
333
334 #[cfg(feature = "time")]
335 pub(crate) fn poll_timer(&self, cx: &mut Context, key: &TimerKey) -> Poll<()> {
336 instrument!(compio_log::Level::DEBUG, "poll_timer", ?cx, ?key);
337 let mut timer_runtime = self.timer_runtime.borrow_mut();
338 if timer_runtime.is_completed(key) {
339 debug!("ready");
340 Poll::Ready(())
341 } else {
342 debug!("pending");
343 timer_runtime.update_waker(key, cx.waker());
344 Poll::Pending
345 }
346 }
347
348 pub fn current_timeout(&self) -> Option<Duration> {
352 #[cfg(not(feature = "time"))]
353 let timeout = None;
354 #[cfg(feature = "time")]
355 let timeout = self.timer_runtime.borrow().min_timeout();
356 timeout
357 }
358
359 pub fn poll(&self) {
364 instrument!(compio_log::Level::DEBUG, "poll");
365 let timeout = self.current_timeout();
366 debug!("timeout: {:?}", timeout);
367 self.poll_with(timeout)
368 }
369
370 pub fn poll_with(&self, timeout: Option<Duration>) {
374 instrument!(compio_log::Level::DEBUG, "poll_with");
375
376 let mut driver = self.driver.borrow_mut();
377 match driver.poll(timeout) {
378 Ok(()) => {}
379 Err(e) => match e.kind() {
380 io::ErrorKind::TimedOut | io::ErrorKind::Interrupted => {
381 debug!("expected error: {e}");
382 }
383 _ => panic!("{e:?}"),
384 },
385 }
386 #[cfg(feature = "time")]
387 self.timer_runtime.borrow_mut().wake();
388 }
389
390 pub fn buffer_pool(&self) -> io::Result<BufferPool> {
395 self.driver.borrow_mut().buffer_pool()
396 }
397
398 pub fn register_files(&self, fds: &[RawFd]) -> io::Result<()> {
405 self.driver.borrow_mut().register_files(fds)
406 }
407
408 pub fn unregister_files(&self) -> io::Result<()> {
415 self.driver.borrow_mut().unregister_files()
416 }
417
418 pub fn register_personality(&self) -> io::Result<u16> {
428 self.driver.borrow_mut().register_personality()
429 }
430
431 pub fn unregister_personality(&self, personality: u16) -> io::Result<()> {
438 self.driver.borrow_mut().unregister_personality(personality)
439 }
440}
441
442impl Drop for Runtime {
443 fn drop(&mut self) {
444 if Rc::strong_count(&self.0) > 1 {
446 return;
447 }
448
449 self.enter(|| {
450 self.executor.clear();
451 })
452 }
453}
454
455impl AsRawFd for Runtime {
456 fn as_raw_fd(&self) -> RawFd {
457 self.driver.borrow().as_raw_fd()
458 }
459}
460
461#[cfg(feature = "criterion")]
462impl criterion::async_executor::AsyncExecutor for Runtime {
463 fn block_on<T>(&self, future: impl Future<Output = T>) -> T {
464 self.block_on(future)
465 }
466}
467
468#[cfg(feature = "criterion")]
469impl criterion::async_executor::AsyncExecutor for &Runtime {
470 fn block_on<T>(&self, future: impl Future<Output = T>) -> T {
471 (**self).block_on(future)
472 }
473}
474
475#[derive(Debug, Clone)]
477pub struct RuntimeBuilder {
478 proactor_builder: ProactorBuilder,
479 thread_affinity: HashSet<usize>,
480 sync_queue_size: usize,
481 local_queue_size: usize,
482 event_interval: u32,
483}
484
485impl Default for RuntimeBuilder {
486 fn default() -> Self {
487 Self::new()
488 }
489}
490
491impl RuntimeBuilder {
492 pub fn new() -> Self {
494 Self {
495 proactor_builder: ProactorBuilder::new(),
496 event_interval: 61,
497 sync_queue_size: 64,
498 local_queue_size: 64,
499 thread_affinity: HashSet::new(),
500 }
501 }
502
503 pub fn with_proactor(&mut self, builder: ProactorBuilder) -> &mut Self {
505 self.proactor_builder = builder;
506 self
507 }
508
509 pub fn thread_affinity(&mut self, cpus: HashSet<usize>) -> &mut Self {
511 self.thread_affinity = cpus;
512 self
513 }
514
515 pub fn event_interval(&mut self, val: usize) -> &mut Self {
520 self.event_interval = val as _;
521 self
522 }
523
524 pub fn sync_queue_size(&mut self, val: usize) -> &mut Self {
530 self.sync_queue_size = val;
531 self
532 }
533
534 pub fn local_queue_size(&mut self, val: usize) -> &mut Self {
539 self.local_queue_size = val;
540 self
541 }
542
543 pub fn build(&self) -> io::Result<Runtime> {
545 let RuntimeBuilder {
546 proactor_builder,
547 thread_affinity,
548 sync_queue_size,
549 local_queue_size,
550 event_interval,
551 } = self;
552
553 if !thread_affinity.is_empty() {
554 bind_to_cpu_set(thread_affinity);
555 }
556 let driver = proactor_builder.build()?;
557 let executor = Executor::with_config(ExecutorConfig {
558 max_interval: *event_interval,
559 sync_queue_size: *sync_queue_size,
560 local_queue_size: *local_queue_size,
561 waker: Some(driver.waker()),
562 });
563 let inner = RuntimeInner {
564 executor,
565 driver: RefCell::new(driver),
566 #[cfg(feature = "time")]
567 timer_runtime: RefCell::new(TimerRuntime::new()),
568 };
569 Ok(Runtime(Rc::new(inner)))
570 }
571}
572
573pub fn spawn<F: Future + 'static>(future: F) -> JoinHandle<F::Output> {
599 Runtime::with_current(|r| r.spawn(future))
600}
601
602pub fn spawn_blocking<T: Send + 'static>(
611 f: impl (FnOnce() -> T) + Send + 'static,
612) -> JoinHandle<T> {
613 Runtime::with_current(|r| r.spawn_blocking(f))
614}
615
616pub fn submit<T: OpCode + 'static>(op: T) -> Submit<T> {
624 Runtime::with_current(|r| r.submit(op))
625}
626
627pub fn submit_multi<T: OpCode + 'static>(op: T) -> SubmitMulti<T> {
636 Runtime::with_current(|r| r.submit_multi(op))
637}
638
639pub fn register_files(fds: &[RawFd]) -> io::Result<()> {
652 Runtime::with_current(|r| r.register_files(fds))
653}
654
655pub fn unregister_files() -> io::Result<()> {
668 Runtime::with_current(|r| r.unregister_files())
669}
670
671#[cfg(feature = "time")]
672pub(crate) async fn create_timer(instant: std::time::Instant) {
673 let key = Runtime::with_current(|r| r.timer_runtime.borrow_mut().insert(instant));
674 if let Some(key) = key {
675 TimerFuture::new(key).await
676 }
677}