Skip to content

Commit fd39b91

Browse files
committed
Fix
1 parent a8c457f commit fd39b91

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Thread.NativeAot.Windows.cs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace System.Threading
1515
public sealed partial class Thread
1616
{
1717
[ThreadStatic]
18-
private static ApartmentType t_apartmentType;
18+
private static sbyte t_apartmentState; // ApartmentState shifted by ApartmentState.Unknonw to represent Unknown as the default value
1919

2020
[ThreadStatic]
2121
private static ComState t_comState;
@@ -334,14 +334,15 @@ public ApartmentState GetApartmentState()
334334
return _initialApartmentState;
335335
}
336336

337-
switch (GetCurrentApartmentType())
337+
switch (GetCurrentApartmentState())
338338
{
339-
case ApartmentType.STA:
339+
case ApartmentState.STA:
340340
return ApartmentState.STA;
341-
case ApartmentType.MTA:
341+
case ApartmentState.MTA:
342342
return ApartmentState.MTA;
343343
default:
344-
return ApartmentState.Unknown;
344+
// If COM is uninitialized on the current thread, it is assumed to be implicit MTA.
345+
return ApartmentState.MTA;
345346
}
346347
}
347348

@@ -374,14 +375,15 @@ private bool SetApartmentStateUnchecked(ApartmentState state, bool throwOnError)
374375
}
375376
else
376377
{
378+
// Compat: Setting ApartmentState to Unknown uninitializes COM
377379
UninitializeCom();
378380
}
379381
}
380382

381383
// Clear the cache and check whether new state matches the desired state
382-
t_apartmentType = ApartmentType.Unknown;
384+
t_apartmentState = 0;
383385

384-
retState = GetApartmentState();
386+
retState = GetCurrentApartmentState();
385387
}
386388

387389
if (retState != state)
@@ -527,49 +529,53 @@ internal static void CheckForPendingInterrupt()
527529
}
528530

529531
internal static bool ReentrantWaitsEnabled =>
530-
GetCurrentApartmentType() == ApartmentType.STA;
532+
GetCurrentApartmentState() == ApartmentState.STA;
531533

532-
internal static ApartmentType GetCurrentApartmentType()
534+
internal static ApartmentState GetCurrentApartmentState()
533535
{
534-
ApartmentType currentThreadType = t_apartmentType;
535-
if (currentThreadType != ApartmentType.Unknown)
536-
return currentThreadType;
536+
sbyte current = t_apartmentState;
537+
if (current != 0)
538+
return (ApartmentState)(current + (sbyte)ApartmentState.Unknown);
537539

538540
Interop.APTTYPE aptType;
539541
Interop.APTTYPEQUALIFIER aptTypeQualifier;
540542
int result = Interop.Ole32.CoGetApartmentType(out aptType, out aptTypeQualifier);
541543

542-
ApartmentType type = ApartmentType.Unknown;
544+
ApartmentState state = ApartmentState.Unknown;
543545

544546
switch (result)
545547
{
546548
case HResults.CO_E_NOTINITIALIZED:
547-
type = ApartmentType.None;
549+
Debug.Fail("COM is not initialized");
550+
state = ApartmentState.Unknown;
548551
break;
549552

550553
case HResults.S_OK:
551554
switch (aptType)
552555
{
553556
case Interop.APTTYPE.APTTYPE_STA:
554557
case Interop.APTTYPE.APTTYPE_MAINSTA:
555-
type = ApartmentType.STA;
558+
state = ApartmentState.STA;
556559
break;
557560

558561
case Interop.APTTYPE.APTTYPE_MTA:
559-
type = ApartmentType.MTA;
562+
state = ApartmentState.MTA;
560563
break;
561564

562565
case Interop.APTTYPE.APTTYPE_NA:
563566
switch (aptTypeQualifier)
564567
{
565568
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_MTA:
569+
state = ApartmentState.MTA;
570+
break;
571+
566572
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_IMPLICIT_MTA:
567-
type = ApartmentType.MTA;
573+
state = ApartmentState.Unknown;
568574
break;
569575

570576
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_STA:
571577
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_MAINSTA:
572-
type = ApartmentType.STA;
578+
state = ApartmentState.STA;
573579
break;
574580

575581
default:
@@ -581,21 +587,13 @@ internal static ApartmentType GetCurrentApartmentType()
581587
break;
582588

583589
default:
584-
Debug.Fail("bad return from CoGetApartmentType");
590+
Debug.Fail("bad return from CoGetApartmentState");
585591
break;
586592
}
587593

588-
if (type != ApartmentType.Unknown)
589-
t_apartmentType = type;
590-
return type;
591-
}
592-
593-
internal enum ApartmentType : byte
594-
{
595-
Unknown = 0,
596-
None,
597-
STA,
598-
MTA
594+
if (state != ApartmentState.Unknown)
595+
t_apartmentState = (sbyte)(state - ApartmentState.Unknown);
596+
return state;
599597
}
600598

601599
[Flags]

0 commit comments

Comments
 (0)