@@ -39,25 +39,9 @@ public sealed partial class Thread
3939 internal WaitSubsystem . ThreadWaitInfo ? _waitInfo ;
4040#endif
4141
42- /*=========================================================================
43- ** The base implementation of Thread is all native. The following fields
44- ** should never be used in the C# code. They are here to define the proper
45- ** space so the thread object may be allocated. DON'T CHANGE THESE UNLESS
46- ** YOU MODIFY ThreadBaseObject in vm\object.h
47- =========================================================================*/
48- #pragma warning disable CA1823 , 169 // These fields are not used from managed.
49- // IntPtrs need to be together, and before ints, because IntPtrs are 64-bit
50- // fields on 64-bit platforms, where they will be sorted together.
51-
52- private IntPtr _DONT_USE_InternalThread ; // Pointer
53- private int _priority ; // INT32
54-
55- // The following field is required for interop with the VS Debugger
56- // Prior to making any changes to this field, please reach out to the VS Debugger
57- // team to make sure that your changes are not going to prevent the debugger
58- // from working.
59- private int _managedThreadId ; // INT32
60- #pragma warning restore CA1823 , 169
42+ private IntPtr _DONT_USE_InternalThread ;
43+ private int _priority ;
44+ private int _managedThreadId ; // Debugger depends on the exact name of this field.
6145
6246 // This is used for a quick check on thread pool threads after running a work item to determine if the name, background
6347 // state, or priority were changed by the work item, and if so to reset it. Other threads may also change some of those,
@@ -315,30 +299,22 @@ public ThreadState ThreadState
315299 [ LibraryImport ( RuntimeHelpers . QCall , EntryPoint = "ThreadNative_GetThreadState" ) ]
316300 private static partial int GetThreadState ( ThreadHandle t ) ;
317301
318- internal void SetWaitSleepJoinState ( )
302+ internal unsafe void SetWaitSleepJoinState ( )
319303 {
320304 // This method is called when the thread is about to enter a wait, sleep, or join state.
321305 // It sets the state in the native layer to indicate that the thread is waiting.
322- SetWaitSleepJoinState ( GetNativeHandle ( ) ) ;
323- GC . KeepAlive ( this ) ;
306+ NativeThread * nativeThread = GetNativeThreadForCurrentThread ( ) ;
307+ Interlocked . Or ( ref nativeThread -> m_State , NativeThread . ThreadState . TS_WaitSleepJoin ) ;
324308 }
325309
326- internal void ClearWaitSleepJoinState ( )
310+ internal unsafe void ClearWaitSleepJoinState ( )
327311 {
328312 // This method is called when the thread is no longer in a wait, sleep, or join state.
329313 // It clears the state in the native layer to indicate that the thread is no longer waiting.
330- ClearWaitSleepJoinState ( GetNativeHandle ( ) ) ;
331- GC . KeepAlive ( this ) ;
314+ NativeThread * nativeThread = GetNativeThreadForCurrentThread ( ) ;
315+ Interlocked . And ( ref nativeThread -> m_State , ~ NativeThread . ThreadState . TS_WaitSleepJoin ) ;
332316 }
333317
334- [ LibraryImport ( RuntimeHelpers . QCall , EntryPoint = "ThreadNative_SetWaitSleepJoinState" ) ]
335- [ SuppressGCTransition ]
336- private static partial void SetWaitSleepJoinState ( ThreadHandle t ) ;
337-
338- [ LibraryImport ( RuntimeHelpers . QCall , EntryPoint = "ThreadNative_ClearWaitSleepJoinState" ) ]
339- [ SuppressGCTransition ]
340- private static partial void ClearWaitSleepJoinState ( ThreadHandle t ) ;
341-
342318 /// <summary>
343319 /// An unstarted thread can be marked to indicate that it will host a
344320 /// single-threaded or multi-threaded apartment.
@@ -611,30 +587,24 @@ private static unsafe void OnThreadExited(Thread* pThread, Exception* pException
611587 [ LibraryImport ( RuntimeHelpers . QCall , EntryPoint = "ThreadNative_ReentrantWaitAny" ) ]
612588 internal static unsafe partial int ReentrantWaitAny ( [ MarshalAs ( UnmanagedType . Bool ) ] bool alertable , int timeout , int count , IntPtr * handles ) ;
613589
614- internal static void CheckForPendingInterrupt ( )
615- {
616- CheckForPendingInterrupt ( CurrentThread . GetNativeHandle ( ) ) ;
617- GC . KeepAlive ( CurrentThread ) ;
618- }
619-
620590 [ LibraryImport ( RuntimeHelpers . QCall , EntryPoint = "ThreadNative_CheckForPendingInterrupt" ) ]
621- private static partial void CheckForPendingInterrupt ( ThreadHandle t ) ;
591+ internal static partial void CheckForPendingInterrupt ( ) ;
622592
623- [ StructLayout ( LayoutKind . Sequential ) ]
624- private struct NativeThreadClass
593+ private unsafe NativeThread * GetNativeThreadForCurrentThread ( )
625594 {
626- public NativeThreadState m_State ;
595+ Debug . Assert ( this == CurrentThread ) ;
596+ return ( NativeThread * ) _DONT_USE_InternalThread ;
627597 }
628598
629- private enum NativeThreadState
599+ [ StructLayout ( LayoutKind . Sequential ) ]
600+ private struct NativeThread
630601 {
631- None = 0 ,
632- TS_AbortRequested = 0x00000001 , // Abort the thread
633- TS_DebugSuspendPending = 0x00000008 , // Is the debugger suspending threads?
634- TS_GCOnTransitions = 0x00000010 , // Force a GC on stub transitions (GCStress only)
602+ public ThreadState m_State ;
635603
636- // We require (and assert) that the following bits are less than 0x100.
637- TS_CatchAtSafePoint = ( TS_AbortRequested | TS_DebugSuspendPending | TS_GCOnTransitions ) ,
638- } ;
604+ internal enum ThreadState
605+ {
606+ TS_WaitSleepJoin = 0x02000000 , // Thread is waiting, sleeping or joining
607+ }
608+ }
639609 }
640610}
0 commit comments