1
- use alloc:: sync:: Arc ;
2
1
use core:: { future:: Future , marker:: PhantomData , mem, panic:: AssertUnwindSafe } ;
3
2
use std:: thread:: { self , JoinHandle } ;
4
3
5
4
use crate :: executor:: FallibleTask ;
6
5
use concurrent_queue:: ConcurrentQueue ;
7
6
use futures_lite:: FutureExt ;
8
7
8
+ #[ cfg( feature = "portable-atomic" ) ]
9
+ use { alloc:: boxed:: Box , portable_atomic_util:: Arc } ;
10
+
11
+ #[ cfg( not( feature = "portable-atomic" ) ) ]
12
+ use alloc:: sync:: Arc ;
13
+
9
14
use crate :: {
10
15
block_on,
11
16
thread_executor:: { ThreadExecutor , ThreadExecutorTicker } ,
@@ -70,7 +75,17 @@ impl TaskPoolBuilder {
70
75
/// This is called on the thread itself and has access to all thread-local storage.
71
76
/// This will block running async tasks on the thread until the callback completes.
72
77
pub fn on_thread_spawn ( mut self , f : impl Fn ( ) + Send + Sync + ' static ) -> Self {
73
- self . on_thread_spawn = Some ( Arc :: new ( f) ) ;
78
+ #[ cfg( feature = "portable-atomic" ) ]
79
+ let arc = {
80
+ let boxed = Box :: new ( f) ;
81
+ let boxed: Box < dyn Fn ( ) + Send + Sync + ' static > = boxed;
82
+ Arc :: from ( boxed)
83
+ } ;
84
+
85
+ #[ cfg( not( feature = "portable-atomic" ) ) ]
86
+ let arc = Arc :: new ( f) ;
87
+
88
+ self . on_thread_spawn = Some ( arc) ;
74
89
self
75
90
}
76
91
@@ -79,7 +94,17 @@ impl TaskPoolBuilder {
79
94
/// This is called on the thread itself and has access to all thread-local storage.
80
95
/// This will block thread termination until the callback completes.
81
96
pub fn on_thread_destroy ( mut self , f : impl Fn ( ) + Send + Sync + ' static ) -> Self {
82
- self . on_thread_destroy = Some ( Arc :: new ( f) ) ;
97
+ #[ cfg( feature = "portable-atomic" ) ]
98
+ let arc = {
99
+ let boxed = Box :: new ( f) ;
100
+ let boxed: Box < dyn Fn ( ) + Send + Sync + ' static > = boxed;
101
+ Arc :: from ( boxed)
102
+ } ;
103
+
104
+ #[ cfg( not( feature = "portable-atomic" ) ) ]
105
+ let arc = Arc :: new ( f) ;
106
+
107
+ self . on_thread_destroy = Some ( arc) ;
83
108
self
84
109
}
85
110
0 commit comments