11use alloc:: { boxed:: Box , format, string:: String , vec:: Vec } ;
22use core:: { future:: Future , marker:: PhantomData , mem, panic:: AssertUnwindSafe } ;
3- use std:: thread:: { self , JoinHandle } ;
3+ use std:: { sync :: OnceLock , thread:: { self , JoinHandle } } ;
44
55use crate :: { bevy_executor:: Executor , Metadata } ;
66use async_task:: FallibleTask ;
@@ -12,6 +12,9 @@ use crate::{block_on, Task};
1212
1313pub use crate :: bevy_executor:: ThreadSpawner ;
1414
15+ static EXECUTOR : Executor = Executor :: new ( ) ;
16+ static TASK_POOL : OnceLock < TaskPool > = OnceLock :: new ( ) ;
17+
1518struct CallOnDrop ( Option < Arc < dyn Fn ( ) + Send + Sync + ' static > > ) ;
1619
1720impl Drop for CallOnDrop {
@@ -148,9 +151,16 @@ impl TaskPool {
148151 self . executor . current_thread_spawner ( )
149152 }
150153
151- /// Create a `TaskPool` with the default configuration.
152- pub fn new ( ) -> Self {
153- TaskPoolBuilder :: new ( ) . build ( )
154+ pub fn try_get ( ) -> Option < & ' static TaskPool > {
155+ TASK_POOL . get ( )
156+ }
157+
158+ pub fn get ( ) -> & ' static TaskPool {
159+ Self :: get_or_init ( Default :: default)
160+ }
161+
162+ pub fn get_or_init ( f : impl FnOnce ( ) -> TaskPoolBuilder ) -> & ' static TaskPool {
163+ TASK_POOL . get_or_init ( || f ( ) . build_static ( & EXECUTOR ) )
154164 }
155165
156166 fn new_internal ( builder : TaskPoolBuilder , executor : & ' static Executor ) -> Self {
@@ -421,12 +431,6 @@ impl TaskPool {
421431 }
422432}
423433
424- impl Default for TaskPool {
425- fn default ( ) -> Self {
426- Self :: new ( )
427- }
428- }
429-
430434impl Drop for TaskPool {
431435 fn drop ( & mut self ) {
432436 self . shutdown_tx . close ( ) ;
@@ -553,7 +557,7 @@ mod tests {
553557
554558 #[ test]
555559 fn test_spawn ( ) {
556- let pool = TaskPool :: new ( ) ;
560+ let pool = TaskPool :: get ( ) ;
557561
558562 let foo = Box :: new ( 42 ) ;
559563 let foo = & * foo;
@@ -636,7 +640,7 @@ mod tests {
636640
637641 #[ test]
638642 fn test_mixed_spawn_on_scope_and_spawn ( ) {
639- let pool = TaskPool :: new ( ) ;
643+ let pool = TaskPool :: get ( ) ;
640644
641645 let foo = Box :: new ( 42 ) ;
642646 let foo = & * foo;
@@ -681,7 +685,7 @@ mod tests {
681685
682686 #[ test]
683687 fn test_thread_locality ( ) {
684- let pool = Arc :: new ( TaskPool :: new ( ) ) ;
688+ let pool = TaskPool :: get ( ) ;
685689 let count = Arc :: new ( AtomicI32 :: new ( 0 ) ) ;
686690 let barrier = Arc :: new ( Barrier :: new ( 101 ) ) ;
687691 let thread_check_failed = Arc :: new ( AtomicBool :: new ( false ) ) ;
@@ -718,7 +722,7 @@ mod tests {
718722
719723 #[ test]
720724 fn test_nested_spawn ( ) {
721- let pool = TaskPool :: new ( ) ;
725+ let pool = TaskPool :: get ( ) ;
722726
723727 let foo = Box :: new ( 42 ) ;
724728 let foo = & * foo;
@@ -756,7 +760,7 @@ mod tests {
756760
757761 #[ test]
758762 fn test_nested_locality ( ) {
759- let pool = Arc :: new ( TaskPool :: new ( ) ) ;
763+ let pool = TaskPool :: get ( ) ;
760764 let count = Arc :: new ( AtomicI32 :: new ( 0 ) ) ;
761765 let barrier = Arc :: new ( Barrier :: new ( 101 ) ) ;
762766 let thread_check_failed = Arc :: new ( AtomicBool :: new ( false ) ) ;
@@ -795,7 +799,7 @@ mod tests {
795799 // This test will often freeze on other executors.
796800 #[ test]
797801 fn test_nested_scopes ( ) {
798- let pool = TaskPool :: new ( ) ;
802+ let pool = TaskPool :: get ( ) ;
799803 let count = Arc :: new ( AtomicI32 :: new ( 0 ) ) ;
800804
801805 pool. scope ( |scope| {
0 commit comments