Skip to content

Commit 7a9a850

Browse files
committed
Fix
1 parent a8c457f commit 7a9a850

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

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

Lines changed: 24 additions & 27 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 ApartmentState t_apartmentState;
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

@@ -379,9 +380,9 @@ private bool SetApartmentStateUnchecked(ApartmentState state, bool throwOnError)
379380
}
380381

381382
// Clear the cache and check whether new state matches the desired state
382-
t_apartmentType = ApartmentType.Unknown;
383+
t_apartmentState = ApartmentState.Unknown;
383384

384-
retState = GetApartmentState();
385+
retState = GetCurrentApartmentState();
385386
}
386387

387388
if (retState != state)
@@ -527,49 +528,53 @@ internal static void CheckForPendingInterrupt()
527528
}
528529

529530
internal static bool ReentrantWaitsEnabled =>
530-
GetCurrentApartmentType() == ApartmentType.STA;
531+
GetCurrentApartmentState() == ApartmentState.STA;
531532

532-
internal static ApartmentType GetCurrentApartmentType()
533+
internal static ApartmentState GetCurrentApartmentState()
533534
{
534-
ApartmentType currentThreadType = t_apartmentType;
535-
if (currentThreadType != ApartmentType.Unknown)
535+
ApartmentState currentThreadType = t_apartmentState;
536+
if (currentThreadType != ApartmentState.Unknown)
536537
return currentThreadType;
537538

538539
Interop.APTTYPE aptType;
539540
Interop.APTTYPEQUALIFIER aptTypeQualifier;
540541
int result = Interop.Ole32.CoGetApartmentType(out aptType, out aptTypeQualifier);
541542

542-
ApartmentType type = ApartmentType.Unknown;
543+
ApartmentState type = ApartmentState.Unknown;
543544

544545
switch (result)
545546
{
546547
case HResults.CO_E_NOTINITIALIZED:
547-
type = ApartmentType.None;
548+
Debug.Fail("COM is not initialized");
549+
type = ApartmentState.Unknown;
548550
break;
549551

550552
case HResults.S_OK:
551553
switch (aptType)
552554
{
553555
case Interop.APTTYPE.APTTYPE_STA:
554556
case Interop.APTTYPE.APTTYPE_MAINSTA:
555-
type = ApartmentType.STA;
557+
type = ApartmentState.STA;
556558
break;
557559

558560
case Interop.APTTYPE.APTTYPE_MTA:
559-
type = ApartmentType.MTA;
561+
type = ApartmentState.MTA;
560562
break;
561563

562564
case Interop.APTTYPE.APTTYPE_NA:
563565
switch (aptTypeQualifier)
564566
{
565567
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_MTA:
568+
type = ApartmentState.MTA;
569+
break;
570+
566571
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_IMPLICIT_MTA:
567-
type = ApartmentType.MTA;
572+
type = ApartmentState.Unknown;
568573
break;
569574

570575
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_STA:
571576
case Interop.APTTYPEQUALIFIER.APTTYPEQUALIFIER_NA_ON_MAINSTA:
572-
type = ApartmentType.STA;
577+
type = ApartmentState.STA;
573578
break;
574579

575580
default:
@@ -585,19 +590,11 @@ internal static ApartmentType GetCurrentApartmentType()
585590
break;
586591
}
587592

588-
if (type != ApartmentType.Unknown)
589-
t_apartmentType = type;
593+
if (type != ApartmentState.Unknown)
594+
t_apartmentState = type;
590595
return type;
591596
}
592597

593-
internal enum ApartmentType : byte
594-
{
595-
Unknown = 0,
596-
None,
597-
STA,
598-
MTA
599-
}
600-
601598
[Flags]
602599
internal enum ComState : byte
603600
{

0 commit comments

Comments
 (0)