@@ -99,13 +99,20 @@ public bool HasException()
9999 }
100100
101101 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
102- public unsafe ExecutionContext ? GetExecutionContext ( )
102+ public unsafe bool TryGetExecutionContext ( out ExecutionContext ? execContext )
103103 {
104104 const uint mask = ( 1u << ( int ) ContinuationFlags . ExecutionContextIndexNumBits ) - 1 ;
105105 uint index = ( ( uint ) Flags >> ( int ) ContinuationFlags . ExecutionContextIndexFirstBit ) & mask ;
106+ if ( index == 0 )
107+ {
108+ execContext = null ;
109+ return false ;
110+ }
111+
106112 Debug . Assert ( index != 0 ) ;
107113 ref byte data = ref RuntimeHelpers . GetRawData ( this ) ;
108- return Unsafe . As < byte , ExecutionContext ? > ( ref Unsafe . Add ( ref data , ( DataOffset - PointerSize ) + index * PointerSize ) ) ;
114+ execContext = Unsafe . As < byte , ExecutionContext ? > ( ref Unsafe . Add ( ref data , ( DataOffset - PointerSize ) + index * PointerSize ) ) ;
115+ return true ;
109116 }
110117
111118 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
@@ -128,16 +135,6 @@ public ref byte GetResultStorageOrNull()
128135 ref byte data = ref RuntimeHelpers . GetRawData ( this ) ;
129136 return ref Unsafe . Add ( ref data , ( DataOffset - PointerSize ) + index * PointerSize ) ;
130137 }
131-
132- protected void EncodeFieldOffsetInFlags ( ref byte field , ContinuationFlags firstBit , ContinuationFlags numBits )
133- {
134- int offset = ( int ) Unsafe . ByteOffset ( ref RuntimeHelpers . GetRawData ( this ) , ref field ) ;
135- offset -= DataOffset ;
136- Debug . Assert ( offset >= 0 && offset % PointerSize == 0 ) ;
137- uint index = 1 + ( uint ) offset / PointerSize ;
138- Debug . Assert ( index < ( 1 << ( int ) numBits ) ) ;
139- Flags |= ( ContinuationFlags ) ( ( uint ) index << ( int ) firstBit ) ;
140- }
141138 }
142139
143140 [ StructLayout ( LayoutKind . Explicit ) ]
@@ -373,7 +370,6 @@ private static unsafe void TransparentAwaitValueTask(ValueTask valueTask)
373370
374371 Debug . Assert ( valueTask . _obj != null ) ;
375372 vtsCont . Initialize ( valueTask . _obj , valueTask . _token ) ;
376- vtsCont . ExecutionContext = ExecutionContext . CaptureForSuspension ( state . CurrentThread ! ) ;
377373
378374 sentinelContinuation . Next = vtsCont ;
379375 state . StackState ->ValueTaskContinuation = vtsCont ;
@@ -401,7 +397,6 @@ private static unsafe void TransparentAwaitValueTaskOfT<T>(ValueTask<T?> valueTa
401397
402398 Debug . Assert ( valueTask . _obj != null ) ;
403399 vtsCont . Initialize < T > ( valueTask . _obj , valueTask . _token ) ;
404- vtsCont . ExecutionContext = ExecutionContext . CaptureForSuspension ( state . CurrentThread ! ) ;
405400
406401 sentinelContinuation . Next = vtsCont ;
407402 state . StackState ->ValueTaskContinuation = vtsCont ;
@@ -660,7 +655,11 @@ private unsafe void DispatchContinuations()
660655 asyncDispatcherInfo . NextContinuation = nextContinuation ;
661656
662657 Debug . Assert ( awaitState . CurrentThread != null ) ;
663- RestoreExecutionContext ( awaitState . CurrentThread , curContinuation . GetExecutionContext ( ) ) ;
658+ if ( curContinuation . TryGetExecutionContext ( out ExecutionContext ? execContext ) )
659+ {
660+ RestoreExecutionContext ( awaitState . CurrentThread , execContext ) ;
661+ }
662+
664663 ref byte resultLoc = ref nextContinuation != null ? ref nextContinuation . GetResultStorageOrNull ( ) : ref GetResultStorage ( ) ;
665664
666665 Continuation ? newContinuation = curContinuation . ResumeInfo ->Resume ( curContinuation , ref resultLoc ) ;
@@ -766,7 +765,11 @@ private unsafe void InstrumentedDispatchContinuations(AsyncInstrumentation.Flags
766765 asyncDispatcherInfo . NextContinuation = nextContinuation ;
767766
768767 Debug . Assert ( awaitState . CurrentThread != null ) ;
769- RestoreExecutionContext ( awaitState . CurrentThread , curContinuation . GetExecutionContext ( ) ) ;
768+ if ( curContinuation . TryGetExecutionContext ( out ExecutionContext ? execContext ) )
769+ {
770+ RestoreExecutionContext ( awaitState . CurrentThread , execContext ) ;
771+ }
772+
770773 ref byte resultLoc = ref nextContinuation != null ? ref nextContinuation . GetResultStorageOrNull ( ) : ref GetResultStorage ( ) ;
771774
772775 RuntimeAsyncInstrumentationHelpers . ResumeRuntimeAsyncMethod ( ref asyncDispatcherInfo , flags , curContinuation ) ;
0 commit comments