1use std::thread;
5use std::pin::Pin;
6use std::sync::Arc;
7use std::ptr::null_mut;
8use std::vec::IntoIter;
9use std::future::Future;
10use std::panic::set_hook;
11use std::any::{Any, TypeId};
12use std::marker::PhantomData;
13use std::ops::{Deref, DerefMut};
14use std::cell::{RefCell, UnsafeCell};
15use std::task::{Waker, Context, Poll};
16use std::time::{Duration, SystemTime};
17use std::io::{Error, Result, ErrorKind};
18use std::alloc::{Layout, set_alloc_error_hook};
19use std::fmt::{Debug, Formatter, Result as FmtResult};
20use std::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, AtomicPtr, Ordering};
21
22pub mod single_thread;
23pub mod multi_thread;
24pub mod worker_thread;
25pub mod serial;
26pub mod serial_local_thread;
27pub mod serial_single_thread;
28pub mod serial_worker_thread;
29pub mod serial_local_compatible_wasm_runtime;
30
31use libc;
32use futures::{future::{FutureExt, BoxFuture},
33 stream::{Stream, BoxStream},
34 task::{ArcWake, AtomicWaker}};
35use parking_lot::{Mutex, Condvar};
36use crossbeam_channel::{Sender, Receiver, unbounded};
37use crossbeam_queue::ArrayQueue;
38use crossbeam_utils::atomic::AtomicCell;
39use flume::{Sender as AsyncSender, Receiver as AsyncReceiver};
40use num_cpus;
41use backtrace::Backtrace;
42use slotmap::{Key, KeyData};
43use quanta::{Clock, Upkeep, Handle, Instant as QInstant};
44
45use pi_hash::XHashMap;
46use pi_cancel_timer::Timer;
47use pi_timer::Timer as NotCancelTimer;
48
49use single_thread::SingleTaskRuntime;
50use worker_thread::{WorkerTaskRunner, WorkerRuntime};
51use multi_thread::{MultiTaskRuntimeBuilder, MultiTaskRuntime, StealableTaskPool};
52
53use crate::lock::spin;
54
55thread_local! {
59 static PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME: AtomicPtr<()> = AtomicPtr::new(null_mut());
60 static PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME_DICT: UnsafeCell<XHashMap<TypeId, Box<dyn Any + 'static>>> = UnsafeCell::new(XHashMap::default());
61}
62
63thread_local! {
67 static PI_ASYNC_THREAD_LOCAL_ID: UnsafeCell<usize> = UnsafeCell::new(usize::MAX);
68}
69
70const DEFAULT_MAX_HIGH_PRIORITY_BOUNDED: usize = 10;
74
75const DEFAULT_HIGH_PRIORITY_BOUNDED: usize = 5;
79
80const DEFAULT_MAX_LOW_PRIORITY_BOUNDED: usize = 0;
84
85static RUNTIME_UID_GEN: AtomicUsize = AtomicUsize::new(1);
89
90static GLOBAL_TIME_LOOP_STATUS: AtomicBool = AtomicBool::new(false);
94
95pub fn startup_global_time_loop(interval: u64) -> Option<GlobalTimeLoopHandle> {
100 if let Err(_) = GLOBAL_TIME_LOOP_STATUS.compare_exchange(false,
101 true,
102 Ordering::AcqRel,
103 Ordering::Relaxed) {
104 None
106 } else {
107 let timer = Upkeep::new_with_clock(Duration::from_millis(interval), Clock::new());
109 let handle = timer.start().unwrap();
110 let clock = Clock::new();
111 let _now = clock.recent();
112
113 Some(GlobalTimeLoopHandle(handle))
114 }
115}
116
117pub struct GlobalTimeLoopHandle(Handle);
121
122impl Drop for GlobalTimeLoopHandle {
123 fn drop(&mut self) {
124 GLOBAL_TIME_LOOP_STATUS.store(false, Ordering::Release);
125 }
126}
127
128pub fn alloc_rt_uid() -> usize {
132 RUNTIME_UID_GEN.fetch_add(1, Ordering::Relaxed)
133}
134
135pub struct TaskId(UnsafeCell<u128>);
139
140impl Debug for TaskId {
141 fn fmt(&self, f: &mut Formatter) -> FmtResult {
142 write!(f, "TaskId[inner = {}]", unsafe { *self.0.get() })
143 }
144}
145
146impl Clone for TaskId {
147 fn clone(&self) -> Self {
148 unsafe {
149 TaskId(UnsafeCell::new(*self.0.get()))
150 }
151 }
152}
153
154impl TaskId {
155 #[inline]
157 pub fn exist_waker<R: 'static>(&self) -> bool {
158 unsafe {
159 let handle = unsafe { TaskHandle::<R>::from_raw((*self.0.get() >> 64) as *const ()) };
160 let inner = &*handle.0;
161 let r = if let Some(waker) = inner.0.swap(None) {
162 inner.0.swap(Some(waker));
163 true
164 } else {
165 false
166 };
167
168 handle.into_raw();
170
171 r
172 }
173 }
174
175 #[inline]
177 pub fn wakeup<R: 'static>(&self) {
178 unsafe {
179 let handle = unsafe { TaskHandle::<R>::from_raw((*self.0.get() >> 64) as *const ()) };
180 let inner = &*handle.0;
181 if let Some(waker) = inner.0.swap(None) {
182 waker.wake();
184 }
185
186 handle.into_raw();
188 }
189 }
190
191 #[inline]
193 pub fn set_waker<R: 'static>(&self, waker: Waker) -> Option<Waker> {
194 unsafe {
195 let handle = unsafe { TaskHandle::<R>::from_raw((*self.0.get() >> 64) as *const ()) };
196 let inner = &*handle.0;
197 let r = inner.0.swap(Some(waker));
198
199 handle.into_raw();
201
202 r
203 }
204 }
205
206 #[inline]
208 pub fn result<R: 'static>(&self) -> Option<R> {
209 unsafe {
210 let handle = unsafe { TaskHandle::<R>::from_raw((*self.0.get() >> 64) as *const ()) };
211 let inner = &*handle.0;
212 let r = inner.1.swap(None);
213
214 handle.into_raw();
216
217 r
218 }
219 }
220
221 #[inline]
223 pub fn set_result<R: 'static>(&self, result: R) -> Option<R> {
224 unsafe {
225 let handle = unsafe { TaskHandle::<R>::from_raw((*self.0.get() >> 64) as *const ()) };
226 let inner = &*handle.0;
227 let r = inner.1.swap(Some(result));
228
229 handle.into_raw();
231
232 r
233 }
234 }
235}
236
237pub(crate) struct TaskHandle<R: 'static>(Box<(
239 AtomicCell<Option<Waker>>, AtomicCell<Option<R>>, )>);
242
243impl<R: 'static> Default for TaskHandle<R> {
244 fn default() -> Self {
245 TaskHandle(Box::new((AtomicCell::new(None), AtomicCell::new(None))))
246 }
247}
248
249impl<R: 'static> TaskHandle<R> {
250 pub unsafe fn from_raw(raw: *const ()) -> TaskHandle<R> {
252 let inner
253 = Box::from_raw(raw as *const (AtomicCell<Option<Waker>>, AtomicCell<Option<R>>) as *mut (AtomicCell<Option<Waker>>, AtomicCell<Option<R>>));
254 TaskHandle(inner)
255 }
256
257 pub fn into_raw(self) -> *const () {
259 Box::into_raw(self.0)
260 as *mut (AtomicCell<Option<Waker>>, AtomicCell<Option<R>>)
261 as *const (AtomicCell<Option<Waker>>, AtomicCell<Option<R>>)
262 as *const ()
263 }
264}
265
266pub(crate) struct TimeoutWaiter {
268 fired: AtomicBool,
269 waker: AtomicWaker,
270}
271
272impl TimeoutWaiter {
273 #[inline]
274 pub fn new() -> Self {
275 TimeoutWaiter {
276 fired: AtomicBool::new(false),
277 waker: AtomicWaker::new(),
278 }
279 }
280
281 #[inline]
282 pub fn is_fired(&self) -> bool {
283 self.fired.load(Ordering::Acquire)
284 }
285
286 #[inline]
287 pub fn register(&self, waker: &Waker) {
288 self.waker.register(waker);
289 }
290
291 #[inline]
292 pub fn fire(&self) {
293 if !self.fired.swap(true, Ordering::AcqRel) {
294 self.waker.wake();
295 }
296 }
297
298 #[inline]
299 pub fn clear_waker(&self) {
300 let _ = self.waker.take();
301 }
302}
303
304#[inline]
345pub(crate) fn wake_registered_thread_waker(worker_waker: &Arc<(AtomicBool, Mutex<()>, Condvar)>) -> bool {
346 let (is_sleep, lock, condvar) = &**worker_waker;
347 let _locked = lock.lock();
348 if is_sleep
349 .compare_exchange(true, false, Ordering::AcqRel, Ordering::Acquire)
350 .is_ok()
351 {
352 condvar.notify_one();
353 return true;
354 }
355
356 false
357}
358
359#[inline]
383pub(crate) fn wake_thread_waker(worker_waker: &Arc<(AtomicBool, Mutex<()>, Condvar)>) -> bool {
384 if !worker_waker.0.load(Ordering::Acquire) {
385 return false;
386 }
387
388 wake_registered_thread_waker(worker_waker)
389}
390
391#[inline]
432pub(crate) fn wake_waiting_worker(
433 waits: &ArrayQueue<Arc<(AtomicBool, Mutex<()>, Condvar)>>,
434) -> bool {
435 let scan_len = waits.capacity();
436 for _ in 0..scan_len {
437 match waits.pop() {
438 Some(worker_waker) => {
439 if wake_registered_thread_waker(&worker_waker) {
440 return true;
441 }
442 },
443 None => {
444 return false;
445 },
446 }
447 }
448
449 false
450}
451
452#[inline]
488pub(crate) fn prune_stale_waiting_workers(
489 waits: &ArrayQueue<Arc<(AtomicBool, Mutex<()>, Condvar)>>,
490) -> usize {
491 let scan_len = waits.len();
492 let mut pruned = 0;
493
494 for _ in 0..scan_len {
495 let Some(worker_waker) = waits.pop() else {
496 break;
497 };
498
499 let is_live = {
500 let _locked = worker_waker.1.lock();
501 worker_waker.0.load(Ordering::Acquire)
502 };
503
504 if is_live {
505 match waits.push(worker_waker) {
506 Ok(()) => (),
507 Err(worker_waker) => {
508 let _ = wake_registered_thread_waker(&worker_waker);
509 },
510 }
511 } else {
512 pruned += 1;
513 }
514 }
515
516 pruned
517}
518
519#[inline]
555pub(crate) fn register_waiting_worker(
556 waits: &ArrayQueue<Arc<(AtomicBool, Mutex<()>, Condvar)>>,
557 worker_waker: &Arc<(AtomicBool, Mutex<()>, Condvar)>,
558) -> bool {
559 waits.push(worker_waker.clone()).is_ok()
560}
561
562#[cfg(test)]
563mod timeout_waiter_tests {
564 use super::{
565 prune_stale_waiting_workers, register_waiting_worker, wake_thread_waker,
566 wake_waiting_worker, TimeoutWaiter,
567 };
568 use crossbeam_queue::ArrayQueue;
569 use futures::task::{waker_ref, ArcWake};
570 use parking_lot::{Condvar, Mutex};
571 use std::sync::{
572 atomic::{AtomicBool, AtomicUsize, Ordering},
573 Arc,
574 };
575
576 struct WakeCounter(AtomicUsize);
577
578 impl ArcWake for WakeCounter {
579 fn wake_by_ref(arc_self: &Arc<Self>) {
580 arc_self.0.fetch_add(1, Ordering::SeqCst);
581 }
582 }
583
584 #[test]
585 fn test_timeout_waiter_fire_wakes_once() {
586 let waiter = TimeoutWaiter::new();
587 let counter = Arc::new(WakeCounter(AtomicUsize::new(0)));
588 let waker = waker_ref(&counter);
589
590 waiter.register(&waker);
591 waiter.fire();
592 waiter.fire();
593
594 assert!(waiter.is_fired());
595 assert_eq!(counter.0.load(Ordering::SeqCst), 1);
596 }
597
598 #[test]
599 fn test_timeout_waiter_clear_waker_before_fire() {
600 let waiter = TimeoutWaiter::new();
601 let counter = Arc::new(WakeCounter(AtomicUsize::new(0)));
602 let waker = waker_ref(&counter);
603
604 waiter.register(&waker);
605 waiter.clear_waker();
606 waiter.fire();
607
608 assert!(waiter.is_fired());
609 assert_eq!(counter.0.load(Ordering::SeqCst), 0);
610 }
611
612 #[test]
613 fn test_timeout_waiter_replaces_waker() {
614 let waiter = TimeoutWaiter::new();
615 let old_counter = Arc::new(WakeCounter(AtomicUsize::new(0)));
616 let new_counter = Arc::new(WakeCounter(AtomicUsize::new(0)));
617 let old_waker = waker_ref(&old_counter);
618 let new_waker = waker_ref(&new_counter);
619
620 waiter.register(&old_waker);
621 waiter.register(&new_waker);
622 waiter.fire();
623
624 assert!(waiter.is_fired());
625 assert_eq!(old_counter.0.load(Ordering::SeqCst), 0);
626 assert_eq!(new_counter.0.load(Ordering::SeqCst), 1);
627 }
628
629 #[test]
630 fn test_worker_waker_wakes_once() {
631 let worker_waker = Arc::new((AtomicBool::new(true), Mutex::new(()), Condvar::new()));
632
633 assert!(wake_thread_waker(&worker_waker));
634 assert!(!worker_waker.0.load(Ordering::SeqCst));
635 assert!(!wake_thread_waker(&worker_waker));
636 }
637
638 #[test]
639 fn test_worker_waker_wait_queue_skips_stale_and_wakes_one_sleeping_worker() {
640 let waits = ArrayQueue::new(4);
641 let stale = Arc::new((AtomicBool::new(false), Mutex::new(()), Condvar::new()));
642 let sleeping = Arc::new((AtomicBool::new(true), Mutex::new(()), Condvar::new()));
643
644 waits.push(stale).unwrap();
645 waits.push(sleeping.clone()).unwrap();
646
647 assert!(wake_waiting_worker(&waits));
648 assert!(!sleeping.0.load(Ordering::SeqCst));
649 assert!(!wake_waiting_worker(&waits));
650 }
651
652 #[test]
653 fn test_worker_waker_register_and_prune_stale_waiter() {
654 let waits = ArrayQueue::new(1);
655 let stale = Arc::new((AtomicBool::new(false), Mutex::new(()), Condvar::new()));
656 let current = Arc::new((AtomicBool::new(false), Mutex::new(()), Condvar::new()));
657
658 waits.push(stale).unwrap();
659 assert!(!register_waiting_worker(&waits, ¤t));
660 assert_eq!(prune_stale_waiting_workers(&waits), 1);
661 assert!(register_waiting_worker(&waits, ¤t));
662 }
663
664 #[test]
665 fn test_worker_waker_prune_keeps_live_waiter() {
666 let waits = ArrayQueue::new(1);
667 let sleeping = Arc::new((AtomicBool::new(true), Mutex::new(()), Condvar::new()));
668
669 waits.push(sleeping.clone()).unwrap();
670 assert_eq!(prune_stale_waiting_workers(&waits), 0);
671 assert!(wake_waiting_worker(&waits));
672 assert!(!sleeping.0.load(Ordering::SeqCst));
673 }
674}
675
676pub struct AsyncTask<
680 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
681 O: Default + 'static = (),
682> {
683 uid: TaskId, future: Mutex<Option<BoxFuture<'static, O>>>, pool: Arc<P>, priority: usize, context: Option<UnsafeCell<Box<dyn Any>>>, }
689
690impl<
691 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
692 O: Default + 'static,
693> Drop for AsyncTask<P, O> {
694 fn drop(&mut self) {
695 let _ = unsafe { TaskHandle::<O>::from_raw((*self.uid.0.get() >> 64) as usize as *const ()) };
696 }
697}
698
699unsafe impl<
700 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
701 O: Default + 'static,
702> Send for AsyncTask<P, O> {}
703unsafe impl<
704 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
705 O: Default + 'static,
706> Sync for AsyncTask<P, O> {}
707
708impl<
709 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>,
710 O: Default + 'static,
711> ArcWake for AsyncTask<P, O> {
712 fn wake_by_ref(arc_self: &Arc<Self>) {
713 let pool = arc_self.get_pool();
714 let _ = pool.push_keep(arc_self.clone());
715
716 if let Some(waits) = pool.get_waits() {
717 let _ = wake_waiting_worker(waits);
719 } else {
720 if let Some(thread_waker) = pool.get_thread_waker() {
722 let _ = wake_thread_waker(thread_waker);
723 }
724 }
725 }
726}
727
728impl<
729 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>,
730 O: Default + 'static,
731> AsyncTask<P, O> {
732 pub fn new(uid: TaskId,
734 pool: Arc<P>,
735 priority: usize,
736 future: Option<BoxFuture<'static, O>>) -> AsyncTask<P, O> {
737 AsyncTask {
738 uid,
739 future: Mutex::new(future),
740 pool,
741 priority,
742 context: None,
743 }
744 }
745
746 pub fn with_context<C: 'static>(uid: TaskId,
748 pool: Arc<P>,
749 priority: usize,
750 future: Option<BoxFuture<'static, O>>,
751 context: C) -> AsyncTask<P, O> {
752 let any = Box::new(context);
753
754 AsyncTask {
755 uid,
756 future: Mutex::new(future),
757 pool,
758 priority,
759 context: Some(UnsafeCell::new(any)),
760 }
761 }
762
763 pub fn with_runtime_and_context<RT, C>(runtime: &RT,
765 priority: usize,
766 future: Option<BoxFuture<'static, O>>,
767 context: C) -> AsyncTask<P, O>
768 where RT: AsyncRuntime<O, Pool = P>,
769 C: Send + 'static {
770 let any = Box::new(context);
771
772 AsyncTask {
773 uid: runtime.alloc::<O>(),
774 future: Mutex::new(future),
775 pool: runtime.shared_pool(),
776 priority,
777 context: Some(UnsafeCell::new(any)),
778 }
779 }
780
781 pub fn is_enable_wakeup(&self) -> bool {
783 self.uid.exist_waker::<O>()
784 }
785
786 pub fn get_inner(&self) -> Option<BoxFuture<'static, O>> {
788 self.future.lock().take()
789 }
790
791 pub fn set_inner(&self, inner: Option<BoxFuture<'static, O>>) {
793 *self.future.lock() = inner;
794 }
795
796 #[inline]
798 pub fn owner(&self) -> usize {
799 unsafe {
800 *self.uid.0.get() as usize
801 }
802 }
803
804 #[inline]
806 pub fn priority(&self) -> usize {
807 self.priority
808 }
809
810 pub fn exist_context(&self) -> bool {
812 self.context.is_some()
813 }
814
815 pub fn get_context<C: Send + 'static>(&self) -> Option<&C> {
817 if let Some(context) = &self.context {
818 let any = unsafe { &*context.get() };
820 return <dyn Any>::downcast_ref::<C>(&**any);
821 }
822
823 None
824 }
825
826 pub fn get_context_mut<C: Send + 'static>(&self) -> Option<&mut C> {
828 if let Some(context) = &self.context {
829 let any = unsafe { &mut *context.get() };
831 return <dyn Any>::downcast_mut::<C>(&mut **any);
832 }
833
834 None
835 }
836
837 pub fn set_context<C: Send + 'static>(&self, new: C) {
839 if let Some(context) = &self.context {
840 let _ = unsafe { &*context.get() };
842
843 let any: Box<dyn Any + 'static> = Box::new(new);
845 unsafe { *context.get() = any; }
846 }
847 }
848
849 pub fn get_pool(&self) -> &P {
851 self.pool.as_ref()
852 }
853}
854
855pub trait AsyncTaskPool<O: Default + 'static = ()>: Default + Send + Sync + 'static {
859 type Pool: AsyncTaskPoolExt<O> + AsyncTaskPool<O>;
860
861 fn get_thread_id(&self) -> usize;
863
864 fn len(&self) -> usize;
866
867 fn push(&self, task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()>;
869
870 fn push_local(&self, task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()>;
872
873 fn push_priority(&self,
875 priority: usize,
876 task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()>;
877
878 fn push_keep(&self, task: Arc<AsyncTask<Self::Pool, O>>) -> Result<()>;
880
881 fn try_pop(&self) -> Option<Arc<AsyncTask<Self::Pool, O>>>;
883
884 fn try_pop_all(&self) -> IntoIter<Arc<AsyncTask<Self::Pool, O>>>;
886
887 fn get_thread_waker(&self) -> Option<&Arc<(AtomicBool, Mutex<()>, Condvar)>>;
889}
890
891pub trait AsyncTaskPoolExt<O: Default + 'static = ()>: Send + Sync + 'static {
895 fn set_waits(&mut self,
897 _waits: Arc<ArrayQueue<Arc<(AtomicBool, Mutex<()>, Condvar)>>>) {}
898
899 fn get_waits(&self) -> Option<&Arc<ArrayQueue<Arc<(AtomicBool, Mutex<()>, Condvar)>>>> {
901 None
903 }
904
905 fn idler_len(&self) -> usize {
907 0
909 }
910
911 fn spawn_worker(&self) -> Option<usize> {
913 None
915 }
916
917 fn worker_len(&self) -> usize {
919 #[cfg(not(target_arch = "wasm32"))]
921 return num_cpus::get();
922 #[cfg(target_arch = "wasm32")]
923 return 1;
924 }
925
926 fn buffer_len(&self) -> usize {
928 0
930 }
931
932 fn set_thread_waker(&mut self, _thread_waker: Arc<(AtomicBool, Mutex<()>, Condvar)>) {
934 }
936
937 fn clone_thread_waker(&self) -> Option<Arc<(AtomicBool, Mutex<()>, Condvar)>> {
939 None
941 }
942
943 fn close_worker(&self) {
945 }
947}
948
949pub trait AsyncRuntime<O: Default + 'static = ()>: Clone + Send + Sync + 'static {
953 type Pool: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = Self::Pool>;
954
955 fn shared_pool(&self) -> Arc<Self::Pool>;
957
958 fn get_id(&self) -> usize;
960
961 fn wait_len(&self) -> usize;
963
964 fn len(&self) -> usize;
966
967 fn alloc<R: 'static>(&self) -> TaskId;
969
970 fn spawn<F>(&self, future: F) -> Result<TaskId>
972 where F: Future<Output = O> + Send + 'static;
973
974 fn spawn_local<F>(&self, future: F) -> Result<TaskId>
976 where F: Future<Output = O> + Send + 'static;
977
978 fn spawn_priority<F>(&self, priority: usize, future: F) -> Result<TaskId>
980 where F: Future<Output = O> + Send + 'static;
981
982 fn spawn_yield<F>(&self, future: F) -> Result<TaskId>
984 where F: Future<Output = O> + Send + 'static;
985
986 fn spawn_timing<F>(&self, future: F, time: usize) -> Result<TaskId>
988 where F: Future<Output = O> + Send + 'static;
989
990 fn spawn_by_id<F>(&self, task_id: TaskId, future: F) -> Result<()>
992 where F: Future<Output = O> + Send + 'static;
993
994 fn spawn_local_by_id<F>(&self, task_id: TaskId, future: F) -> Result<()>
996 where F: Future<Output = O> + Send + 'static;
997
998 fn spawn_priority_by_id<F>(&self,
1000 task_id: TaskId,
1001 priority: usize,
1002 future: F) -> Result<()>
1003 where F: Future<Output = O> + Send + 'static;
1004
1005 fn spawn_yield_by_id<F>(&self, task_id: TaskId, future: F) -> Result<()>
1007 where F: Future<Output = O> + Send + 'static;
1008
1009 fn spawn_timing_by_id<F>(&self,
1011 task_id: TaskId,
1012 future: F,
1013 time: usize) -> Result<()>
1014 where F: Future<Output = O> + Send + 'static;
1015
1016 fn pending<Output: 'static>(&self, task_id: &TaskId, waker: Waker) -> Poll<Output>;
1018
1019 fn wakeup<Output: 'static>(&self, task_id: &TaskId);
1021
1022 fn wait<V: Send + 'static>(&self) -> AsyncWait<V>;
1024
1025 fn wait_any<V: Send + 'static>(&self, capacity: usize) -> AsyncWaitAny<V>;
1027
1028 fn wait_any_callback<V: Send + 'static>(&self, capacity: usize) -> AsyncWaitAnyCallback<V>;
1030
1031 fn map_reduce<V: Send + 'static>(&self, capacity: usize) -> AsyncMapReduce<V>;
1033
1034 fn timeout(&self, timeout: usize) -> BoxFuture<'static, ()>;
1036
1037 fn yield_now(&self) -> BoxFuture<'static, ()>;
1039
1040 fn pipeline<S, SO, F, FO>(&self, input: S, filter: F) -> BoxStream<'static, FO>
1042 where S: Stream<Item = SO> + Send + 'static,
1043 SO: Send + 'static,
1044 F: FnMut(SO) -> AsyncPipelineResult<FO> + Send + 'static,
1045 FO: Send + 'static;
1046
1047 fn close(&self) -> bool;
1049}
1050
1051pub trait AsyncRuntimeExt<O: Default + 'static = ()> {
1055 fn spawn_with_context<F, C>(&self,
1057 task_id: TaskId,
1058 future: F,
1059 context: C) -> Result<()>
1060 where F: Future<Output = O> + Send + 'static,
1061 C: 'static;
1062
1063 fn spawn_timing_with_context<F, C>(&self,
1065 task_id: TaskId,
1066 future: F,
1067 context: C,
1068 time: usize) -> Result<()>
1069 where F: Future<Output = O> + Send + 'static,
1070 C: Send + 'static;
1071
1072 fn block_on<F>(&self, future: F) -> Result<F::Output>
1074 where F: Future + Send + 'static,
1075 <F as Future>::Output: Default + Send + 'static;
1076}
1077
1078pub struct AsyncRuntimeBuilder<O: Default + 'static = ()>(PhantomData<O>);
1082
1083impl<O: Default + 'static> AsyncRuntimeBuilder<O> {
1084 pub fn default_worker_thread(worker_name: Option<&str>,
1086 worker_stack_size: Option<usize>,
1087 worker_sleep_timeout: Option<u64>,
1088 worker_loop_interval: Option<Option<u64>>) -> WorkerRuntime<O> {
1089 let runner = WorkerTaskRunner::default();
1090
1091 let thread_name = if let Some(name) = worker_name {
1092 name
1093 } else {
1094 "Default-Single-Worker"
1096 };
1097 let thread_stack_size = if let Some(size) = worker_stack_size {
1098 size
1099 } else {
1100 2 * 1024 * 1024
1102 };
1103 let sleep_timeout = if let Some(timeout) = worker_sleep_timeout {
1104 timeout
1105 } else {
1106 1
1108 };
1109 let loop_interval = if let Some(interval) = worker_loop_interval {
1110 interval
1111 } else {
1112 None
1114 };
1115
1116 let clock = Clock::new();
1118 let runner_copy = runner.clone();
1119 let rt_copy = runner.get_runtime();
1120 let rt = runner.startup(
1121 thread_name,
1122 thread_stack_size,
1123 sleep_timeout,
1124 loop_interval,
1125 move || {
1126 let last = clock.recent();
1127 match runner_copy.run_once() {
1128 Err(e) => {
1129 panic!("Run runner failed, reason: {:?}", e);
1130 },
1131 Ok(len) => {
1132 (len == 0,
1133 clock
1134 .recent()
1135 .duration_since(last))
1136 },
1137 }
1138 },
1139 move || {
1140 rt_copy.wait_len() + rt_copy.len()
1141 },
1142 );
1143
1144 rt
1145 }
1146
1147 pub fn custom_worker_thread<P, F0, F1>(pool: P,
1149 worker_handle: Arc<AtomicBool>,
1150 worker_condvar: Arc<(AtomicBool, Mutex<()>, Condvar)>,
1151 thread_name: &str,
1152 thread_stack_size: usize,
1153 sleep_timeout: u64,
1154 loop_interval: Option<u64>,
1155 loop_func: F0,
1156 get_queue_len: F1) -> WorkerRuntime<O, P>
1157 where P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>,
1158 F0: Fn() -> (bool, Duration) + Send + 'static,
1159 F1: Fn() -> usize + Send + 'static {
1160 let runner = WorkerTaskRunner::new(pool,
1161 worker_handle,
1162 worker_condvar);
1163
1164 let rt_copy = runner.get_runtime();
1166 let rt = runner.startup(
1167 thread_name,
1168 thread_stack_size,
1169 sleep_timeout,
1170 loop_interval,
1171 loop_func,
1172 move || {
1173 rt_copy.wait_len() + get_queue_len()
1174 },
1175 );
1176
1177 rt
1178 }
1179
1180 pub fn default_multi_thread(worker_prefix: Option<&str>,
1215 worker_stack_size: Option<usize>,
1216 worker_size: Option<usize>,
1217 worker_sleep_timeout: Option<u64>) -> MultiTaskRuntime<O> {
1218 let mut builder = if let Some(size) = worker_size.filter(|size| *size > 0) {
1219 let pool = StealableTaskPool::with(size,
1220 65535,
1221 [1, 1],
1222 3000);
1223 MultiTaskRuntimeBuilder::new(pool)
1224 .thread_stack_size(2 * 1024 * 1024)
1225 .set_timer_interval(1)
1226 } else {
1227 MultiTaskRuntimeBuilder::default()
1228 };
1229
1230 if let Some(size) = worker_size {
1231 builder = builder
1232 .init_worker_size(size)
1233 .set_worker_limit(size, size);
1234 }
1235 if let Some(thread_prefix) = worker_prefix {
1236 builder = builder.thread_prefix(thread_prefix);
1237 }
1238 if let Some(thread_stack_size) = worker_stack_size {
1239 builder = builder.thread_stack_size(thread_stack_size);
1240 }
1241 if let Some(sleep_timeout) = worker_sleep_timeout {
1242 builder = builder.set_timeout(sleep_timeout);
1243 }
1244
1245 builder.build()
1246 }
1247
1248 pub fn custom_multi_thread<P>(pool: P,
1250 worker_prefix: &str,
1251 worker_stack_size: usize,
1252 worker_size: usize,
1253 worker_sleep_timeout: u64,
1254 worker_timer_interval: usize) -> MultiTaskRuntime<O, P>
1255 where P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P> {
1256 MultiTaskRuntimeBuilder::new(pool)
1257 .thread_prefix(worker_prefix)
1258 .thread_stack_size(worker_stack_size)
1259 .init_worker_size(worker_size)
1260 .set_worker_limit(worker_size, worker_size)
1261 .set_timeout(worker_sleep_timeout)
1262 .set_timer_interval(worker_timer_interval)
1263 .build()
1264 }
1265}
1266
1267pub fn bind_local_thread<O: Default + 'static>(runtime: LocalAsyncRuntime<O>) {
1269 match PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME.try_with(move |rt| {
1270 let raw = Arc::into_raw(Arc::new(runtime)) as *mut LocalAsyncRuntime<O> as *mut ();
1271 rt.store(raw, Ordering::Relaxed);
1272 }) {
1273 Err(e) => {
1274 panic!("Bind single runtime to local thread failed, reason: {:?}", e);
1275 },
1276 Ok(_) => (),
1277 }
1278}
1279
1280pub fn unbind_local_thread() {
1282 let _ = PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME.try_with(move |rt| {
1283 rt.store(null_mut(), Ordering::Relaxed);
1284 });
1285}
1286
1287pub struct LocalAsyncRuntime<O: Default + 'static> {
1291 inner: *const (), get_id_func: fn(*const ()) -> usize, spawn_func: fn(*const (), BoxFuture<'static, O>) -> Result<()>, spawn_local_func: fn(*const (), BoxFuture<'static, O>) -> Result<()>, spawn_timing_func: fn(*const (), BoxFuture<'static, O>, usize) -> Result<()>, timeout_func: fn(*const (), usize) -> BoxFuture<'static, ()>, }
1298
1299unsafe impl<O: Default + 'static> Send for LocalAsyncRuntime<O> {}
1300unsafe impl<O: Default + 'static> Sync for LocalAsyncRuntime<O> {}
1301
1302impl<O: Default + 'static> LocalAsyncRuntime<O> {
1303 pub fn new(inner: *const (),
1305 get_id_func: fn(*const ()) -> usize,
1306 spawn_func: fn(*const (), BoxFuture<'static, O>) -> Result<()>,
1307 spawn_local_func: fn(*const (), BoxFuture<'static, O>) -> Result<()>,
1308 spawn_timing_func: fn(*const (), BoxFuture<'static, O>, usize) -> Result<()>,
1309 timeout_func: fn(*const (), usize) -> BoxFuture<'static, ()>) -> Self {
1310 LocalAsyncRuntime {
1311 inner,
1312 get_id_func,
1313 spawn_func,
1314 spawn_local_func,
1315 spawn_timing_func,
1316 timeout_func,
1317 }
1318 }
1319
1320 #[inline]
1322 pub fn get_id(&self) -> usize {
1323 (self.get_id_func)(self.inner)
1324 }
1325
1326 #[inline]
1328 pub fn spawn<F>(&self, future: F) -> Result<()>
1329 where F: Future<Output = O> + Send + 'static {
1330 (self.spawn_func)(self.inner, async move {
1331 future.await
1332 }.boxed())
1333 }
1334
1335 #[inline]
1337 pub fn spawn_local<F>(&self, future: F) -> Result<()>
1338 where F: Future<Output = O> + Send + 'static {
1339 (self.spawn_local_func)(self.inner, async move {
1340 future.await
1341 }.boxed())
1342 }
1343
1344 #[inline]
1346 pub fn sapwn_timing_func<F>(&self, future: F, timeout: usize) -> Result<()>
1347 where F: Future<Output = O> + Send + 'static {
1348 (self.spawn_timing_func)(self.inner,
1349 async move {
1350 future.await
1351 }.boxed(),
1352 timeout)
1353 }
1354
1355 #[inline]
1357 pub fn timeout(&self, timeout: usize) -> BoxFuture<'static, ()> {
1358 (self.timeout_func)(self.inner, timeout)
1359 }
1360}
1361
1362pub fn local_async_runtime<O: Default + 'static>() -> Option<Arc<LocalAsyncRuntime<O>>> {
1367 match PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME.try_with(move |ptr| {
1368 let raw = ptr.load(Ordering::Relaxed) as *const LocalAsyncRuntime<O>;
1369 unsafe {
1370 if raw.is_null() {
1371 None
1373 } else {
1374 let shared: Arc<LocalAsyncRuntime<O>> = unsafe { Arc::from_raw(raw) };
1376 let result = shared.clone();
1377 Arc::into_raw(shared); Some(result)
1379 }
1380 }
1381 }) {
1382 Err(_) => None, Ok(rt) => rt,
1384 }
1385}
1386
1387pub fn spawn_local<O, F>(future: F) -> Result<()>
1392 where O: Default + 'static,
1393 F: Future<Output = O> + Send + 'static {
1394 if let Some(rt) = local_async_runtime::<O>() {
1395 rt.spawn(future)
1396 } else {
1397 Err(Error::new(ErrorKind::Other, format!("Spawn task to local thread failed, reason: runtime not exist")))
1398 }
1399}
1400
1401pub fn get_local_dict<T: 'static>() -> Option<&'static T> {
1405 match PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME_DICT.try_with(move |dict| {
1406 unsafe {
1407 if let Some(any) = (&*dict.get()).get(&TypeId::of::<T>()) {
1408 <dyn Any>::downcast_ref::<T>(&**any)
1410 } else {
1411 None
1413 }
1414 }
1415 }) {
1416 Err(_) => {
1417 None
1418 },
1419 Ok(result) => {
1420 result
1421 }
1422 }
1423}
1424
1425pub fn get_local_dict_mut<T: 'static>() -> Option<&'static mut T> {
1429 match PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME_DICT.try_with(move |dict| {
1430 unsafe {
1431 if let Some(any) = (&mut *dict.get()).get_mut(&TypeId::of::<T>()) {
1432 <dyn Any>::downcast_mut::<T>(&mut **any)
1434 } else {
1435 None
1437 }
1438 }
1439 }) {
1440 Err(_) => {
1441 None
1442 },
1443 Ok(result) => {
1444 result
1445 }
1446 }
1447}
1448
1449pub fn set_local_dict<T: 'static>(value: T) -> Option<T> {
1453 match PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME_DICT.try_with(move |dict| {
1454 unsafe {
1455 let result = if let Some(any) = (&mut *dict.get()).remove(&TypeId::of::<T>()) {
1456 if let Ok(r) = any.downcast() {
1458 Some(*r)
1460 } else {
1461 None
1462 }
1463 } else {
1464 None
1466 };
1467
1468 (&mut *dict.get()).insert(TypeId::of::<T>(), Box::new(value) as Box<dyn Any>);
1470
1471 result
1472 }
1473 }) {
1474 Err(_) => {
1475 None
1476 },
1477 Ok(result) => {
1478 result
1479 }
1480 }
1481}
1482
1483pub fn remove_local_dict<T: 'static>() -> Option<T> {
1487 match PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME_DICT.try_with(move |dict| {
1488 unsafe {
1489 if let Some(any) = (&mut *dict.get()).remove(&TypeId::of::<T>()) {
1490 if let Ok(r) = any.downcast() {
1492 Some(*r)
1494 } else {
1495 None
1496 }
1497 } else {
1498 None
1500 }
1501 }
1502 }) {
1503 Err(_) => {
1504 None
1505 },
1506 Ok(result) => {
1507 result
1508 }
1509 }
1510}
1511
1512pub fn clear_local_dict() -> Result<()> {
1516 match PI_ASYNC_LOCAL_THREAD_ASYNC_RUNTIME_DICT.try_with(move |dict| {
1517 unsafe {
1518 (&mut *dict.get()).clear();
1519 }
1520 }) {
1521 Err(e) => {
1522 Err(Error::new(ErrorKind::Other, format!("Clear local dict failed, reason: {:?}", e)))
1523 },
1524 Ok(_) => {
1525 Ok(())
1526 }
1527 }
1528}
1529
1530const ASYNC_VALUE_EMPTY: u8 = 0;
1531const ASYNC_VALUE_WAITING: u8 = 1;
1532const ASYNC_VALUE_SETTING: u8 = 2;
1533const ASYNC_VALUE_READY: u8 = 3;
1534const ASYNC_VALUE_TAKING: u8 = 4;
1535const ASYNC_VALUE_CONSUMED: u8 = 5;
1536
1537pub struct AsyncValue<V: Send + 'static>(Arc<InnerAsyncValue<V>>);
1558
1559unsafe impl<V: Send + 'static> Send for AsyncValue<V> {}
1560unsafe impl<V: Send + 'static> Sync for AsyncValue<V> {}
1561
1562impl<V: Send + 'static> Clone for AsyncValue<V> {
1563 fn clone(&self) -> Self {
1564 AsyncValue(self.0.clone())
1565 }
1566}
1567
1568impl<V: Send + 'static> Debug for AsyncValue<V> {
1569 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
1570 write!(f,
1571 "AsyncValue[status = {}]",
1572 self.0.status.load(Ordering::Acquire))
1573 }
1574}
1575
1576impl<V: Send + 'static> Future for AsyncValue<V> {
1577 type Output = V;
1578
1579 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1580 let mut spin_len = 1;
1581 loop {
1582 match self.0.status.load(Ordering::Acquire) {
1583 ASYNC_VALUE_EMPTY => {
1584 self.0.waker.register(cx.waker());
1585 match self.0.status.compare_exchange(ASYNC_VALUE_EMPTY,
1586 ASYNC_VALUE_WAITING,
1587 Ordering::AcqRel,
1588 Ordering::Acquire) {
1589 Ok(_) => {
1590 return Poll::Pending;
1591 },
1592 Err(ASYNC_VALUE_EMPTY) => {
1593 continue;
1594 },
1595 Err(ASYNC_VALUE_WAITING) | Err(ASYNC_VALUE_SETTING) => {
1596 return Poll::Pending;
1597 },
1598 Err(ASYNC_VALUE_READY) => {
1599 continue;
1600 },
1601 Err(ASYNC_VALUE_TAKING) => {
1602 spin_len = spin(spin_len);
1603 continue;
1604 },
1605 Err(ASYNC_VALUE_CONSUMED) => {
1606 panic!("AsyncValue polled after completion");
1607 },
1608 Err(_) => {
1609 panic!("AsyncValue entered invalid state");
1610 },
1611 }
1612 },
1613 ASYNC_VALUE_WAITING | ASYNC_VALUE_SETTING => {
1614 self.0.waker.register(cx.waker());
1615 match self.0.status.load(Ordering::Acquire) {
1616 ASYNC_VALUE_READY => {
1617 continue;
1618 },
1619 ASYNC_VALUE_TAKING => {
1620 spin_len = spin(spin_len);
1621 continue;
1622 },
1623 ASYNC_VALUE_CONSUMED => {
1624 panic!("AsyncValue polled after completion");
1625 },
1626 _ => {
1627 return Poll::Pending;
1628 },
1629 }
1630 },
1631 ASYNC_VALUE_READY => {
1632 match self.0.status.compare_exchange(ASYNC_VALUE_READY,
1633 ASYNC_VALUE_TAKING,
1634 Ordering::AcqRel,
1635 Ordering::Acquire) {
1636 Ok(_) => {
1637 let value = unsafe { (*self.0.value.get()).take().unwrap() };
1638 self.0.status.store(ASYNC_VALUE_CONSUMED, Ordering::Release);
1639 return Poll::Ready(value);
1640 },
1641 Err(ASYNC_VALUE_TAKING) => {
1642 spin_len = spin(spin_len);
1643 continue;
1644 },
1645 Err(ASYNC_VALUE_CONSUMED) => {
1646 panic!("AsyncValue polled after completion");
1647 },
1648 Err(_) => {
1649 continue;
1650 },
1651 }
1652 },
1653 ASYNC_VALUE_TAKING => {
1654 spin_len = spin(spin_len);
1656 continue;
1657 },
1658 ASYNC_VALUE_CONSUMED => {
1659 panic!("AsyncValue polled after completion");
1660 },
1661 _ => {
1662 panic!("AsyncValue entered invalid state");
1663 },
1664 }
1665 }
1666 }
1667}
1668
1669impl<V: Send + 'static> AsyncValue<V> {
1673 pub fn new() -> Self {
1675 let inner = InnerAsyncValue {
1676 value: UnsafeCell::new(None),
1677 waker: AtomicWaker::new(),
1678 status: AtomicU8::new(ASYNC_VALUE_EMPTY),
1679 };
1680
1681 AsyncValue(Arc::new(inner))
1682 }
1683
1684 pub fn is_complete(&self) -> bool {
1686 match self.0.status.load(Ordering::Acquire) {
1687 ASYNC_VALUE_READY | ASYNC_VALUE_TAKING | ASYNC_VALUE_CONSUMED => true,
1688 _ => false,
1689 }
1690 }
1691
1692 pub fn set(self, value: V) {
1694 let mut value = Some(value);
1695 loop {
1696 match self.0.status.load(Ordering::Acquire) {
1697 ASYNC_VALUE_EMPTY => {
1698 match self.0.status.compare_exchange(ASYNC_VALUE_EMPTY,
1699 ASYNC_VALUE_SETTING,
1700 Ordering::AcqRel,
1701 Ordering::Acquire) {
1702 Ok(_) => {
1703 unsafe { *self.0.value.get() = value.take(); }
1704 self.0.status.store(ASYNC_VALUE_READY, Ordering::Release);
1705 self.0.waker.wake();
1706 return;
1707 },
1708 Err(_) => {
1709 continue;
1710 },
1711 }
1712 },
1713 ASYNC_VALUE_WAITING => {
1714 match self.0.status.compare_exchange(ASYNC_VALUE_WAITING,
1715 ASYNC_VALUE_SETTING,
1716 Ordering::AcqRel,
1717 Ordering::Acquire) {
1718 Ok(_) => {
1719 unsafe { *self.0.value.get() = value.take(); }
1720 self.0.status.store(ASYNC_VALUE_READY, Ordering::Release);
1721 self.0.waker.wake();
1722 return;
1723 },
1724 Err(_) => {
1725 continue;
1726 },
1727 }
1728 },
1729 _ => {
1730 return;
1732 }
1733 }
1734 }
1735 }
1736}
1737
1738pub struct InnerAsyncValue<V: Send + 'static> {
1740 value: UnsafeCell<Option<V>>, waker: AtomicWaker, status: AtomicU8, }
1744
1745pub struct AsyncVariableGuard<'a, V: Send + 'static> {
1749 value: &'a UnsafeCell<Option<V>>, waker: &'a UnsafeCell<Option<Waker>>, status: &'a AtomicU8, }
1753
1754unsafe impl<V: Send + 'static> Send for AsyncVariableGuard<'_, V> {}
1755
1756impl<V: Send + 'static> Drop for AsyncVariableGuard<'_, V> {
1757 fn drop(&mut self) {
1758 self.status.fetch_sub(2, Ordering::Relaxed);
1762 }
1763}
1764
1765impl<V: Send + 'static> Deref for AsyncVariableGuard<'_, V> {
1766 type Target = Option<V>;
1767
1768 fn deref(&self) -> &Self::Target {
1769 unsafe {
1770 &*self.value.get()
1771 }
1772 }
1773}
1774
1775impl<V: Send + 'static> DerefMut for AsyncVariableGuard<'_, V> {
1776 fn deref_mut(&mut self) -> &mut Self::Target {
1777 unsafe {
1778 &mut *self.value.get()
1779 }
1780 }
1781}
1782
1783impl<V: Send + 'static> AsyncVariableGuard<'_, V> {
1784 pub fn finish(self) {
1786 if self.status.fetch_add(4, Ordering::Relaxed) == 3 {
1788 if let Some(waker) = unsafe { (&mut *self.waker.get()).take() } {
1789 waker.wake();
1791 }
1792 }
1793 }
1794}
1795
1796pub struct AsyncVariable<V: Send + 'static>(Arc<InnerAsyncVariable<V>>);
1800
1801unsafe impl<V: Send + 'static> Send for AsyncVariable<V> {}
1802unsafe impl<V: Send + 'static> Sync for AsyncVariable<V> {}
1803
1804impl<V: Send + 'static> Clone for AsyncVariable<V> {
1805 fn clone(&self) -> Self {
1806 AsyncVariable(self.0.clone())
1807 }
1808}
1809
1810impl<V: Send + 'static> Future for AsyncVariable<V> {
1811 type Output = V;
1812
1813 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1814 unsafe {
1815 *self.0.waker.get() = Some(cx.waker().clone()); }
1817
1818 let mut spin_len = 1;
1819 loop {
1820 match self.0.status.compare_exchange(0,
1821 1,
1822 Ordering::Acquire,
1823 Ordering::Relaxed) {
1824 Err(current) if current & 4 != 0 => {
1825 unsafe {
1827 let _ = (&mut *self.0.waker.get()).take(); return Poll::Ready((&mut *(&self).0.value.get()).take().unwrap());
1829 }
1830 },
1831 Err(_) => {
1832 spin_len = spin(spin_len);
1834 },
1835 Ok(_) => {
1836 return Poll::Pending;
1838 },
1839 }
1840 }
1841 }
1842}
1843
1844impl<V: Send + 'static> AsyncVariable<V> {
1845 pub fn new() -> Self {
1847 let inner = InnerAsyncVariable {
1848 value: UnsafeCell::new(None),
1849 waker: UnsafeCell::new(None),
1850 status: AtomicU8::new(0),
1851 };
1852
1853 AsyncVariable(Arc::new(inner))
1854 }
1855
1856 pub fn is_complete(&self) -> bool {
1858 self
1859 .0
1860 .status
1861 .load(Ordering::Acquire) & 4 != 0
1862 }
1863
1864 pub fn lock(&self) -> Option<AsyncVariableGuard<V>> {
1866 let mut spin_len = 1;
1867 loop {
1868 match self
1869 .0
1870 .status
1871 .compare_exchange(1,
1872 3,
1873 Ordering::Acquire,
1874 Ordering::Relaxed) {
1875 Err(0) => {
1876 match self
1878 .0
1879 .status
1880 .compare_exchange(0,
1881 2,
1882 Ordering::Acquire,
1883 Ordering::Relaxed) {
1884 Err(1) => {
1885 continue;
1887 },
1888 Err(2) => {
1889 spin_len = spin(spin_len);
1891 },
1892 Err(3) => {
1893 spin_len = spin(spin_len);
1895 },
1896 Err(_) => {
1897 return None;
1899 },
1900 Ok(_) => {
1901 let guard = AsyncVariableGuard {
1903 value: &self.0.value,
1904 waker: &self.0.waker,
1905 status: &self.0.status,
1906 };
1907
1908 return Some(guard)
1909 },
1910 }
1911 },
1912 Err(2) => {
1913 spin_len = spin(spin_len);
1915 },
1916 Err(3) => {
1917 spin_len = spin(spin_len);
1919 },
1920 Err(_) => {
1921 return None;
1923 }
1924 Ok(_) => {
1925 let guard = AsyncVariableGuard {
1927 value: &self.0.value,
1928 waker: &self.0.waker,
1929 status: &self.0.status,
1930 };
1931
1932 return Some(guard)
1933 },
1934 }
1935 }
1936 }
1937}
1938
1939pub struct InnerAsyncVariable<V: Send + 'static> {
1941 value: UnsafeCell<Option<V>>, waker: UnsafeCell<Option<Waker>>, status: AtomicU8, }
1945
1946pub struct AsyncWaitResult<V: Send + 'static>(pub Arc<RefCell<Option<Result<V>>>>);
1950
1951unsafe impl<V: Send + 'static> Send for AsyncWaitResult<V> {}
1952unsafe impl<V: Send + 'static> Sync for AsyncWaitResult<V> {}
1953
1954impl<V: Send + 'static> Clone for AsyncWaitResult<V> {
1955 fn clone(&self) -> Self {
1956 AsyncWaitResult(self.0.clone())
1957 }
1958}
1959
1960pub struct AsyncWaitResults<V: Send + 'static>(pub Arc<RefCell<Option<Vec<Result<V>>>>>);
1964
1965unsafe impl<V: Send + 'static> Send for AsyncWaitResults<V> {}
1966unsafe impl<V: Send + 'static> Sync for AsyncWaitResults<V> {}
1967
1968impl<V: Send + 'static> Clone for AsyncWaitResults<V> {
1969 fn clone(&self) -> Self {
1970 AsyncWaitResults(self.0.clone())
1971 }
1972}
1973
1974pub enum AsyncTimingTask<
1978 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
1979 O: Default + 'static = (),
1980> {
1981 Pended(TaskId), WaitRun(Arc<AsyncTask<P, O>>), TimeoutWake(Arc<TimeoutWaiter>), }
1985
1986pub struct AsyncTaskTimer<
1990 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
1991 O: Default + 'static = (),
1992> {
1993 producor: Sender<(usize, AsyncTimingTask<P, O>)>, consumer: Receiver<(usize, AsyncTimingTask<P, O>)>, timer: Arc<RefCell<Timer<AsyncTimingTask<P, O>, 1000, 60, 3>>>, clock: Clock, now: QInstant, }
1999
2000unsafe impl<
2001 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2002 O: Default + 'static,
2003> Send for AsyncTaskTimer<P, O> {}
2004unsafe impl<
2005 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2006 O: Default + 'static,
2007> Sync for AsyncTaskTimer<P, O> {}
2008
2009impl<
2010 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2011 O: Default + 'static,
2012> AsyncTaskTimer<P, O> {
2013 pub fn new() -> Self {
2015 let (producor, consumer) = unbounded();
2016 let clock = Clock::new();
2017 let now = clock.recent();
2018
2019 AsyncTaskTimer {
2020 producor,
2021 consumer,
2022 timer: Arc::new(RefCell::new(Timer::<AsyncTimingTask<P, O>, 1000, 60, 3>::default())),
2023 clock,
2024 now,
2025 }
2026 }
2027
2028 #[inline]
2030 pub fn get_producor(&self) -> &Sender<(usize, AsyncTimingTask<P, O>)> {
2031 &self.producor
2032 }
2033
2034 #[inline]
2036 pub fn len(&self) -> usize {
2037 let timer = self.timer.as_ref().borrow();
2038 timer.add_count() - timer.remove_count()
2039 }
2040
2041 pub fn set_timer(&self, task: AsyncTimingTask<P, O>, timeout: usize) -> usize {
2043 let current_time = self
2044 .clock
2045 .recent()
2046 .duration_since(self.now)
2047 .as_millis() as u64;
2048 self
2049 .timer
2050 .borrow_mut()
2051 .push_time(current_time + timeout as u64, task)
2052 .data()
2053 .as_ffi() as usize
2054 }
2055
2056 pub fn cancel_timer(&self, timer_ref: usize) -> Option<AsyncTimingTask<P, O>> {
2058 if let Some(item) = self
2059 .timer
2060 .borrow_mut()
2061 .cancel(KeyData::from_ffi(timer_ref as u64).into()) {
2062 Some(item)
2063 } else {
2064 None
2065 }
2066 }
2067
2068 pub fn consume(&self) -> usize {
2070 let timer_tasks = self.consumer.try_iter().collect::<Vec<(usize, AsyncTimingTask<P, O>)>>();
2071 let len = timer_tasks.len();
2072 for (timeout, task) in timer_tasks {
2073 self.set_timer(task, timeout);
2074 }
2075
2076 len
2077 }
2078
2079 pub fn is_require_pop(&self) -> Option<u64> {
2081 let current_time = self
2082 .clock
2083 .recent()
2084 .duration_since(self.now)
2085 .as_millis() as u64;
2086 if self.timer.borrow_mut().is_ok(current_time) {
2087 Some(current_time)
2088 } else {
2089 None
2090 }
2091 }
2092
2093 pub fn pop(&self, current_time: u64) -> Option<(usize, AsyncTimingTask<P, O>)> {
2095 if let Some((key, item)) = self.timer.borrow_mut().pop_kv(current_time) {
2096 Some((key.data().as_ffi() as usize, item))
2097 } else {
2098 None
2099 }
2100 }
2101}
2102
2103pub struct AsyncTaskTimerByNotCancel<
2107 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2108 O: Default + 'static = (),
2109> {
2110 producor: Sender<(usize, AsyncTimingTask<P, O>)>, consumer: Receiver<(usize, AsyncTimingTask<P, O>)>, timer: Arc<RefCell<NotCancelTimer<AsyncTimingTask<P, O>, 1000, 60, 3>>>, clock: Clock, now: QInstant, }
2116
2117unsafe impl<
2118 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2119 O: Default + 'static,
2120> Send for AsyncTaskTimerByNotCancel<P, O> {}
2121unsafe impl<
2122 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2123 O: Default + 'static,
2124> Sync for AsyncTaskTimerByNotCancel<P, O> {}
2125
2126impl<
2127 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2128 O: Default + 'static,
2129> AsyncTaskTimerByNotCancel<P, O> {
2130 pub fn new() -> Self {
2132 let (producor, consumer) = unbounded();
2133 let clock = Clock::new();
2134 let now = clock.recent();
2135
2136 AsyncTaskTimerByNotCancel {
2137 producor,
2138 consumer,
2139 timer: Arc::new(RefCell::new(NotCancelTimer::<AsyncTimingTask<P, O>, 1000, 60, 3>::default())),
2140 clock,
2141 now,
2142 }
2143 }
2144
2145 #[inline]
2147 pub fn get_producor(&self) -> &Sender<(usize, AsyncTimingTask<P, O>)> {
2148 &self.producor
2149 }
2150
2151 #[inline]
2153 pub fn len(&self) -> usize {
2154 let timer = self.timer.as_ref().borrow();
2155 timer.add_count() - timer.remove_count()
2156 }
2157
2158 pub fn set_timer(&self, task: AsyncTimingTask<P, O>, timeout: usize) {
2160 self
2161 .timer
2162 .borrow_mut()
2163 .push(timeout, task);
2164 }
2165
2166 pub fn consume(&self) -> usize {
2168 let timer_tasks = self.consumer.try_iter().collect::<Vec<(usize, AsyncTimingTask<P, O>)>>();
2169 let len = timer_tasks.len();
2170 for (timeout, task) in timer_tasks {
2171 self.set_timer(task, timeout);
2172 }
2173
2174 len
2175 }
2176
2177 pub fn is_require_pop(&self) -> Option<u64> {
2179 let current_time = self
2180 .clock
2181 .recent()
2182 .duration_since(self.now)
2183 .as_millis() as u64;
2184 if self.timer.borrow_mut().is_ok(current_time) {
2185 Some(current_time)
2186 } else {
2187 None
2188 }
2189 }
2190
2191 pub fn pop(&self, current_time: u64) -> Option<AsyncTimingTask<P, O>> {
2193 if let Some(item) = self.timer.borrow_mut().pop(current_time) {
2194 Some(item)
2195 } else {
2196 None
2197 }
2198 }
2199}
2200
2201pub struct AsyncWaitTimeout<
2205 RT: AsyncRuntime<O>,
2206 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2207 O: Default + 'static = (),
2208> {
2209 rt: RT, producor: Sender<(usize, AsyncTimingTask<P, O>)>, timeout: usize, registered: AtomicBool, waiter: Arc<TimeoutWaiter>, }
2215
2216unsafe impl<
2217 RT: AsyncRuntime<O>,
2218 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2219 O: Default + 'static,
2220> Send for AsyncWaitTimeout<RT, P, O> {}
2221unsafe impl<
2222 RT: AsyncRuntime<O>,
2223 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2224 O: Default + 'static,
2225> Sync for AsyncWaitTimeout<RT, P, O> {}
2226
2227impl<
2228 RT: AsyncRuntime<O>,
2229 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2230 O: Default + 'static,
2231> Future for AsyncWaitTimeout<RT, P, O> {
2232 type Output = ();
2233
2234 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2235 if self.waiter.is_fired() {
2236 return Poll::Ready(());
2238 }
2239
2240 self.waiter.register(cx.waker());
2241
2242 if !self.registered.swap(true, Ordering::AcqRel) {
2243 let _ = self
2245 .producor
2246 .send((self.timeout, AsyncTimingTask::TimeoutWake(self.waiter.clone())));
2247 }
2248
2249 if self.waiter.is_fired() {
2250 Poll::Ready(())
2251 } else {
2252 Poll::Pending
2253 }
2254 }
2255}
2256
2257impl<
2258 RT: AsyncRuntime<O>,
2259 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2260 O: Default + 'static,
2261> Drop for AsyncWaitTimeout<RT, P, O> {
2262 fn drop(&mut self) {
2263 self.waiter.clear_waker();
2264 }
2265}
2266
2267impl<
2268 RT: AsyncRuntime<O>,
2269 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2270 O: Default + 'static,
2271> AsyncWaitTimeout<RT, P, O> {
2272 pub fn new(rt: RT,
2274 producor: Sender<(usize, AsyncTimingTask<P, O>)>,
2275 timeout: usize) -> Self {
2276 AsyncWaitTimeout {
2277 rt,
2278 producor,
2279 timeout,
2280 registered: AtomicBool::new(false), waiter: Arc::new(TimeoutWaiter::new()),
2282 }
2283 }
2284}
2285
2286pub struct LocalAsyncWaitTimeout<
2290 RT: AsyncRuntime<O>,
2291 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2292 O: Default + 'static = (),
2293> {
2294 rt: RT, timer: Arc<AsyncTaskTimerByNotCancel<P, O>>, timeout: usize, registered: AtomicBool, waiter: Arc<TimeoutWaiter>, }
2300
2301unsafe impl<
2302 RT: AsyncRuntime<O>,
2303 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2304 O: Default + 'static,
2305> Send for LocalAsyncWaitTimeout<RT, P, O> {}
2306unsafe impl<
2307 RT: AsyncRuntime<O>,
2308 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2309 O: Default + 'static,
2310> Sync for LocalAsyncWaitTimeout<RT, P, O> {}
2311
2312impl<
2313 RT: AsyncRuntime<O>,
2314 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2315 O: Default + 'static,
2316> Future for LocalAsyncWaitTimeout<RT, P, O> {
2317 type Output = ();
2318
2319 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2320 if self.waiter.is_fired() {
2321 return Poll::Ready(());
2323 }
2324
2325 self.waiter.register(cx.waker());
2326
2327 if !self.registered.swap(true, Ordering::AcqRel) {
2328 self
2330 .timer
2331 .set_timer(AsyncTimingTask::TimeoutWake(self.waiter.clone()),
2332 self.timeout);
2333 }
2334
2335 if self.waiter.is_fired() {
2336 Poll::Ready(())
2337 } else {
2338 Poll::Pending
2339 }
2340 }
2341}
2342
2343impl<
2344 RT: AsyncRuntime<O>,
2345 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2346 O: Default + 'static,
2347> Drop for LocalAsyncWaitTimeout<RT, P, O> {
2348 fn drop(&mut self) {
2349 self.waiter.clear_waker();
2350 }
2351}
2352
2353impl<
2354 RT: AsyncRuntime<O>,
2355 P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>,
2356 O: Default + 'static,
2357> LocalAsyncWaitTimeout<RT, P, O> {
2358 pub fn new(rt: RT,
2360 timer: Arc<AsyncTaskTimerByNotCancel<P, O>>,
2361 timeout: usize) -> Self {
2362 LocalAsyncWaitTimeout {
2363 rt,
2364 timer,
2365 timeout,
2366 registered: AtomicBool::new(false), waiter: Arc::new(TimeoutWaiter::new()),
2368 }
2369 }
2370}
2371
2372pub struct AsyncWait<V: Send + 'static>(AsyncWaitAny<V>);
2376
2377unsafe impl<V: Send + 'static> Send for AsyncWait<V> {}
2378unsafe impl<V: Send + 'static> Sync for AsyncWait<V> {}
2379
2380impl<V: Send + 'static> AsyncWait<V> {
2384 pub fn spawn<RT, O, F>(&self,
2386 rt: RT,
2387 timeout: Option<usize>,
2388 future: F) -> Result<()>
2389 where RT: AsyncRuntime<O>,
2390 O: Default + 'static,
2391 F: Future<Output = Result<V>> + Send + 'static {
2392 self.0.spawn(rt.clone(), future)?;
2393
2394 if let Some(timeout) = timeout {
2395 let rt_copy = rt.clone();
2397 self.0.spawn(rt, async move {
2398 rt_copy.timeout(timeout).await;
2399
2400 Err(Error::new(ErrorKind::TimedOut, format!("Time out")))
2402 })
2403 } else {
2404 Ok(())
2406 }
2407 }
2408
2409 pub fn spawn_local<O, F>(&self,
2411 timeout: Option<usize>,
2412 future: F) -> Result<()>
2413 where O: Default + 'static,
2414 F: Future<Output = Result<V>> + Send + 'static {
2415 if let Some(rt) = local_async_runtime::<O>() {
2416 self.0.spawn_local(future)?;
2418
2419 if let Some(timeout) = timeout {
2420 let rt_copy = rt.clone();
2422 self.0.spawn_local(async move {
2423 rt_copy.timeout(timeout).await;
2424
2425 Err(Error::new(ErrorKind::TimedOut, format!("Time out")))
2427 })
2428 } else {
2429 Ok(())
2431 }
2432 } else {
2433 Err(Error::new(ErrorKind::Other, format!("Spawn wait task failed, reason: local async runtime not exist")))
2435 }
2436 }
2437}
2438
2439impl<V: Send + 'static> AsyncWait<V> {
2443 pub async fn wait_result(self) -> Result<V> {
2445 self.0.wait_result().await
2446 }
2447}
2448
2449pub struct AsyncWaitAny<V: Send + 'static> {
2453 capacity: usize, producor: AsyncSender<Result<V>>, consumer: AsyncReceiver<Result<V>>, }
2457
2458unsafe impl<V: Send + 'static> Send for AsyncWaitAny<V> {}
2459unsafe impl<V: Send + 'static> Sync for AsyncWaitAny<V> {}
2460
2461impl<V: Send + 'static> AsyncWaitAny<V> {
2465 pub fn spawn<RT, O, F>(&self,
2467 rt: RT,
2468 future: F) -> Result<()>
2469 where RT: AsyncRuntime<O>,
2470 O: Default + 'static,
2471 F: Future<Output = Result<V>> + Send + 'static {
2472 let producor = self.producor.clone();
2473 rt.spawn_by_id(rt.alloc::<O>(), async move {
2474 let value = future.await;
2475 producor.into_send_async(value).await;
2476
2477 Default::default()
2479 })
2480 }
2481
2482 pub fn spawn_local<F>(&self,
2484 future: F) -> Result<()>
2485 where F: Future<Output = Result<V>> + Send + 'static {
2486 if let Some(rt) = local_async_runtime() {
2487 let producor = self.producor.clone();
2489 rt.spawn(async move {
2490 let value = future.await;
2491 producor.into_send_async(value).await;
2492 })
2493 } else {
2494 Err(Error::new(ErrorKind::Other, format!("Spawn wait any task failed, reason: local async runtime not exist")))
2496 }
2497 }
2498}
2499
2500impl<V: Send + 'static> AsyncWaitAny<V> {
2504 pub async fn wait_result(self) -> Result<V> {
2506 match self.consumer.recv_async().await {
2507 Err(e) => {
2508 Err(Error::new(ErrorKind::Other, format!("Wait any result failed, reason: {:?}", e)))
2510 },
2511 Ok(result) => {
2512 result
2514 },
2515 }
2516 }
2517}
2518
2519pub struct AsyncWaitAnyCallback<V: Send + 'static> {
2523 capacity: usize, producor: AsyncSender<Result<V>>, consumer: AsyncReceiver<Result<V>>, }
2527
2528unsafe impl<V: Send + 'static> Send for AsyncWaitAnyCallback<V> {}
2529unsafe impl<V: Send + 'static> Sync for AsyncWaitAnyCallback<V> {}
2530
2531impl<V: Send + 'static> AsyncWaitAnyCallback<V> {
2535 pub fn spawn<RT, O, F>(&self,
2537 rt: RT,
2538 future: F) -> Result<()>
2539 where RT: AsyncRuntime<O>,
2540 O: Default + 'static,
2541 F: Future<Output = Result<V>> + Send + 'static {
2542 let producor = self.producor.clone();
2543 rt.spawn_by_id(rt.alloc::<O>(), async move {
2544 let value = future.await;
2545 producor.into_send_async(value).await;
2546
2547 Default::default()
2549 })
2550 }
2551
2552 pub fn spawn_local<F>(&self,
2554 future: F) -> Result<()>
2555 where F: Future<Output = Result<V>> + Send + 'static {
2556 if let Some(rt) = local_async_runtime() {
2557 let producor = self.producor.clone();
2559 rt.spawn(async move {
2560 let value = future.await;
2561 producor.into_send_async(value).await;
2562 })
2563 } else {
2564 Err(Error::new(ErrorKind::Other, format!("Spawn wait any task failed by callback, reason: current async runtime not exist")))
2566 }
2567 }
2568}
2569
2570impl<V: Send + 'static> AsyncWaitAnyCallback<V> {
2574 pub async fn wait_result(mut self,
2576 callback: impl Fn(&Result<V>) -> bool + Send + Sync + 'static) -> Result<V> {
2577 let checker = create_checker(self.capacity, callback);
2578 loop {
2579 match self.consumer.recv_async().await {
2580 Err(e) => {
2581 return Err(Error::new(ErrorKind::Other, format!("Wait any result failed by callback, reason: {:?}", e)));
2583 },
2584 Ok(result) => {
2585 if checker(&result) {
2587 return result;
2589 }
2590 },
2591 }
2592 }
2593 }
2594}
2595
2596fn create_checker<V, F>(len: usize,
2598 callback: F) -> Arc<dyn Fn(&Result<V>) -> bool + Send + Sync + 'static>
2599 where V: Send + 'static,
2600 F: Fn(&Result<V>) -> bool + Send + Sync + 'static {
2601 let mut check_counter = AtomicUsize::new(len); Arc::new(move |result| {
2603 if check_counter.fetch_sub(1, Ordering::SeqCst) == 1 {
2604 true
2606 } else {
2607 callback(result)
2609 }
2610 })
2611}
2612
2613pub struct AsyncMapReduce<V: Send + 'static> {
2617 count: usize, capacity: usize, producor: AsyncSender<(usize, Result<V>)>, consumer: AsyncReceiver<(usize, Result<V>)>, }
2622
2623unsafe impl<V: Send + 'static> Send for AsyncMapReduce<V> {}
2624
2625impl<V: Send + 'static> AsyncMapReduce<V> {
2629 pub fn map<RT, O, F>(&mut self, rt: RT, future: F) -> Result<usize>
2631 where RT: AsyncRuntime<O>,
2632 O: Default + 'static,
2633 F: Future<Output = Result<V>> + Send + 'static {
2634 if self.count >= self.capacity {
2635 return Err(Error::new(ErrorKind::Other, format!("Map task to runtime failed, capacity: {}, reason: out of capacity", self.capacity)));
2637 }
2638
2639 let index = self.count;
2640 let producor = self.producor.clone();
2641 rt.spawn_by_id(rt.alloc::<O>(), async move {
2642 let value = future.await;
2643 producor.into_send_async((index, value)).await;
2644
2645 Default::default()
2647 })?;
2648
2649 self.count += 1; Ok(index)
2651 }
2652}
2653
2654impl<V: Send + 'static> AsyncMapReduce<V> {
2658 pub async fn reduce(self, order: bool) -> Result<Vec<Result<V>>> {
2660 let mut count = self.count;
2661 let mut results = Vec::with_capacity(count);
2662 while count > 0 {
2663 match self.consumer.recv_async().await {
2664 Err(e) => {
2665 return Err(Error::new(ErrorKind::Other, format!("Reduce result failed, reason: {:?}", e)));
2667 },
2668 Ok((index, result)) => {
2669 results.push((index, result));
2671 count -= 1;
2672 },
2673 }
2674 }
2675
2676 if order {
2677 results.sort_by_key(|(key, _value)| {
2679 key.clone()
2680 });
2681 }
2682 let (_, values) = results
2683 .into_iter()
2684 .unzip::<usize, Result<V>, Vec<usize>, Vec<Result<V>>>();
2685
2686 Ok(values)
2687 }
2688}
2689
2690pub enum AsyncPipelineResult<O: 'static> {
2694 Disconnect, Filtered(O), }
2697
2698pub fn spawn_worker_thread<F0, F1>(thread_name: &str,
2704 thread_stack_size: usize,
2705 thread_handler: Arc<AtomicBool>,
2706 thread_waker: Arc<(AtomicBool, Mutex<()>, Condvar)>, sleep_timeout: u64, loop_interval: Option<u64>, loop_func: F0,
2710 get_queue_len: F1) -> Arc<AtomicBool>
2711 where F0: Fn() -> (bool, Duration) + Send + 'static,
2712 F1: Fn() -> usize + Send + 'static {
2713 let thread_status_copy = thread_handler.clone();
2714
2715 thread::Builder::new()
2716 .name(thread_name.to_string())
2717 .stack_size(thread_stack_size).spawn(move || {
2718 let mut sleep_count = 0;
2719
2720 while thread_handler.load(Ordering::Relaxed) {
2721 let (is_no_task, run_time) = loop_func();
2722
2723 if is_no_task {
2724 if sleep_count > 1 {
2726 sleep_count = 0; let (is_sleep, lock, condvar) = &*thread_waker;
2729 if get_queue_len() > 0 {
2730 continue;
2732 }
2733
2734 {
2735 let _locked = lock.lock();
2736 if !is_sleep.load(Ordering::Acquire) {
2737 is_sleep.store(true, Ordering::Release);
2739 }
2740 }
2741
2742 if get_queue_len() > 0 {
2743 is_sleep.store(false, Ordering::Release);
2745 continue;
2746 }
2747
2748 let mut locked = lock.lock();
2749 if is_sleep.load(Ordering::Acquire) {
2750 let _ = condvar.wait_for(
2751 &mut locked,
2752 Duration::from_millis(sleep_timeout),
2753 );
2754 }
2755 is_sleep.store(false, Ordering::Release);
2756
2757 continue; }
2759
2760 sleep_count += 1; if let Some(interval) = &loop_interval {
2762 if let Some(remaining_interval) = Duration::from_millis(*interval).checked_sub(run_time){
2764 thread::sleep(remaining_interval);
2766 }
2767 }
2768 } else {
2769 sleep_count = 0; if let Some(interval) = &loop_interval {
2772 if let Some(remaining_interval) = Duration::from_millis(*interval).checked_sub(run_time){
2774 thread::sleep(remaining_interval);
2776 }
2777 }
2778 }
2779 }
2780 });
2781
2782 thread_status_copy
2783}
2784
2785pub fn wakeup_worker_thread<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>>(worker_waker: &Arc<(AtomicBool, Mutex<()>, Condvar)>, rt: &SingleTaskRuntime<O, P>) {
2787 if worker_waker.0.load(Ordering::Relaxed) && rt.len() > 0 {
2789 let _ = wake_thread_waker(worker_waker);
2790 }
2791}
2792
2793pub fn register_global_panic_handler<Handler>(handler: Handler)
2795 where Handler: Fn(thread::Thread, String, Option<String>, Option<(String, u32, u32)>) -> Option<i32> + Send + Sync + 'static {
2796 set_hook(Box::new(move |panic_info| {
2797 let thread_info = thread::current();
2798
2799 let payload = panic_info.payload();
2800 let payload_info = match payload.downcast_ref::<&str>() {
2801 None => {
2802 match payload.downcast_ref::<String>() {
2804 None => {
2805 "Unknow panic".to_string()
2807 },
2808 Some(info) => {
2809 info.clone()
2810 }
2811 }
2812 },
2813 Some(info) => {
2814 info.to_string()
2815 }
2816 };
2817
2818 let other_info = if let Some(arg) = panic_info.payload_as_str() {
2819 Some(arg.to_string())
2820 } else {
2821 None
2822 };
2823
2824 let location = if let Some(location) = panic_info.location() {
2825 Some((location.file().to_string(), location.line(), location.column()))
2826 } else {
2827 None
2828 };
2829
2830 if let Some(exit_code) = handler(thread_info, payload_info, other_info, location) {
2831 std::process::exit(exit_code);
2833 }
2834 }));
2835}
2836
2837pub fn replace_global_alloc_error_handler() {
2839 set_alloc_error_hook(global_alloc_error_handle);
2840}
2841
2842fn global_alloc_error_handle(layout: Layout) {
2843 let bt = Backtrace::new();
2844 eprintln!("[UTC: {}][Thread: {}]Global memory allocation of {:?} bytes failed, stacktrace: \n{:?}",
2845 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis(),
2846 thread::current().name().unwrap_or(""),
2847 layout.size(),
2848 bt);
2849}
2850
2851pub(crate) struct YieldNow(bool);
2853
2854impl Future for YieldNow {
2855 type Output = ();
2856
2857 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2858 if self.0 {
2859 Poll::Ready(())
2860 } else {
2861 self.0 = true;
2862 cx.waker().wake_by_ref();
2863 Poll::Pending
2864 }
2865 }
2866}