Skip to content

Commit

Permalink
Small cleanup for Lock in Mono (#94275)
Browse files Browse the repository at this point in the history
* Small cleanup for Lock in Mono

- Enable preview features in shared CoreLib project instead of disabling the compiler warning. Probably a bit cleaner, and would enable using Lock in other libraries to try it out.
- Undid an unnecessary change from #87672
  • Loading branch information
kouvel authored Nov 2, 2023
1 parent 43d164d commit 13b4315
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ public static void PulseAll(object obj)
ObjPulseAll(obj);
}

#pragma warning disable CA2252 // Opt in to preview features before using them (Lock)
/// <summary>
/// Gets the number of times there was contention upon trying to take a <see cref="Monitor"/>'s lock so far.
/// </summary>
public static long LockContentionCount => GetLockContentionCount() + Lock.ContentionCount;
#pragma warning restore CA2252

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ObjectNative_GetMonitorLockContentionCount")]
private static partial long GetLockContentionCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<!-- AD0001 : https://github.com/dotnet/runtime/issues/90356 -->
<NoWarn>$(NoWarn);AD0001</NoWarn>
<!-- CA2252: Opt in to preview features before using them (Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<SharedGUID>c5ed3c1d-b572-46f1-8f96-522a85ce1179</SharedGUID>
<StringResourcesName Condition="'$(StringResourcesName)' == ''">System.Private.CoreLib.Strings</StringResourcesName>
<GenerateResourcesSubstitutions>true</GenerateResourcesSubstitutions>

<!-- CA2252: Opt in to preview features before using them (for System.Threading.Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ private void ContentionLockCreated(nint LockID, nint AssociatedObjectID, ushort
LogContentionLockCreated(LockID, AssociatedObjectID, ClrInstanceID);
}

#pragma warning disable CA2252 // Opt in to preview features before using them (Lock)
[NonEvent]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ContentionLockCreated(Lock lockObj) => ContentionLockCreated(lockObj.LockIdForEvents, lockObj.ObjectIdForEvents);
#pragma warning restore CA2252

[Event(81, Level = EventLevel.Informational, Message = Messages.ContentionStart, Task = Tasks.Contention, Opcode = EventOpcode.Start, Version = 2, Keywords = Keywords.ContentionKeyword)]
private void ContentionStart(
Expand All @@ -101,7 +99,6 @@ private void ContentionStart(
LogContentionStart(ContentionFlags, ClrInstanceID, LockID, AssociatedObjectID, LockOwnerThreadID);
}

#pragma warning disable CA2252 // Opt in to preview features before using them (Lock)
[NonEvent]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ContentionStart(Lock lockObj) =>
Expand All @@ -111,7 +108,6 @@ public void ContentionStart(Lock lockObj) =>
lockObj.LockIdForEvents,
lockObj.ObjectIdForEvents,
lockObj.OwningThreadId);
#pragma warning restore CA2252

[Event(91, Level = EventLevel.Informational, Message = Messages.ContentionStop, Task = Tasks.Contention, Opcode = EventOpcode.Stop, Version = 1, Keywords = Keywords.ContentionKeyword)]
private void ContentionStop(ContentionFlagsMap ContentionFlags, ushort ClrInstanceID, double DurationNs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ private unsafe void ContentionLockCreated(nint LockID, nint AssociatedObjectID,
WriteEventCore(90, 3, data);
}

#pragma warning disable CA2252 // Opt in to preview features before using them (Lock)
[NonEvent]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ContentionLockCreated(Lock lockObj) => ContentionLockCreated(lockObj.LockIdForEvents, lockObj.ObjectIdForEvents);
#pragma warning restore CA2252

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", Justification = "Parameters to this method are primitive and are trimmer safe")]
[Event(81, Level = EventLevel.Informational, Message = Messages.ContentionStart, Task = Tasks.Contention, Opcode = EventOpcode.Start, Version = 2, Keywords = Keywords.ContentionKeyword)]
Expand Down Expand Up @@ -131,7 +129,6 @@ private unsafe void ContentionStart(
WriteEventCore(81, 3, data);
}

#pragma warning disable CA2252 // Opt in to preview features before using them (Lock)
[NonEvent]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ContentionStart(Lock lockObj) =>
Expand All @@ -141,7 +138,6 @@ public void ContentionStart(Lock lockObj) =>
lockObj.LockIdForEvents,
lockObj.ObjectIdForEvents,
lockObj.OwningThreadId);
#pragma warning restore CA2252

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", Justification = "Parameters to this method are primitive and are trimmer safe")]
[Event(91, Level = EventLevel.Informational, Message = Messages.ContentionStop, Task = Tasks.Contention, Opcode = EventOpcode.Stop, Version = 1, Keywords = Keywords.ContentionKeyword)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ private static void ReliableEnterTimeout(object obj, int timeout, ref bool lockT
try_enter_with_atomic_var(obj, timeout, true, ref lockTaken);
}

#pragma warning disable CA2252 // Opt in to preview features before using them (Lock)
public static long LockContentionCount => Monitor_get_lock_contention_count() + Lock.ContentionCount;
#pragma warning restore CA2252

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern long Monitor_get_lock_contention_count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace System.Threading
//
internal partial class TimerQueue
{
public static long TickCount64 => Environment.TickCount64;
private static long TickCount64 => Environment.TickCount64;
private static List<TimerQueue>? s_scheduledTimers;
private static List<TimerQueue>? s_scheduledTimersToFire;
private static long s_shortestDueTimeMs = long.MaxValue;
Expand Down

0 comments on commit 13b4315

Please sign in to comment.