@@ -9,6 +9,7 @@ use crate::broadcast::BroadcastContext;
99use crate :: job:: { ArcJob , HeapJob , JobFifo , JobRef } ;
1010use crate :: latch:: { CountLatch , CountLockLatch , Latch } ;
1111use crate :: registry:: { global_registry, in_worker, Registry , WorkerThread } ;
12+ use crate :: tlv:: { self , Tlv } ;
1213use crate :: unwind;
1314use std:: any:: Any ;
1415use std:: fmt;
@@ -76,6 +77,9 @@ struct ScopeBase<'scope> {
7677 /// `Sync`, but it's still safe to let the `Scope` implement `Sync` because
7778 /// the closures are only *moved* across threads to be executed.
7879 marker : PhantomData < Box < dyn FnOnce ( & Scope < ' scope > ) + Send + Sync + ' scope > > ,
80+
81+ /// The TLV at the scope's creation. Used to set the TLV for spawned jobs.
82+ tlv : Tlv ,
7983}
8084
8185/// Creates a "fork-join" scope `s` and invokes the closure with a
@@ -540,7 +544,7 @@ impl<'scope> Scope<'scope> {
540544 BODY : FnOnce ( & Scope < ' scope > ) + Send + ' scope ,
541545 {
542546 let scope_ptr = ScopePtr ( self ) ;
543- let job = HeapJob :: new ( move || unsafe {
547+ let job = HeapJob :: new ( self . base . tlv , move || unsafe {
544548 // SAFETY: this job will execute before the scope ends.
545549 let scope = scope_ptr. as_ref ( ) ;
546550 ScopeBase :: execute_job ( & scope. base , move || body ( scope) )
@@ -600,7 +604,7 @@ impl<'scope> ScopeFifo<'scope> {
600604 BODY : FnOnce ( & ScopeFifo < ' scope > ) + Send + ' scope ,
601605 {
602606 let scope_ptr = ScopePtr ( self ) ;
603- let job = HeapJob :: new ( move || unsafe {
607+ let job = HeapJob :: new ( self . base . tlv , move || unsafe {
604608 // SAFETY: this job will execute before the scope ends.
605609 let scope = scope_ptr. as_ref ( ) ;
606610 ScopeBase :: execute_job ( & scope. base , move || body ( scope) )
@@ -652,6 +656,7 @@ impl<'scope> ScopeBase<'scope> {
652656 panic : AtomicPtr :: new ( ptr:: null_mut ( ) ) ,
653657 job_completed_latch : ScopeLatch :: new ( owner) ,
654658 marker : PhantomData ,
659+ tlv : tlv:: get ( ) ,
655660 }
656661 }
657662
@@ -690,6 +695,10 @@ impl<'scope> ScopeBase<'scope> {
690695 {
691696 let result = unsafe { Self :: execute_job_closure ( self , func) } ;
692697 self . job_completed_latch . wait ( owner) ;
698+
699+ // Restore the TLV if we ran some jobs while waiting
700+ tlv:: set ( self . tlv ) ;
701+
693702 self . maybe_propagate_panic ( ) ;
694703 result. unwrap ( ) // only None if `op` panicked, and that would have been propagated
695704 }
@@ -749,6 +758,10 @@ impl<'scope> ScopeBase<'scope> {
749758 let panic = self . panic . swap ( ptr:: null_mut ( ) , Ordering :: Relaxed ) ;
750759 if !panic. is_null ( ) {
751760 let value = unsafe { Box :: from_raw ( panic) } ;
761+
762+ // Restore the TLV if we ran some jobs while waiting
763+ tlv:: set ( self . tlv ) ;
764+
752765 unwind:: resume_unwinding ( * value) ;
753766 }
754767 }
0 commit comments