make DiagnosticCounter and its children lock 'this' instead of an arbitrary String...
authorSung Yoon Whang <suwhang@microsoft.com>
Mon, 17 Jun 2019 21:31:55 +0000 (14:31 -0700)
committerStephen Toub <stoub@microsoft.com>
Mon, 17 Jun 2019 21:31:55 +0000 (17:31 -0400)
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/DiagnosticCounter.cs
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventCounter.cs
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/IncrementingEventCounter.cs
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/IncrementingPollingCounter.cs
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/PollingCounter.cs

index 5a4429f..481a295 100644 (file)
@@ -69,7 +69,7 @@ namespace System.Diagnostics.Tracing
         /// </summary>
         public void AddMetadata(string key, string value)
         {
-            lock (MyLock)
+            lock (this)
             {
                 _metadata = _metadata ?? new Dictionary<string, string>();
                 _metadata.Add(key, value);
@@ -111,9 +111,6 @@ namespace System.Diagnostics.Tracing
 
         internal abstract void WritePayload(float intervalSec, int pollingIntervalMillisec);
 
-        // arbitrarily we use name as the lock object.  
-        internal object MyLock { get { return Name; } }
-
         internal void ReportOutOfBandMessage(string message)
         {
             EventSource.ReportOutOfBandMessage(message, true);
@@ -121,6 +118,8 @@ namespace System.Diagnostics.Tracing
 
         internal string GetMetadataString()
         {
+            Debug.Assert(Monitor.IsEntered(this));
+
             if (_metadata == null)
             {
                 return "";
index 43026e8..5a6509b 100644 (file)
@@ -71,7 +71,7 @@ namespace System.Diagnostics.Tracing
 
         internal void OnMetricWritten(double value)
         {
-            Debug.Assert(Monitor.IsEntered(MyLock));
+            Debug.Assert(Monitor.IsEntered(this));
             _sum += value;
             _sumSquared += value * value;
             if (value > _max)
@@ -85,7 +85,7 @@ namespace System.Diagnostics.Tracing
 
         internal override void WritePayload(float intervalSec, int pollingIntervalMillisec)
         {
-            lock (MyLock)
+            lock (this)
             {
                 Flush();
                 CounterPayload payload = new CounterPayload();
@@ -116,7 +116,7 @@ namespace System.Diagnostics.Tracing
 
         private void ResetStatistics()
         {
-            Debug.Assert(Monitor.IsEntered(MyLock));
+            Debug.Assert(Monitor.IsEntered(this));
             _count = 0;
             _sum = 0;
             _sumSquared = 0;
@@ -154,7 +154,7 @@ namespace System.Diagnostics.Tracing
                 {
                     // It is possible that two threads both think the buffer is full, but only one get to actually flush it, the other
                     // will eventually enter this code path and potentially calling Flushing on a buffer that is not full, and that's okay too.
-                    lock (MyLock) // Lock the counter
+                    lock (this) // Lock the counter
                         Flush();
                     i = 0;
                 }
@@ -170,7 +170,7 @@ namespace System.Diagnostics.Tracing
 
         protected void Flush()
         {
-            Debug.Assert(Monitor.IsEntered(MyLock));
+            Debug.Assert(Monitor.IsEntered(this));
             for (int i = 0; i < _bufferedValues.Length; i++)
             {
                 var value = Interlocked.Exchange(ref _bufferedValues[i], UnusedBufferSlotValue);
index 596de0c..3bfdb7b 100644 (file)
@@ -43,7 +43,7 @@ namespace System.Diagnostics.Tracing
         /// <param name="increment">The value to increment by.</param>
         public void Increment(double increment = 1)
         {
-            lock (MyLock)
+            lock (this)
             {
                 _increment += increment;
             }
@@ -57,7 +57,7 @@ namespace System.Diagnostics.Tracing
 
         internal override void WritePayload(float intervalSec, int pollingIntervalMillisec)
         {
-            lock (MyLock)     // Lock the counter
+            lock (this)     // Lock the counter
             {
                 IncrementingCounterPayload payload = new IncrementingCounterPayload();
                 payload.Name = Name;
index 06d1d75..5c52460 100644 (file)
@@ -56,7 +56,7 @@ namespace System.Diagnostics.Tracing
         {
             try
             {
-                lock (MyLock)
+                lock (this)
                 {
                     _increment = _totalValueProvider();
                 }
@@ -70,7 +70,7 @@ namespace System.Diagnostics.Tracing
         internal override void WritePayload(float intervalSec, int pollingIntervalMillisec)
         {
             UpdateMetric();
-            lock (MyLock)     // Lock the counter
+            lock (this)     // Lock the counter
             {
                 IncrementingCounterPayload payload = new IncrementingCounterPayload();
                 payload.Name = Name;
index 13ff314..08a10ae 100644 (file)
@@ -47,7 +47,7 @@ namespace System.Diagnostics.Tracing
 
         internal override void WritePayload(float intervalSec, int pollingIntervalMillisec)
         {
-            lock (MyLock)
+            lock (this)
             {
                 double value = 0;
                 try