|
|
@ -29,7 +29,7 @@ namespace Grpc.Core.Internal |
|
|
|
{ |
|
|
|
{ |
|
|
|
static readonly NativeMethods Native = NativeMethods.Get(); |
|
|
|
static readonly NativeMethods Native = NativeMethods.Get(); |
|
|
|
|
|
|
|
|
|
|
|
AtomicCounter shutdownRefcount = new AtomicCounter(1); |
|
|
|
AtomicCounter shutdownRefcount; |
|
|
|
CompletionRegistry completionRegistry; |
|
|
|
CompletionRegistry completionRegistry; |
|
|
|
|
|
|
|
|
|
|
|
private CompletionQueueSafeHandle() |
|
|
|
private CompletionQueueSafeHandle() |
|
|
@ -51,6 +51,7 @@ namespace Grpc.Core.Internal |
|
|
|
{ |
|
|
|
{ |
|
|
|
var cq = Native.grpcsharp_completion_queue_create_async(); |
|
|
|
var cq = Native.grpcsharp_completion_queue_create_async(); |
|
|
|
cq.completionRegistry = completionRegistry; |
|
|
|
cq.completionRegistry = completionRegistry; |
|
|
|
|
|
|
|
cq.shutdownRefcount = new AtomicCounter(1); |
|
|
|
return cq; |
|
|
|
return cq; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -95,23 +96,29 @@ namespace Grpc.Core.Internal |
|
|
|
|
|
|
|
|
|
|
|
private void DecrementShutdownRefcount() |
|
|
|
private void DecrementShutdownRefcount() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (shutdownRefcount.Decrement() == 0) |
|
|
|
if (shutdownRefcount == null || shutdownRefcount.Decrement() == 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Native.grpcsharp_completion_queue_shutdown(this); |
|
|
|
Native.grpcsharp_completion_queue_shutdown(this); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void BeginOp() |
|
|
|
private void BeginOp() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (shutdownRefcount != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
bool success = false; |
|
|
|
bool success = false; |
|
|
|
shutdownRefcount.IncrementIfNonzero(ref success); |
|
|
|
shutdownRefcount.IncrementIfNonzero(ref success); |
|
|
|
GrpcPreconditions.CheckState(success, "Shutdown has already been called"); |
|
|
|
GrpcPreconditions.CheckState(success, "Shutdown has already been called"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void EndOp() |
|
|
|
private void EndOp() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (shutdownRefcount != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
DecrementShutdownRefcount(); |
|
|
|
DecrementShutdownRefcount(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Allows declaring BeginOp and EndOp of a completion queue with a using statement. |
|
|
|
// Allows declaring BeginOp and EndOp of a completion queue with a using statement. |
|
|
|
// Declared as struct for better performance. |
|
|
|
// Declared as struct for better performance. |
|
|
|