aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/Grpc.Core/Internal
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2017-11-20 20:23:30 +0100
committerGravatar Jan Tattermusch <jtattermusch@google.com>2017-11-21 12:30:52 +0100
commiteec8b84c71bc81a7073142506f369a9b8a3a63a6 (patch)
tree69ca981a6a9cebea6b067cc876f253a6fa6ce40f /src/csharp/Grpc.Core/Internal
parent8bae77cdd17b9bb9056cfff63366ad7fd97f352d (diff)
spinlock in completion registry is slightly faster
Diffstat (limited to 'src/csharp/Grpc.Core/Internal')
-rw-r--r--src/csharp/Grpc.Core/Internal/CompletionRegistry.cs23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs b/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs
index f4aea685c3..b68655b33c 100644
--- a/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs
+++ b/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs
@@ -19,7 +19,9 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Runtime.InteropServices;
+using System.Threading;
using Grpc.Core.Logging;
using Grpc.Core.Utils;
@@ -35,7 +37,7 @@ namespace Grpc.Core.Internal
readonly GrpcEnvironment environment;
readonly Dictionary<IntPtr, IOpCompletionCallback> dict = new Dictionary<IntPtr, IOpCompletionCallback>(new IntPtrComparer());
- readonly object myLock = new object();
+ SpinLock spinLock = new SpinLock(Debugger.IsAttached);
IntPtr lastRegisteredKey; // only for testing
public CompletionRegistry(GrpcEnvironment environment)
@@ -46,11 +48,19 @@ namespace Grpc.Core.Internal
public void Register(IntPtr key, IOpCompletionCallback callback)
{
environment.DebugStats.PendingBatchCompletions.Increment();
- lock (myLock)
+
+ bool lockTaken = false;
+ try
{
+ spinLock.Enter(ref lockTaken);
+
dict.Add(key, callback);
this.lastRegisteredKey = key;
}
+ finally
+ {
+ if (lockTaken) spinLock.Exit();
+ }
}
public void RegisterBatchCompletion(BatchContextSafeHandle ctx, BatchCompletionDelegate callback, object state)
@@ -68,11 +78,18 @@ namespace Grpc.Core.Internal
public IOpCompletionCallback Extract(IntPtr key)
{
IOpCompletionCallback value = null;
- lock (myLock)
+ bool lockTaken = false;
+ try
{
+ spinLock.Enter(ref lockTaken);
+
value = dict[key];
dict.Remove(key);
}
+ finally
+ {
+ if (lockTaken) spinLock.Exit();
+ }
environment.DebugStats.PendingBatchCompletions.Decrement();
return value;
}