Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite568f75

Browse files
authored
Add an internal mode toLock to have it use non-alertable waits (#97227)
* Add an internal mode to `Lock` to have it use trivial (non-alertable) waits- Added an internal constructor that enables the lock to use trivial waits- Trivial waits are non-alertable waits that are not forwarded to `SynchronizationContext` wait overrides, are non-message-pumping waits, and are not interruptible- Updated most of the uses of `Lock` in NativeAOT to use trivial waits- Also simplified the fix in#94873 to avoid having to do the relevant null checks in various places along the wait path, by limiting the scope of the null checks to the initialization phaseFixes#97105
1 parent013ddc0 commite568f75

File tree

24 files changed

+113
-65
lines changed

24 files changed

+113
-65
lines changed

‎src/coreclr/System.Private.CoreLib/src/System/Threading/WaitHandle.CoreCLR.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Threading
99
publicabstractpartialclassWaitHandle
1010
{
1111
[MethodImpl(MethodImplOptions.InternalCall)]
12-
privatestaticexternintWaitOneCore(IntPtrwaitHandle,intmillisecondsTimeout);
12+
privatestaticexternintWaitOneCore(IntPtrwaitHandle,intmillisecondsTimeout,booluseTrivialWaits);
1313

1414
privatestaticunsafeintWaitMultipleIgnoringSyncContextCore(Span<IntPtr>waitHandles,boolwaitAll,intmillisecondsTimeout)
1515
{

‎src/coreclr/nativeaot/Common/src/System/Collections/Concurrent/ConcurrentUnifier.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal abstract class ConcurrentUnifier<K, V>
6565
{
6666
protectedConcurrentUnifier()
6767
{
68-
_lock=newLock();
68+
_lock=newLock(useTrivialWaits:true);
6969
_container=newContainer(this);
7070
}
7171

‎src/coreclr/nativeaot/Common/src/System/Collections/Concurrent/ConcurrentUnifierW.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ internal abstract class ConcurrentUnifierW<K, V>
7575
{
7676
protectedConcurrentUnifierW()
7777
{
78-
_lock=newLock();
78+
_lock=newLock(useTrivialWaits:true);
7979
_container=newContainer(this);
8080
}
8181

‎src/coreclr/nativeaot/Common/src/System/Collections/Concurrent/ConcurrentUnifierWKeyed.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal abstract class ConcurrentUnifierWKeyed<K, V>
8484
{
8585
protectedConcurrentUnifierWKeyed()
8686
{
87-
_lock=newLock();
87+
_lock=newLock(useTrivialWaits:true);
8888
_container=newContainer(this);
8989
}
9090

‎src/coreclr/nativeaot/System.Private.CoreLib/src/CompatibilitySuppressions.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,10 @@
913913
<DiagnosticId>CP0002</DiagnosticId>
914914
<Target>M:System.Reflection.MethodBase.GetParametersAsSpan</Target>
915915
</Suppression>
916+
<Suppression>
917+
<DiagnosticId>CP0002</DiagnosticId>
918+
<Target>M:System.Threading.Lock.#ctor(System.Boolean)</Target>
919+
</Suppression>
916920
<Suppression>
917921
<DiagnosticId>CP0015</DiagnosticId>
918922
<Target>M:System.Diagnostics.Tracing.EventSource.Write``1(System.String,``0):[T:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute]</Target>

‎src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/FrozenObjectHeapManager.cs‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal unsafe partial class FrozenObjectHeapManager
1616
{
1717
publicstaticreadonlyFrozenObjectHeapManagerInstance=newFrozenObjectHeapManager();
1818

19-
privatereadonlyLowLevelLockm_Crst=newLowLevelLock();
19+
privatereadonlyLockm_Crst=newLock(useTrivialWaits:true);
2020
privateFrozenObjectSegmentm_CurrentSegment;
2121

2222
// Default size to reserve for a frozen segment
@@ -34,9 +34,7 @@ internal unsafe partial class FrozenObjectHeapManager
3434
{
3535
HalfBakedObject*obj=null;
3636

37-
m_Crst.Acquire();
38-
39-
try
37+
using(m_Crst.EnterScope())
4038
{
4139
Debug.Assert(type!=null);
4240
// _ASSERT(FOH_COMMIT_SIZE >= MIN_OBJECT_SIZE);
@@ -84,10 +82,6 @@ internal unsafe partial class FrozenObjectHeapManager
8482
Debug.Assert(obj!=null);
8583
}
8684
}// end of m_Crst lock
87-
finally
88-
{
89-
m_Crst.Release();
90-
}
9185

9286
IntPtrresult=(IntPtr)obj;
9387

‎src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/ClassConstructorRunner.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public static CctorHandle GetCctor(StaticClassConstructionContext* pContext)
275275
#ifTARGET_WASM
276276
if(s_cctorGlobalLock==null)
277277
{
278-
Interlocked.CompareExchange(refs_cctorGlobalLock,newLock(),null);
278+
Interlocked.CompareExchange(refs_cctorGlobalLock,newLock(useTrivialWaits:true),null);
279279
}
280280
if(s_cctorArrays==null)
281281
{
@@ -342,7 +342,7 @@ public static CctorHandle GetCctor(StaticClassConstructionContext* pContext)
342342

343343
Debug.Assert(resultArray[resultIndex]._pContext==default(StaticClassConstructionContext*));
344344
resultArray[resultIndex]._pContext=pContext;
345-
resultArray[resultIndex].Lock=newLock();
345+
resultArray[resultIndex].Lock=newLock(useTrivialWaits:true);
346346
s_count++;
347347
}
348348

@@ -489,7 +489,7 @@ public static CctorHandle GetCctorThatThreadIsBlockedOn(int managedThreadId)
489489
internalstaticvoidInitialize()
490490
{
491491
s_cctorArrays=newCctor[10][];
492-
s_cctorGlobalLock=newLock();
492+
s_cctorGlobalLock=newLock(useTrivialWaits:true);
493493
}
494494

495495
[Conditional("ENABLE_NOISY_CCTOR_LOG")]

‎src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract partial class ComWrappers
4444
privatestaticreadonlyList<GCHandle>s_referenceTrackerNativeObjectWrapperCache=newList<GCHandle>();
4545

4646
privatereadonlyConditionalWeakTable<object,ManagedObjectWrapperHolder>_ccwTable=newConditionalWeakTable<object,ManagedObjectWrapperHolder>();
47-
privatereadonlyLock_lock=newLock();
47+
privatereadonlyLock_lock=newLock(useTrivialWaits:true);
4848
privatereadonlyDictionary<IntPtr,GCHandle>_rcwCache=newDictionary<IntPtr,GCHandle>();
4949

5050
internalstaticboolTryGetComInstanceForIID(objectobj,Guidiid,outIntPtrunknown,outlongwrapperId)

‎src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Condition.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public unsafe bool Wait(int millisecondsTimeout, object? associatedObjectForMoni
114114
success=
115115
waiter.ev.WaitOneNoCheck(
116116
millisecondsTimeout,
117+
false,// useTrivialWaits
117118
associatedObjectForMonitorWait,
118119
associatedObjectForMonitorWait!=null
119120
?NativeRuntimeEventSource.WaitHandleWaitSourceMap.MonitorWait

‎src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Lock.NativeAot.cs‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ internal void Reenter(uint previousRecursionCount)
9292
_recursionCount=previousRecursionCount;
9393
}
9494

95+
privatestaticboolIsFullyInitialized
96+
{
97+
get
98+
{
99+
// If NativeRuntimeEventSource is already being class-constructed by this thread earlier in the stack, Log can
100+
// be null. This property is used to avoid going down the wait path in that case to avoid null checks in several
101+
// other places.
102+
Debug.Assert((StaticsInitializationStage)s_staticsInitializationStage==StaticsInitializationStage.Complete);
103+
returnNativeRuntimeEventSource.Log!=null;
104+
}
105+
}
106+
95107
[MethodImpl(MethodImplOptions.AggressiveInlining)]
96108
privateTryLockResultLazyInitializeOrEnter()
97109
{
@@ -101,6 +113,10 @@ private TryLockResult LazyInitializeOrEnter()
101113
caseStaticsInitializationStage.Complete:
102114
if(_spinCount==SpinCountNotInitialized)
103115
{
116+
if(!IsFullyInitialized)
117+
{
118+
gotocaseStaticsInitializationStage.Started;
119+
}
104120
_spinCount=s_maxSpinCount;
105121
}
106122
returnTryLockResult.Spin;
@@ -121,7 +137,7 @@ private TryLockResult LazyInitializeOrEnter()
121137
}
122138

123139
stage=(StaticsInitializationStage)Volatile.Read(refs_staticsInitializationStage);
124-
if(stage==StaticsInitializationStage.Complete)
140+
if(stage==StaticsInitializationStage.Complete&&IsFullyInitialized)
125141
{
126142
gotocaseStaticsInitializationStage.Complete;
127143
}
@@ -166,14 +182,17 @@ private static bool TryInitializeStatics()
166182
returntrue;
167183
}
168184

185+
boolisFullyInitialized;
169186
try
170187
{
171188
s_isSingleProcessor=Environment.IsSingleProcessor;
172189
s_maxSpinCount=DetermineMaxSpinCount();
173190
s_minSpinCount=DetermineMinSpinCount();
174191

175-
// Also initialize some types that are used later to prevent potential class construction cycles
176-
_=NativeRuntimeEventSource.Log;
192+
// Also initialize some types that are used later to prevent potential class construction cycles. If
193+
// NativeRuntimeEventSource is already being class-constructed by this thread earlier in the stack, Log can be
194+
// null. Avoid going down the wait path in that case to avoid null checks in several other places.
195+
isFullyInitialized=NativeRuntimeEventSource.Log!=null;
177196
}
178197
catch
179198
{
@@ -182,7 +201,7 @@ private static bool TryInitializeStatics()
182201
}
183202

184203
Volatile.Write(refs_staticsInitializationStage,(int)StaticsInitializationStage.Complete);
185-
returntrue;
204+
returnisFullyInitialized;
186205
}
187206

188207
// Returns false until the static variable is lazy-initialized

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp