Skip to main content

pi_async_rt/rt/
single_thread.rs

1//! # 单线程运行时
2//!
3//! - [SingleTaskPool]\: 单线程任务池
4//! - [SingleTaskRunner]\: 单线程异步任务执行器
5//! - [SingleTaskRuntime]\: 异步单线程任务运行时
6//!
7//! [SingleTaskPool]: struct.SingleTaskPool.html
8//! [SingleTaskRunner]: struct.SingleTaskRunner.html
9//! [SingleTaskRuntime]: struct.SingleTaskRuntime.html
10//!
11//! # Examples
12//!
13//! ```
14//! use pi_async_rt::prelude::{AsyncRuntimeExt, SingleTaskPool, SingleTaskRunner};
15//! let pool = SingleTaskPool::default();
16//! let rt = SingleTaskRunner::<(), SingleTaskPool<()>>::new(pool).into_local();
17//! let _ = rt.block_on(async move {});
18//! ```
19
20use std::thread;
21use std::sync::Arc;
22use std::vec::IntoIter;
23use std::future::Future;
24use std::cell::UnsafeCell;
25use std::task::{Context, Poll, Waker};
26use std::io::{Error, ErrorKind, Result};
27use std::collections::vec_deque::VecDeque;
28use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
29
30use async_stream::stream;
31use crossbeam_channel::Sender;
32use crossbeam_queue::SegQueue;
33use flume::bounded as async_bounded;
34use futures::{
35    future::{BoxFuture, FutureExt},
36    stream::{BoxStream, Stream, StreamExt},
37    task::waker_ref,
38};
39use parking_lot::{Condvar, Mutex};
40use quanta::Clock;
41
42use wrr::IWRRSelector;
43
44use super::{
45    PI_ASYNC_THREAD_LOCAL_ID, DEFAULT_MAX_HIGH_PRIORITY_BOUNDED, DEFAULT_HIGH_PRIORITY_BOUNDED, DEFAULT_MAX_LOW_PRIORITY_BOUNDED, alloc_rt_uid, AsyncMapReduce, AsyncPipelineResult, AsyncRuntime,
46    AsyncRuntimeExt, AsyncTask, AsyncTaskPollClaim, AsyncTaskPollGuard, AsyncTaskPool, AsyncTaskPoolExt, AsyncTaskTimer, AsyncWait,
47    AsyncWaitAny, AsyncWaitAnyCallback, AsyncWaitTimeout, LocalAsyncRuntime, TaskId, YieldNow,
48    requeue_runtime_task
49};
50use crate::rt::{TaskHandle, AsyncTimingTask};
51
52///
53/// 单线程任务池
54///
55pub struct SingleTaskPool<O: Default + 'static> {
56    id:             usize,                                                      //绑定的运行时唯一id
57    public:         SegQueue<Arc<AsyncTask<SingleTaskPool<O>, O>>>,             //外部任务队列
58    internal:       UnsafeCell<VecDeque<Arc<AsyncTask<SingleTaskPool<O>, O>>>>, //内部任务队列
59    stack:          UnsafeCell<Vec<Arc<AsyncTask<SingleTaskPool<O>, O>>>>,      //本地任务栈
60    selector:       UnsafeCell<IWRRSelector<2>>,                                //任务池选择器
61    consume_count:  AtomicUsize,                                                //任务消费计数
62    produce_count:  AtomicUsize,                                                //任务生产计数
63    thread_waker:   Option<Arc<(AtomicBool, Mutex<()>, Condvar)>>,              //绑定线程的唤醒器
64}
65
66unsafe impl<O: Default + 'static> Send for SingleTaskPool<O> {}
67unsafe impl<O: Default + 'static> Sync for SingleTaskPool<O> {}
68
69impl<O: Default + 'static> Default for SingleTaskPool<O> {
70    fn default() -> Self {
71        SingleTaskPool::new([1, 1])
72    }
73}
74
75impl<O: Default + 'static> AsyncTaskPool<O> for SingleTaskPool<O> {
76    type Pool = SingleTaskPool<O>;
77
78    #[inline]
79    fn get_thread_id(&self) -> usize {
80        let rt_uid = self.id;
81        match PI_ASYNC_THREAD_LOCAL_ID.try_with(move |thread_id| {
82            let current = unsafe { *thread_id.get() };
83            if current == usize::MAX {
84                //当前线程还未初始化运行时的线程id,则初始化
85                unsafe {
86                    *thread_id.get() = rt_uid << 32;
87                    *thread_id.get()
88                }
89            } else {
90                current
91            }
92        }) {
93            Err(e) => {
94                //不应该执行到这个分支
95                panic!(
96                    "Get thread id failed, thread: {:?}, reason: {:?}",
97                    thread::current(),
98                    e
99                );
100            }
101            Ok(id) => id,
102        }
103    }
104
105    #[inline]
106    fn len(&self) -> usize {
107        if let Some(len) = self
108            .produce_count
109            .load(Ordering::Relaxed)
110            .checked_sub(self.consume_count.load(Ordering::Relaxed))
111        {
112            len
113        } else {
114            0
115        }
116    }
117
118    #[inline]
119    fn push(&self, task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()> {
120        self.public.push(task);
121        self.produce_count.fetch_add(1, Ordering::Relaxed);
122        Ok(())
123    }
124
125    #[inline]
126    fn push_local(&self, task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()> {
127        let id = self.get_thread_id();
128        let rt_uid = task.owner();
129        if (id >> 32) == rt_uid {
130            //当前是运行时所在线程
131            unsafe {{
132                (&mut *self.internal.get()).push_back(task);
133            }}
134            self.produce_count.fetch_add(1, Ordering::Relaxed);
135            Ok(())
136        } else {
137            //当前不是运行时所在线程
138            self.push(task)
139        }
140    }
141
142    #[inline]
143    fn push_priority(&self,
144                     priority: usize,
145                     task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()> {
146        if priority >= DEFAULT_MAX_HIGH_PRIORITY_BOUNDED {
147            //最高优先级
148            let id = self.get_thread_id();
149            let rt_uid = task.owner();
150            if (id >> 32) == rt_uid {
151                //当前是运行时所在线程
152                unsafe {
153                    let stack = (&mut *self.stack.get());
154                    if stack
155                        .capacity()
156                        .checked_sub(stack.len())
157                        .unwrap_or(0) >= 0 {
158                        //本地任务栈有空闲容量,则立即将任务加入本地任务栈
159                        (&mut *self.stack.get()).push(task);
160                    } else {
161                        //本地内部任务队列有空闲容量,则立即将任务加入本地内部任务队列
162                        (&mut *self.internal.get()).push_back(task);
163                    }
164                }
165
166                self.produce_count.fetch_add(1, Ordering::Relaxed);
167                Ok(())
168            } else {
169                //当前不是运行时所在线程
170                self.push(task)
171            }
172        } else if priority >= DEFAULT_HIGH_PRIORITY_BOUNDED {
173            //高优先级
174            self.push_local(task)
175        } else {
176            //低优先级
177            self.push(task)
178        }
179    }
180
181    #[inline]
182    fn push_keep(&self, task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()> {
183        self.push_priority(DEFAULT_HIGH_PRIORITY_BOUNDED, task)
184    }
185
186    #[inline]
187    fn try_pop(&self) -> Option<Arc<AsyncTask<Self::Pool, O>>> {
188        let task = unsafe { (&mut *self
189            .stack
190            .get())
191            .pop()
192        };
193        if task.is_some() {
194            //指定工作者的任务栈有任务,则立即返回任务
195            self.consume_count.fetch_add(1, Ordering::Relaxed);
196            return task;
197        }
198
199        //从指定工作者的任务队列中弹出任务
200        let task = try_pop_by_weight(self);
201        if task.is_some() {
202            self
203                .consume_count
204                .fetch_add(1, Ordering::Relaxed);
205        }
206        task
207    }
208
209    #[inline]
210    fn try_pop_all(&self) -> IntoIter<Arc<AsyncTask<Self::Pool, O>>> {
211        let mut all = Vec::with_capacity(self.len());
212
213        let internal = unsafe { (&mut *self.internal.get()) };
214        for _ in 0..internal.len() {
215            if let Some(task) = internal.pop_front() {
216                all.push(task);
217            }
218        }
219
220        let public_len = self.public.len();
221        for _ in 0..public_len {
222            if let Some(task) = self.public.pop() {
223                all.push(task);
224            }
225        }
226
227        all.into_iter()
228    }
229
230    #[inline]
231    fn get_thread_waker(&self) -> Option<&Arc<(AtomicBool, Mutex<()>, Condvar)>> {
232        self.thread_waker.as_ref()
233    }
234}
235
236// 尝试通过统计信息更新权重,根据权重选择从本地外部任务队列或本地内部任务队列中弹出任务
237fn try_pop_by_weight<O: Default + 'static>(pool: &SingleTaskPool<O>)
238    -> Option<Arc<AsyncTask<SingleTaskPool<O>, O>>> {
239    unsafe {
240        //根据权重选择从指定的任务队列弹出任务
241        match (&mut *pool.selector.get()).select() {
242            0 => {
243                //弹出外部任务
244                let task = try_pop_external(pool);
245                if task.is_some() {
246                    task
247                } else {
248                    //当前没有外部任务,则尝试弹出内部任务
249                    try_pop_internal(pool)
250                }
251            },
252            _ => {
253                //弹出内部任务
254                let task = try_pop_internal(pool);
255                if task.is_some() {
256                    task
257                } else {
258                    //当前没有内部任务,则尝试弹出外部任务
259                    try_pop_external(pool)
260                }
261            },
262        }
263    }
264}
265
266// 尝试弹出内部任务队列的任务
267#[inline]
268fn try_pop_internal<O: Default + 'static>(pool: &SingleTaskPool<O>)
269    -> Option<Arc<AsyncTask<SingleTaskPool<O>, O>>> {
270    unsafe { (&mut *pool.internal.get()).pop_front() }
271}
272
273// 尝试弹出外部任务队列的任务
274#[inline]
275fn try_pop_external<O: Default + 'static>(pool: &SingleTaskPool<O>)
276                                          -> Option<Arc<AsyncTask<SingleTaskPool<O>, O>>> {
277    pool.public.pop()
278}
279
280impl<O: Default + 'static> AsyncTaskPoolExt<O> for SingleTaskPool<O> {
281    fn set_thread_waker(&mut self, thread_waker: Arc<(AtomicBool, Mutex<()>, Condvar)>) {
282        self.thread_waker = Some(thread_waker);
283    }
284}
285
286impl<O: Default + 'static> SingleTaskPool<O> {
287    /// 构建指定权重的单线程任务池
288    pub fn new(weights: [u8; 2]) -> Self {
289        let id = alloc_rt_uid();
290        let public = SegQueue::new();
291        let internal = UnsafeCell::new(VecDeque::new());
292        let stack = UnsafeCell::new(Vec::with_capacity(1));
293        let selector = UnsafeCell::new(IWRRSelector::new(weights));
294        let consume_count = AtomicUsize::new(0);
295        let produce_count = AtomicUsize::new(0);
296
297        SingleTaskPool {
298            id,
299            public,
300            internal,
301            stack,
302            selector,
303            consume_count,
304            produce_count,
305            thread_waker: Some(Arc::new((
306                AtomicBool::new(false),
307                Mutex::new(()),
308                Condvar::new(),
309            ))),
310        }
311    }
312}
313
314///
315/// 异步单线程任务运行时
316///
317pub struct SingleTaskRuntime<
318    O: Default + 'static = (),
319    P: AsyncTaskPoolExt<O> + AsyncTaskPool<O> = SingleTaskPool<O>,
320>(
321    Arc<(
322        usize,                                  //运行时唯一id
323        Arc<P>,                                 //异步任务池
324        Sender<(usize, AsyncTimingTask<P, O>)>, //休眠的异步任务生产者
325        AsyncTaskTimer<P, O>,                   //本地定时器
326        AtomicUsize,                            //定时器任务生产计数
327        AtomicUsize,                            //定时器任务消费计数
328    )>,
329);
330
331unsafe impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>> Send
332    for SingleTaskRuntime<O, P>
333{
334}
335unsafe impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>> Sync
336    for SingleTaskRuntime<O, P>
337{
338}
339
340impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>> Clone
341    for SingleTaskRuntime<O, P>
342{
343    fn clone(&self) -> Self {
344        SingleTaskRuntime(self.0.clone())
345    }
346}
347
348impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>> AsyncRuntime<O>
349    for SingleTaskRuntime<O, P>
350{
351    type Pool = P;
352
353    /// 共享运行时内部任务池
354    fn shared_pool(&self) -> Arc<Self::Pool> {
355        (self.0).1.clone()
356    }
357
358    /// 获取当前异步运行时的唯一id
359    fn get_id(&self) -> usize {
360        (self.0).0
361    }
362
363    /// 获取当前异步运行时待处理任务数量
364    fn wait_len(&self) -> usize {
365        (self.0)
366            .4
367            .load(Ordering::Relaxed)
368            .checked_sub((self.0).5.load(Ordering::Relaxed))
369            .unwrap_or(0)
370    }
371
372    /// 获取当前异步运行时任务数量
373    fn len(&self) -> usize {
374        (self.0).1.len()
375    }
376
377    /// 分配异步任务的唯一id
378    fn alloc<R: 'static>(&self) -> TaskId {
379        TaskId(UnsafeCell::new((TaskHandle::<R>::default().into_raw() as u128) << 64 | self.get_id() as u128 & 0xffffffffffffffff))
380    }
381
382    /// 派发一个指定的异步任务到异步运行时
383    fn spawn<F>(&self, future: F) -> Result<TaskId>
384    where
385        F: Future<Output = O> + Send + 'static,
386    {
387        let task_id = self.alloc::<F::Output>();
388        if let Err(e) = self.spawn_by_id(task_id.clone(), future) {
389            return Err(e);
390        }
391
392        Ok(task_id)
393    }
394
395    /// 派发一个异步任务到本地异步运行时,如果本地没有本异步运行时,则会派发到当前运行时中
396    fn spawn_local<F>(&self, future: F) -> Result<TaskId>
397        where
398            F: Future<Output = O> + Send + 'static {
399        let task_id = self.alloc::<F::Output>();
400        if let Err(e) = self.spawn_local_by_id(task_id.clone(), future) {
401            return Err(e);
402        }
403
404        Ok(task_id)
405    }
406
407    /// 派发一个指定优先级的异步任务到异步运行时
408    fn spawn_priority<F>(&self, priority: usize, future: F) -> Result<TaskId>
409        where
410            F: Future<Output = O> + Send + 'static {
411        let task_id = self.alloc::<F::Output>();
412        if let Err(e) = self.spawn_priority_by_id(task_id.clone(), priority, future) {
413            return Err(e);
414        }
415
416        Ok(task_id)
417    }
418
419    /// 派发一个异步任务到异步运行时,并立即让出任务的当前运行
420    fn spawn_yield<F>(&self, future: F) -> Result<TaskId>
421        where
422            F: Future<Output = O> + Send + 'static {
423        let task_id = self.alloc::<F::Output>();
424        if let Err(e) = self.spawn_yield_by_id(task_id.clone(), future) {
425            return Err(e);
426        }
427
428        Ok(task_id)
429    }
430
431    /// 派发一个在指定时间后执行的异步任务到异步运行时,时间单位ms
432    fn spawn_timing<F>(&self, future: F, time: usize) -> Result<TaskId>
433    where
434        F: Future<Output = O> + Send + 'static,
435    {
436        let task_id = self.alloc::<F::Output>();
437        if let Err(e) = self.spawn_timing_by_id(task_id.clone(), future, time) {
438            return Err(e);
439        }
440
441        Ok(task_id)
442    }
443
444    /// 派发一个指定任务唯一id的异步任务到异步运行时
445    fn spawn_by_id<F>(&self, task_id: TaskId, future: F) -> Result<()>
446        where
447            F: Future<Output = O> + Send + 'static {
448        if let Err(e) = (self.0).1.push(Arc::new(AsyncTask::new(
449            task_id,
450            (self.0).1.clone(),
451            DEFAULT_MAX_LOW_PRIORITY_BOUNDED,
452            Some(future.boxed()),
453        ))) {
454            return Err(Error::new(ErrorKind::Other, e));
455        }
456
457        Ok(())
458    }
459
460    /// 派发一个指定任务唯一id的异步任务到本地异步运行时,如果本地没有本异步运行时,则会派发到当前运行时中
461    fn spawn_local_by_id<F>(&self, task_id: TaskId, future: F) -> Result<()>
462        where
463            F: Future<Output = O> + Send + 'static {
464        (self.0).1.push_local(Arc::new(AsyncTask::new(
465            task_id,
466            (self.0).1.clone(),
467            DEFAULT_HIGH_PRIORITY_BOUNDED,
468            Some(future.boxed()))))
469    }
470
471    /// 派发一个指定任务唯一id和任务优先级的异步任务到异步运行时
472    fn spawn_priority_by_id<F>(&self,
473                               task_id: TaskId,
474                               priority: usize,
475                               future: F) -> Result<()>
476        where
477            F: Future<Output = O> + Send + 'static {
478        (self.0).1.push_priority(priority, Arc::new(AsyncTask::new(
479            task_id,
480            (self.0).1.clone(),
481            priority,
482            Some(future.boxed()))))
483    }
484
485    /// 派发一个指定任务唯一id的异步任务到异步运行时,并立即让出任务的当前运行
486    #[inline]
487    fn spawn_yield_by_id<F>(&self, task_id: TaskId, future: F) -> Result<()>
488        where
489            F: Future<Output = O> + Send + 'static {
490        self.spawn_priority_by_id(task_id,
491                                  DEFAULT_HIGH_PRIORITY_BOUNDED,
492                                  future)
493    }
494
495    /// 派发一个指定任务唯一id和在指定时间后执行的异步任务到异步运行时,时间单位ms
496    fn spawn_timing_by_id<F>(&self,
497                             task_id: TaskId,
498                             future: F,
499                             time: usize) -> Result<()>
500        where
501            F: Future<Output = O> + Send + 'static {
502        let rt = self.clone();
503        self.spawn_by_id(task_id, async move {
504            (rt.0).3.set_timer(
505                AsyncTimingTask::WaitRun(Arc::new(AsyncTask::new(
506                    rt.alloc::<F::Output>(),
507                    (rt.0).1.clone(),
508                    DEFAULT_HIGH_PRIORITY_BOUNDED,
509                    Some(future.boxed()),
510                ))),
511                time,
512            );
513
514            (rt.0).4.fetch_add(1, Ordering::Relaxed);
515            Default::default()
516        })
517    }
518
519    /// 挂起指定唯一id的异步任务
520    fn pending<Output: 'static>(&self, task_id: &TaskId, waker: Waker) -> Poll<Output> {
521        task_id.set_waker::<Output>(waker);
522        Poll::Pending
523    }
524
525    /// 唤醒指定唯一id的异步任务
526    fn wakeup<Output: 'static>(&self, task_id: &TaskId) {
527        task_id.wakeup::<Output>();
528    }
529
530    /// 挂起当前异步运行时的当前任务,并在指定的其它运行时上派发一个指定的异步任务,等待其它运行时上的异步任务完成后,唤醒当前运行时的当前任务,并返回其它运行时上的异步任务的值
531    fn wait<V: Send + 'static>(&self) -> AsyncWait<V> {
532        AsyncWait(self.wait_any(2))
533    }
534
535    /// 挂起当前异步运行时的当前任务,并在多个其它运行时上执行多个其它任务,其中任意一个任务完成,则唤醒当前运行时的当前任务,并返回这个已完成任务的值,而其它未完成的任务的值将被忽略
536    fn wait_any<V: Send + 'static>(&self, capacity: usize) -> AsyncWaitAny<V> {
537        let (producor, consumer) = async_bounded(capacity);
538
539        AsyncWaitAny {
540            capacity,
541            producor,
542            consumer,
543        }
544    }
545
546    /// 挂起当前异步运行时的当前任务,并在多个其它运行时上执行多个其它任务,任务返回后需要通过用户指定的检查回调进行检查,其中任意一个任务检查通过,则唤醒当前运行时的当前任务,并返回这个已完成任务的值,而其它未完成或未检查通过的任务的值将被忽略,如果所有任务都未检查通过,则强制唤醒当前运行时的当前任务
547    fn wait_any_callback<V: Send + 'static>(&self, capacity: usize) -> AsyncWaitAnyCallback<V> {
548        let (producor, consumer) = async_bounded(capacity);
549
550        AsyncWaitAnyCallback {
551            capacity,
552            producor,
553            consumer,
554        }
555    }
556
557    /// 构建用于派发多个异步任务到指定运行时的映射归并,需要指定映射归并的容量
558    fn map_reduce<V: Send + 'static>(&self, capacity: usize) -> AsyncMapReduce<V> {
559        let (producor, consumer) = async_bounded(capacity);
560
561        AsyncMapReduce {
562            count: 0,
563            capacity,
564            producor,
565            consumer,
566        }
567    }
568
569    /// 挂起当前异步运行时的当前任务,等待指定的时间后唤醒当前任务
570    fn timeout(&self, timeout: usize) -> BoxFuture<'static, ()> {
571        let rt = self.clone();
572        let producor = (self.0).2.clone();
573
574        AsyncWaitTimeout::new(rt, producor, timeout).boxed()
575    }
576
577    /// 立即让出当前任务的执行
578    fn yield_now(&self) -> BoxFuture<'static, ()> {
579        async move {
580            YieldNow(false).await;
581        }.boxed()
582    }
583
584    /// 生成一个异步管道,输入指定流,输入流的每个值通过过滤器生成输出流的值
585    fn pipeline<S, SO, F, FO>(&self, input: S, mut filter: F) -> BoxStream<'static, FO>
586    where
587        S: Stream<Item = SO> + Send + 'static,
588        SO: Send + 'static,
589        F: FnMut(SO) -> AsyncPipelineResult<FO> + Send + 'static,
590        FO: Send + 'static,
591    {
592        let output = stream! {
593            for await value in input {
594                match filter(value) {
595                    AsyncPipelineResult::Disconnect => {
596                        //立即中止管道
597                        break;
598                    },
599                    AsyncPipelineResult::Filtered(result) => {
600                        yield result;
601                    },
602                }
603            }
604        };
605
606        output.boxed()
607    }
608
609    /// 关闭异步运行时,返回请求关闭是否成功
610    fn close(&self) -> bool {
611        false
612    }
613}
614
615impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>> AsyncRuntimeExt<O>
616    for SingleTaskRuntime<O, P>
617{
618    fn spawn_with_context<F, C>(&self, task_id: TaskId, future: F, context: C) -> Result<()>
619    where
620        F: Future<Output = O> + Send + 'static,
621        C: 'static,
622    {
623        if let Err(e) = (self.0).1.push(Arc::new(AsyncTask::with_context(
624            task_id,
625            (self.0).1.clone(),
626            DEFAULT_MAX_LOW_PRIORITY_BOUNDED,
627            Some(future.boxed()),
628            context,
629        ))) {
630            return Err(Error::new(ErrorKind::Other, e));
631        }
632
633        Ok(())
634    }
635
636    fn spawn_timing_with_context<F, C>(
637        &self,
638        task_id: TaskId,
639        future: F,
640        context: C,
641        time: usize,
642    ) -> Result<()>
643    where
644        F: Future<Output = O> + Send + 'static,
645        C: Send + 'static,
646    {
647        let rt = self.clone();
648        self.spawn_by_id(task_id, async move {
649            (rt.0).3.set_timer(
650                AsyncTimingTask::WaitRun(Arc::new(AsyncTask::with_context(
651                    rt.alloc::<F::Output>(),
652                    (rt.0).1.clone(),
653                    DEFAULT_MAX_HIGH_PRIORITY_BOUNDED,
654                    Some(future.boxed()),
655                    context,
656                ))),
657                time,
658            );
659
660            (rt.0).4.fetch_add(1, Ordering::Relaxed);
661            Default::default()
662        })
663    }
664
665    fn block_on<F>(&self, future: F) -> Result<F::Output>
666    where
667        F: Future + Send + 'static,
668        <F as Future>::Output: Default + Send + 'static,
669    {
670        let runner = SingleTaskRunner {
671            is_running: AtomicBool::new(true),
672            runtime: self.clone(),
673            clock: Clock::new(),
674        };
675        let mut result: Option<<F as Future>::Output> = None;
676        let result_raw = (&mut result) as *mut Option<<F as Future>::Output> as usize;
677
678        self.spawn(async move {
679            //在指定运行时中执行,并返回结果
680            let r = future.await;
681            unsafe {
682                *(result_raw as *mut Option<<F as Future>::Output>) = Some(r);
683            }
684
685            Default::default()
686        });
687
688        loop {
689            //执行异步任务
690            while runner.run()? > 0 {}
691
692            //尝试获取异步任务的执行结果
693            if let Some(result) = result.take() {
694                //异步任务已完成,则立即返回执行结果
695                return Ok(result);
696            }
697        }
698    }
699}
700
701impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>>
702    SingleTaskRuntime<O, P>
703{
704    /// 获取当前单线程异步运行时的本地异步运行时
705    pub fn to_local_runtime(&self) -> LocalAsyncRuntime<O> {
706        LocalAsyncRuntime {
707            inner: self.as_raw(),
708            get_id_func: SingleTaskRuntime::<O, P>::get_id_raw,
709            spawn_func: SingleTaskRuntime::<O, P>::spawn_raw,
710            spawn_local_func: SingleTaskRuntime::<O, P>::spawn_local_raw,
711            spawn_timing_func: SingleTaskRuntime::<O, P>::spawn_timing_raw,
712            timeout_func: SingleTaskRuntime::<O, P>::timeout_raw,
713        }
714    }
715
716    // 获取当前单线程异步运行时的指针
717    #[inline]
718    pub(crate) fn as_raw(&self) -> *const () {
719        Arc::into_raw(self.0.clone()) as *const ()
720    }
721
722    // 获取指定指针的单线程异步运行时
723    #[inline]
724    pub(crate) fn from_raw(raw: *const ()) -> Self {
725        let inner = unsafe {
726            Arc::from_raw(
727                raw as *const (
728                    usize,
729                    Arc<P>,
730                    Sender<(usize, AsyncTimingTask<P, O>)>,
731                    AsyncTaskTimer<P, O>,
732                    AtomicUsize,
733                    AtomicUsize,
734                ),
735            )
736        };
737        SingleTaskRuntime(inner)
738    }
739
740    // 获取当前异步运行时的唯一id
741    pub(crate) fn get_id_raw(raw: *const ()) -> usize {
742        let rt = SingleTaskRuntime::<O, P>::from_raw(raw);
743        let id = rt.get_id();
744        Arc::into_raw(rt.0); //避免提前释放
745        id
746    }
747
748    // 派发一个指定的异步任务到异步运行时
749    pub(crate) fn spawn_raw(raw: *const (), future: BoxFuture<'static, O>) -> Result<()> {
750        let rt = SingleTaskRuntime::<O, P>::from_raw(raw);
751        let result = rt.spawn_by_id(rt.alloc::<O>(), future);
752        Arc::into_raw(rt.0); //避免提前释放
753        result
754    }
755
756    // 派发一个指定的异步任务到本地异步运行时
757    pub(crate) fn spawn_local_raw(raw: *const (), future: BoxFuture<'static, O>) -> Result<()> {
758        let rt = SingleTaskRuntime::<O, P>::from_raw(raw);
759        let result = rt.spawn_local_by_id(rt.alloc::<O>(), future);
760        Arc::into_raw(rt.0); //避免提前释放
761        result
762    }
763
764    // 定时派发一个指定的异步任务到异步运行时
765    pub(crate) fn spawn_timing_raw(
766        raw: *const (),
767        future: BoxFuture<'static, O>,
768        timeout: usize,
769    ) -> Result<()> {
770        let rt = SingleTaskRuntime::<O, P>::from_raw(raw);
771        let result = rt.spawn_timing_by_id(rt.alloc::<O>(), future, timeout);
772        Arc::into_raw(rt.0); //避免提前释放
773        result
774    }
775
776    // 挂起当前异步运行时的当前任务,等待指定的时间后唤醒当前任务
777    pub(crate) fn timeout_raw(raw: *const (), timeout: usize) -> BoxFuture<'static, ()> {
778        let rt = SingleTaskRuntime::<O, P>::from_raw(raw);
779        let boxed = rt.timeout(timeout);
780        Arc::into_raw(rt.0); //避免提前释放
781        boxed
782    }
783}
784
785///
786/// 单线程异步任务执行器
787///
788pub struct SingleTaskRunner<
789    O: Default + 'static,
790    P: AsyncTaskPoolExt<O> + AsyncTaskPool<O> = SingleTaskPool<O>,
791> {
792    is_running: AtomicBool,                 //是否开始运行
793    runtime:    SingleTaskRuntime<O, P>,    //异步单线程任务运行时
794    clock:      Clock,                      //执行器的时钟
795}
796
797unsafe impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>> Send
798    for SingleTaskRunner<O, P>
799{
800}
801unsafe impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>> Sync
802    for SingleTaskRunner<O, P>
803{
804}
805
806impl<O: Default + 'static> Default for SingleTaskRunner<O> {
807    fn default() -> Self {
808        SingleTaskRunner::new(SingleTaskPool::default())
809    }
810}
811
812impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>>
813    SingleTaskRunner<O, P>
814{
815    /// 用指定的任务池构建单线程异步运行时
816    pub fn new(pool: P) -> Self {
817        let rt_uid = pool.get_thread_id() >> 32;
818        let pool = Arc::new(pool);
819
820        //构建本地定时器和定时异步任务生产者
821        let timer = AsyncTaskTimer::new();
822        let producor = timer.producor.clone();
823        let timer_producor_count = AtomicUsize::new(0);
824        let timer_consume_count = AtomicUsize::new(0);
825
826        //构建单线程任务运行时
827        let runtime = SingleTaskRuntime(Arc::new((rt_uid,
828                                                  pool,
829                                                  producor,
830                                                  timer,
831                                                  timer_producor_count,
832                                                  timer_consume_count)));
833
834        SingleTaskRunner {
835            is_running: AtomicBool::new(false),
836            runtime,
837            clock: Clock::new(),
838        }
839    }
840
841    /// 获取单线程异步任务执行器的线程唤醒器
842    pub fn get_thread_waker(&self) -> Option<Arc<(AtomicBool, Mutex<()>, Condvar)>> {
843        (self.runtime.0).1.get_thread_waker().cloned()
844    }
845
846    /// 启动单线程异步任务执行器
847    pub fn startup(&self) -> Option<SingleTaskRuntime<O, P>> {
848        if cfg!(target_arch = "aarch64") {
849            match self
850                .is_running
851                .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
852            {
853                Ok(false) => {
854                    //未启动,则启动,并返回单线程异步运行时
855                    Some(self.runtime.clone())
856                }
857                _ => {
858                    //已启动,则忽略
859                    None
860                }
861            }
862        } else {
863            match self.is_running.compare_exchange(
864                false,
865                true,
866                Ordering::SeqCst,
867                Ordering::SeqCst,
868            ) {
869                Ok(false) => {
870                    //未启动,则启动,并返回单线程异步运行时
871                    Some(self.runtime.clone())
872                }
873                _ => {
874                    //已启动,则忽略
875                    None
876                }
877            }
878        }
879    }
880
881    /// 运行一次单线程异步任务执行器,返回当前任务池中任务的数量
882    pub fn run_once(&self) -> Result<usize> {
883        if !self.is_running.load(Ordering::Relaxed) {
884            //未启动,则返回错误原因
885            return Err(Error::new(
886                ErrorKind::Other,
887                "Single thread runtime not running",
888            ));
889        }
890
891        //设置新的定时任务,并唤醒已过期的定时任务
892        let mut pop_len = 0;
893        (self.runtime.0)
894            .4
895            .fetch_add((self.runtime.0).3.consume(),
896                       Ordering::Relaxed);
897        loop {
898            let current_time = (self.runtime.0).3.is_require_pop();
899            if let Some(current_time) = current_time {
900                //当前有到期的定时异步任务,则只处理到期的一个定时异步任务
901                let timed_out = (self.runtime.0).3.pop(current_time);
902                if let Some((handle, timing_task)) = timed_out {
903                    match timing_task {
904                        AsyncTimingTask::Pended(expired) => {
905                            //唤醒休眠的异步任务,并立即执行
906                            self.runtime.wakeup::<O>(&expired);
907                            if let Some(task) = (self.runtime.0).1.try_pop() {
908                                run_task(task);
909                            }
910                        }
911                        AsyncTimingTask::WaitRun(expired) => {
912                            //立即执行到期的定时异步任务,并立即执行
913                            (self.runtime.0).1.push_priority(handle, expired);
914                            if let Some(task) = (self.runtime.0).1.try_pop() {
915                                run_task(task);
916                            }
917                        }
918                        AsyncTimingTask::TimeoutWake(waiter) => {
919                            //唤醒等待timeout到期的任务
920                            waiter.fire();
921                            if let Some(task) = (self.runtime.0).1.try_pop() {
922                                run_task(task);
923                            }
924                        }
925                    }
926                    pop_len += 1;
927                }
928            } else {
929                //当前没有到期的定时异步任务,则退出本次定时异步任务处理
930                break;
931            }
932        }
933        (self.runtime.0)
934            .5
935            .fetch_add(pop_len,
936                       Ordering::Relaxed);
937
938        //继续执行当前任务池中的一个异步任务
939        match (self.runtime.0).1.try_pop() {
940            None => {
941                //当前没有异步任务,则立即返回
942                return Ok(0);
943            }
944            Some(task) => {
945                run_task(task);
946            }
947        }
948
949        Ok((self.runtime.0).1.len())
950    }
951
952    /// 运行单线程异步任务执行器,并执行任务池中的所有任务
953    pub fn run(&self) -> Result<usize> {
954        if !self.is_running.load(Ordering::Relaxed) {
955            //未启动,则返回错误原因
956            return Err(Error::new(
957                ErrorKind::Other,
958                "Single thread runtime not running",
959            ));
960        }
961
962        loop {
963            //设置新的定时任务,并唤醒已过期的定时任务
964            let mut pop_len = 0;
965            let mut start_run_millis = self.clock.recent(); //重置开运行时长
966            (self.runtime.0)
967                .4
968                .fetch_add((self.runtime.0).3.consume(),
969                           Ordering::Relaxed);
970            loop {
971                let current_time = (self.runtime.0).3.is_require_pop();
972                if let Some(current_time) = current_time {
973                    //当前有到期的定时异步任务,则只处理到期的一个定时异步任务
974                    let timed_out = (self.runtime.0).3.pop(current_time);
975                    if let Some((handle, timing_task)) = timed_out {
976                        match timing_task {
977                            AsyncTimingTask::Pended(expired) => {
978                                //唤醒休眠的异步任务,并立即执行
979                                self.runtime.wakeup::<O>(&expired);
980                                if let Some(task) = (self.runtime.0).1.try_pop() {
981                                    run_task(task);
982                                }
983                            }
984                            AsyncTimingTask::WaitRun(expired) => {
985                                //立即执行到期的定时异步任务,并立即执行
986                                (self.runtime.0).1.push_priority(handle, expired);
987                                if let Some(task) = (self.runtime.0).1.try_pop() {
988                                    run_task(task);
989                                }
990                            }
991                            AsyncTimingTask::TimeoutWake(waiter) => {
992                                //唤醒等待timeout到期的任务
993                                waiter.fire();
994                                if let Some(task) = (self.runtime.0).1.try_pop() {
995                                    run_task(task);
996                                }
997                            }
998                        }
999                        pop_len += 1;
1000                    }
1001                } else {
1002                    //当前没有到期的定时异步任务,则退出本次定时异步任务处理
1003                    break;
1004                }
1005            }
1006            (self.runtime.0)
1007                .5
1008                .fetch_add(pop_len,
1009                           Ordering::Relaxed);
1010
1011            //继续执行当前任务池中的一个异步任务
1012            while self
1013                .clock
1014                .recent()
1015                .duration_since(start_run_millis)
1016                .as_millis() < 1 {
1017                match (self.runtime.0).1.try_pop() {
1018                    None => {
1019                        //当前没有异步任务,则立即返回
1020                        return Ok((self.runtime.0).1.len());
1021                    }
1022                    Some(task) => {
1023                        run_task(task);
1024                    }
1025                }
1026            }
1027        }
1028    }
1029
1030    /// 转换为本地异步单线程任务运行时
1031    pub fn into_local(self) -> SingleTaskRuntime<O, P> {
1032        self.runtime
1033    }
1034}
1035
1036/// 对单线程执行器弹出的一个任务执行轮询。
1037///
1038/// 托管生命周期与多线程驱动共用:认领一个已调度义务,丢弃重复/已完成队列项,
1039/// 轮询期间的唤醒延期处理,并在 `Pending` 时先恢复 Future、再准确重排一次。尽管
1040/// 本执行器只有一个消费者,合并仍可防止有限的集中唤醒和已完成任务的迟到唤醒
1041/// 队列项扩大队列。
1042///
1043/// 公开手工驱动任务保持兼容手工模式,并使用原取出/轮询/恢复路径。每次托管轮询的成本为
1044/// O(1),另加用户轮询和可选的一次队列入队。状态处理自身不新增分配;可选入队继续服从
1045/// 任务池既有的容量和扩容行为。Future 互斥锁不跨用户代码、任务池访问或工作线程通知,
1046/// 也不引入新的阻塞或锁顺序。函数消费一个物理队列 `Arc`、返回 `()`,且可能轮询/
1047/// 析构、重排一次并通知执行器,因此非纯且非幂等。Future/context 仍在既有执行器线程
1048/// 析构,V8 任务池也保持这一边界。
1049#[inline]
1050fn run_task<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>>(
1051    task: Arc<AsyncTask<P, O>>,
1052) {
1053    match task.try_begin_runtime_poll() {
1054        AsyncTaskPollClaim::Discard => return,
1055        AsyncTaskPollClaim::Legacy => {
1056            let waker = waker_ref(&task);
1057            let mut context = Context::from_waker(&*waker);
1058            if let Some(mut future) = task.get_inner() {
1059                if let Poll::Pending = future.as_mut().poll(&mut context) {
1060                    task.set_inner(Some(future));
1061                }
1062            }
1063            return;
1064        },
1065        AsyncTaskPollClaim::Managed => (),
1066    }
1067
1068    // 守卫必须先于局部 Future 声明:栈展开时先析构 Future,再由守卫把析构期间
1069    // 发出的唤醒吸收到终态。
1070    let guard = AsyncTaskPollGuard::new(&task);
1071    let waker = waker_ref(&task);
1072    let mut context = Context::from_waker(&*waker);
1073    let mut future = match task.take_inner_for_runtime_poll() {
1074        Some(future) => future,
1075        None => {
1076            guard.finish_ready();
1077            return;
1078        },
1079    };
1080
1081    match future.as_mut().poll(&mut context) {
1082        Poll::Pending => {
1083            task.restore_inner_after_runtime_poll(future);
1084            if guard.finish_pending() {
1085                requeue_runtime_task(task.get_pool(), &task);
1086            }
1087        },
1088        Poll::Ready(_) => guard.finish_ready(),
1089    }
1090}