@@ -61,18 +61,14 @@ pub use async_task::Task;
61
61
/// }));
62
62
/// ```
63
63
#[ derive( Debug ) ]
64
- pub struct Executor < ' a > {
64
+ pub struct Executor {
65
65
state : once_cell:: sync:: OnceCell < Arc < State > > ,
66
- _marker : PhantomData < std:: cell:: UnsafeCell < & ' a ( ) > > ,
67
66
}
68
67
69
- unsafe impl Send for Executor < ' _ > { }
70
- unsafe impl Sync for Executor < ' _ > { }
68
+ impl UnwindSafe for Executor { }
69
+ impl RefUnwindSafe for Executor { }
71
70
72
- impl UnwindSafe for Executor < ' _ > { }
73
- impl RefUnwindSafe for Executor < ' _ > { }
74
-
75
- impl < ' a > Executor < ' a > {
71
+ impl Executor {
76
72
/// Creates a new executor.
77
73
///
78
74
/// # Examples
@@ -82,10 +78,9 @@ impl<'a> Executor<'a> {
82
78
///
83
79
/// let ex = Executor::new();
84
80
/// ```
85
- pub const fn new ( ) -> Executor < ' a > {
81
+ pub const fn new ( ) -> Executor {
86
82
Executor {
87
83
state : once_cell:: sync:: OnceCell :: new ( ) ,
88
- _marker : PhantomData ,
89
84
}
90
85
}
91
86
@@ -102,8 +97,22 @@ impl<'a> Executor<'a> {
102
97
/// println!("Hello world");
103
98
/// });
104
99
/// ```
105
- pub fn spawn < T : Send + ' a > ( & self , future : impl Future < Output = T > + Send + ' a ) -> Task < T > {
106
- unsafe { self . spawn_unchecked ( future) }
100
+ pub fn spawn < T : Send + ' static > (
101
+ & self ,
102
+ future : impl Future < Output = T > + Send + ' static ,
103
+ ) -> Task < T > {
104
+ let mut active = self . state ( ) . active . lock ( ) . unwrap ( ) ;
105
+ let index = active. next_vacant ( ) ;
106
+ let state = self . state ( ) . clone ( ) ;
107
+ let future = async move {
108
+ let _guard = CallOnDrop ( move || drop ( state. active . lock ( ) . unwrap ( ) . remove ( index) ) ) ;
109
+ future. await
110
+ } ;
111
+
112
+ let ( runnable, task) = async_task:: spawn ( future, self . schedule ( ) ) ;
113
+ active. insert ( runnable. waker ( ) ) ;
114
+ runnable. schedule ( ) ;
115
+ task
107
116
}
108
117
109
118
/// Attempts to run a task if at least one is scheduled.
@@ -211,24 +220,9 @@ impl<'a> Executor<'a> {
211
220
fn state ( & self ) -> & Arc < State > {
212
221
self . state . get_or_init ( || Arc :: new ( State :: new ( ) ) )
213
222
}
214
-
215
- unsafe fn spawn_unchecked < T > ( & self , future : impl Future < Output = T > ) -> Task < T > {
216
- let mut active = self . state ( ) . active . lock ( ) . unwrap ( ) ;
217
- let index = active. next_vacant ( ) ;
218
- let state = self . state ( ) . clone ( ) ;
219
- let future = async move {
220
- let _guard = CallOnDrop ( move || drop ( state. active . lock ( ) . unwrap ( ) . remove ( index) ) ) ;
221
- future. await
222
- } ;
223
-
224
- let ( runnable, task) = async_task:: spawn_unchecked ( future, self . schedule ( ) ) ;
225
- active. insert ( runnable. waker ( ) ) ;
226
- runnable. schedule ( ) ;
227
- task
228
- }
229
223
}
230
224
231
- impl Drop for Executor < ' _ > {
225
+ impl Drop for Executor {
232
226
fn drop ( & mut self ) {
233
227
if let Some ( state) = self . state . get ( ) {
234
228
let mut active = state. active . lock ( ) . unwrap ( ) ;
@@ -244,8 +238,8 @@ impl Drop for Executor<'_> {
244
238
}
245
239
}
246
240
247
- impl < ' a > Default for Executor < ' a > {
248
- fn default ( ) -> Executor < ' a > {
241
+ impl Default for Executor {
242
+ fn default ( ) -> Executor {
249
243
Executor :: new ( )
250
244
}
251
245
}
@@ -267,18 +261,18 @@ impl<'a> Default for Executor<'a> {
267
261
/// }));
268
262
/// ```
269
263
#[ derive( Debug ) ]
270
- pub struct LocalExecutor < ' a > {
264
+ pub struct LocalExecutor {
271
265
/// The inner executor.
272
- inner : once_cell:: unsync:: OnceCell < Executor < ' a > > ,
266
+ inner : once_cell:: unsync:: OnceCell < Executor > ,
273
267
274
268
/// Make sure the type is `!Send` and `!Sync`.
275
269
_marker : PhantomData < Rc < ( ) > > ,
276
270
}
277
271
278
- impl UnwindSafe for LocalExecutor < ' _ > { }
279
- impl RefUnwindSafe for LocalExecutor < ' _ > { }
272
+ impl UnwindSafe for LocalExecutor { }
273
+ impl RefUnwindSafe for LocalExecutor { }
280
274
281
- impl < ' a > LocalExecutor < ' a > {
275
+ impl LocalExecutor {
282
276
/// Creates a single-threaded executor.
283
277
///
284
278
/// # Examples
@@ -288,7 +282,7 @@ impl<'a> LocalExecutor<'a> {
288
282
///
289
283
/// let local_ex = LocalExecutor::new();
290
284
/// ```
291
- pub const fn new ( ) -> LocalExecutor < ' a > {
285
+ pub const fn new ( ) -> LocalExecutor {
292
286
LocalExecutor {
293
287
inner : once_cell:: unsync:: OnceCell :: new ( ) ,
294
288
_marker : PhantomData ,
@@ -308,8 +302,19 @@ impl<'a> LocalExecutor<'a> {
308
302
/// println!("Hello world");
309
303
/// });
310
304
/// ```
311
- pub fn spawn < T : ' a > ( & self , future : impl Future < Output = T > + ' a ) -> Task < T > {
312
- unsafe { self . inner ( ) . spawn_unchecked ( future) }
305
+ pub fn spawn < T : ' static > ( & self , future : impl Future < Output = T > + ' static ) -> Task < T > {
306
+ let mut active = self . inner ( ) . state ( ) . active . lock ( ) . unwrap ( ) ;
307
+ let index = active. next_vacant ( ) ;
308
+ let state = self . inner ( ) . state ( ) . clone ( ) ;
309
+ let future = async move {
310
+ let _guard = CallOnDrop ( move || drop ( state. active . lock ( ) . unwrap ( ) . remove ( index) ) ) ;
311
+ future. await
312
+ } ;
313
+
314
+ let ( runnable, task) = async_task:: spawn_local ( future, self . schedule ( ) ) ;
315
+ active. insert ( runnable. waker ( ) ) ;
316
+ runnable. schedule ( ) ;
317
+ task
313
318
}
314
319
315
320
/// Attempts to run a task if at least one is scheduled.
@@ -375,14 +380,24 @@ impl<'a> LocalExecutor<'a> {
375
380
self . inner ( ) . run ( future) . await
376
381
}
377
382
383
+ /// Returns a function that schedules a runnable task when it gets woken up.
384
+ fn schedule ( & self ) -> impl Fn ( Runnable ) + Send + Sync + ' static {
385
+ let state = self . inner ( ) . state ( ) . clone ( ) ;
386
+
387
+ move |runnable| {
388
+ state. queue . push ( runnable) . unwrap ( ) ;
389
+ state. notify ( ) ;
390
+ }
391
+ }
392
+
378
393
/// Returns a reference to the inner executor.
379
- fn inner ( & self ) -> & Executor < ' a > {
394
+ fn inner ( & self ) -> & Executor {
380
395
self . inner . get_or_init ( || Executor :: new ( ) )
381
396
}
382
397
}
383
398
384
- impl < ' a > Default for LocalExecutor < ' a > {
385
- fn default ( ) -> LocalExecutor < ' a > {
399
+ impl Default for LocalExecutor {
400
+ fn default ( ) -> LocalExecutor {
386
401
LocalExecutor :: new ( )
387
402
}
388
403
}
@@ -402,6 +417,7 @@ struct State {
402
417
/// A list of sleeping tickers.
403
418
sleepers : Mutex < Sleepers > ,
404
419
420
+ /// Currently active tasks.
405
421
active : Mutex < Arena < Waker > > ,
406
422
}
407
423
0 commit comments