File tree 1 file changed +13
-3
lines changed
1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -698,6 +698,9 @@ where
698
698
}
699
699
700
700
thread_local ! {
701
+ // Invariant: `CURRENT` and `CURRENT_ID` will always be initialized
702
+ // together. However, while `CURRENT_ID` will be available during
703
+ // TLS constructors, `CURRENT` will not.
701
704
static CURRENT : OnceCell <Thread > = const { OnceCell :: new( ) } ;
702
705
static CURRENT_ID : Cell <Option <ThreadId >> = const { Cell :: new( None ) } ;
703
706
}
@@ -722,9 +725,13 @@ pub(crate) fn set_current(thread: Thread) {
722
725
pub ( crate ) fn try_current ( ) -> Option < Thread > {
723
726
CURRENT
724
727
. try_with ( |current| {
725
- let thread = current. get_or_init ( Thread :: new_unnamed) . clone ( ) ;
726
- CURRENT_ID . set ( Some ( thread. id ( ) ) ) ;
727
- thread
728
+ current
729
+ . get_or_init ( || {
730
+ let thread = Thread :: new_unnamed ( ) ;
731
+ CURRENT_ID . set ( Some ( thread. id ( ) ) ) ;
732
+ thread
733
+ } )
734
+ . clone ( )
728
735
} )
729
736
. ok ( )
730
737
}
@@ -736,6 +743,9 @@ pub(crate) fn try_current() -> Option<Thread> {
736
743
#[ inline]
737
744
pub ( crate ) fn try_current_id ( ) -> Option < ThreadId > {
738
745
if CURRENT_ID . get ( ) . is_none ( ) {
746
+ // If `CURRENT_ID` isn't initialized yet, then `CURRENT` must also not be initialized.
747
+ // `try_current()` will try to initialize both `CURRENT` and `CURRENT_ID`.
748
+ // Subsequent calls to `try_current_id` will then no longer enter this if-branch.
739
749
let _ = try_current ( ) ;
740
750
}
741
751
CURRENT_ID . get ( )
You can’t perform that action at this time.
0 commit comments