14
14
15
15
use std:: backtrace:: Backtrace ;
16
16
use std:: future:: Future ;
17
+ use std:: panic:: Location ;
17
18
use std:: sync:: Arc ;
18
- use std:: thread;
19
19
use std:: time:: Duration ;
20
20
use std:: time:: Instant ;
21
21
@@ -32,6 +32,8 @@ use tokio::task::JoinHandle;
32
32
33
33
use crate :: runtime:: catch_unwind:: CatchUnwindFuture ;
34
34
use crate :: runtime:: MemStat ;
35
+ use crate :: runtime:: Thread ;
36
+ use crate :: runtime:: ThreadJoinHandle ;
35
37
36
38
/// Methods to spawn tasks.
37
39
pub trait TrySpawn {
@@ -101,7 +103,7 @@ impl Runtime {
101
103
let handle = runtime. handle ( ) . clone ( ) ;
102
104
103
105
// Block the runtime to shutdown.
104
- let join_handler = thread :: spawn ( move || {
106
+ let join_handler = Thread :: spawn ( move || {
105
107
// We ignore channel is closed.
106
108
let _ = runtime. block_on ( recv_stop) ;
107
109
@@ -201,10 +203,16 @@ impl Runtime {
201
203
self . handle . clone ( )
202
204
}
203
205
206
+ #[ track_caller]
204
207
pub fn block_on < T , F > ( & self , future : F ) -> F :: Output
205
208
where F : Future < Output = Result < T > > + Send + ' static {
206
209
let future = CatchUnwindFuture :: create ( future) ;
207
- self . handle . block_on ( future) . flatten ( )
210
+ #[ allow( clippy:: disallowed_methods) ]
211
+ tokio:: task:: block_in_place ( || {
212
+ self . handle
213
+ . block_on ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
214
+ . flatten ( )
215
+ } )
208
216
}
209
217
210
218
// For each future of `futures`, before being executed
@@ -260,6 +268,7 @@ impl Runtime {
260
268
let permit = semaphore. acquire_owned ( ) . await . map_err ( |e| {
261
269
ErrorCode :: Internal ( format ! ( "semaphore closed, acquire permit failure. {}" , e) )
262
270
} ) ?;
271
+ #[ expect( clippy:: disallowed_methods) ]
263
272
let handler = self
264
273
. handle
265
274
. spawn ( async_backtrace:: location!( ) . frame ( async move {
@@ -280,6 +289,7 @@ impl Runtime {
280
289
F : FnOnce ( ) -> Result < R > + Send + ' static ,
281
290
R : Send + ' static ,
282
291
{
292
+ #[ allow( clippy:: disallowed_methods) ]
283
293
match_join_handle ( self . handle . spawn_blocking ( f) ) . await
284
294
}
285
295
}
@@ -298,6 +308,7 @@ impl TrySpawn for Runtime {
298
308
async_backtrace:: location!( format!( "Running query {} spawn task" , id) ) . frame ( task)
299
309
}
300
310
} ;
311
+ #[ expect( clippy:: disallowed_methods) ]
301
312
Ok ( self . handle . spawn ( task) )
302
313
}
303
314
}
@@ -306,7 +317,7 @@ impl TrySpawn for Runtime {
306
317
pub struct Dropper {
307
318
name : Option < String > ,
308
319
close : Option < oneshot:: Sender < ( ) > > ,
309
- join_handler : Option < thread :: JoinHandle < bool > > ,
320
+ join_handler : Option < ThreadJoinHandle < bool > > ,
310
321
}
311
322
312
323
impl Drop for Dropper {
@@ -389,6 +400,70 @@ pub fn spawn<F>(future: F) -> tokio::task::JoinHandle<F::Output>
389
400
where
390
401
F : Future + Send + ' static ,
391
402
F :: Output : Send + ' static ,
403
+ {
404
+ #[ expect( clippy:: disallowed_methods) ]
405
+ tokio:: spawn ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
406
+ }
407
+
408
+ #[ track_caller]
409
+ pub fn spawn_local < F > ( future : F ) -> tokio:: task:: JoinHandle < F :: Output >
410
+ where
411
+ F : Future + Send + ' static ,
412
+ F :: Output : Send + ' static ,
413
+ {
414
+ #[ expect( clippy:: disallowed_methods) ]
415
+ tokio:: task:: spawn_local ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
416
+ }
417
+
418
+ #[ track_caller]
419
+ pub fn spawn_blocking < F , R > ( f : F ) -> tokio:: task:: JoinHandle < R >
420
+ where
421
+ F : FnOnce ( ) -> R + Send + ' static ,
422
+ R : Send + ' static ,
423
+ {
424
+ #[ expect( clippy:: disallowed_methods) ]
425
+ tokio:: runtime:: Handle :: current ( ) . spawn_blocking ( f)
426
+ }
427
+
428
+ #[ track_caller]
429
+ pub fn try_spawn_blocking < F , R > ( f : F ) -> Result < tokio:: task:: JoinHandle < R > , F >
430
+ where
431
+ F : FnOnce ( ) -> R + Send + ' static ,
432
+ R : Send + ' static ,
433
+ {
434
+ match tokio:: runtime:: Handle :: try_current ( ) {
435
+ Err ( _) => Err ( f) ,
436
+ #[ expect( clippy:: disallowed_methods) ]
437
+ Ok ( handler) => Ok ( handler. spawn_blocking ( f) ) ,
438
+ }
439
+ }
440
+
441
+ #[ track_caller]
442
+ pub fn block_on < F : Future > ( future : F ) -> F :: Output {
443
+ #[ expect( clippy:: disallowed_methods) ]
444
+ tokio:: task:: block_in_place ( || {
445
+ tokio:: runtime:: Handle :: current ( )
446
+ . block_on ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
447
+ } )
448
+ }
449
+
450
+ #[ track_caller]
451
+ pub fn try_block_on < F : Future > ( future : F ) -> Result < F :: Output , F > {
452
+ match tokio:: runtime:: Handle :: try_current ( ) {
453
+ Err ( _) => Err ( future) ,
454
+ #[ expect( clippy:: disallowed_methods) ]
455
+ Ok ( handler) => Ok ( tokio:: task:: block_in_place ( || {
456
+ handler. block_on ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
457
+ } ) ) ,
458
+ }
459
+ }
460
+
461
+ fn location_future < F : Future > (
462
+ future : F ,
463
+ frame_location : & ' static Location ,
464
+ ) -> impl Future < Output = F :: Output >
465
+ where
466
+ F : Future ,
392
467
{
393
468
// NOTE:
394
469
// Frame name: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=689fbc84ab4be894c0cdd285bea24845
@@ -397,16 +472,12 @@ where
397
472
let frame_name = std:: any:: type_name :: < F > ( )
398
473
. trim_end_matches ( "::{{closure}}" )
399
474
. to_string ( ) ;
400
- let frame_location = std:: panic:: Location :: caller ( ) ;
401
475
402
- #[ expect( clippy:: disallowed_methods) ]
403
- tokio:: spawn (
404
- async_backtrace:: location!(
405
- frame_name,
406
- frame_location. file( ) ,
407
- frame_location. line( ) ,
408
- frame_location. column( )
409
- )
410
- . frame ( future) ,
476
+ async_backtrace:: location!(
477
+ frame_name,
478
+ frame_location. file( ) ,
479
+ frame_location. line( ) ,
480
+ frame_location. column( )
411
481
)
482
+ . frame ( future)
412
483
}
0 commit comments