OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 // Copyright 2015-2016, Google Inc. |
| 3 // All rights reserved. |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 #endregion |
| 31 using System; |
| 32 using System.Runtime.InteropServices; |
| 33 using System.Threading.Tasks; |
| 34 using Grpc.Core.Profiling; |
| 35 |
| 36 using Grpc.Core.Utils; |
| 37 |
| 38 namespace Grpc.Core.Internal |
| 39 { |
| 40 /// <summary> |
| 41 /// grpc_completion_queue from <c>grpc/grpc.h</c> |
| 42 /// </summary> |
| 43 internal class CompletionQueueSafeHandle : SafeHandleZeroIsInvalid |
| 44 { |
| 45 static readonly NativeMethods Native = NativeMethods.Get(); |
| 46 |
| 47 AtomicCounter shutdownRefcount = new AtomicCounter(1); |
| 48 |
| 49 private CompletionQueueSafeHandle() |
| 50 { |
| 51 } |
| 52 |
| 53 public static CompletionQueueSafeHandle Create() |
| 54 { |
| 55 return Native.grpcsharp_completion_queue_create(); |
| 56 |
| 57 } |
| 58 |
| 59 public CompletionQueueEvent Next() |
| 60 { |
| 61 return Native.grpcsharp_completion_queue_next(this); |
| 62 } |
| 63 |
| 64 public CompletionQueueEvent Pluck(IntPtr tag) |
| 65 { |
| 66 using (Profilers.ForCurrentThread().NewScope("CompletionQueueSafeHan
dle.Pluck")) |
| 67 { |
| 68 return Native.grpcsharp_completion_queue_pluck(this, tag); |
| 69 } |
| 70 } |
| 71 |
| 72 /// <summary> |
| 73 /// Creates a new usage scope for this completion queue. Once successful
ly created, |
| 74 /// the completion queue won't be shutdown before scope.Dispose() is cal
led. |
| 75 /// </summary> |
| 76 public UsageScope NewScope() |
| 77 { |
| 78 return new UsageScope(this); |
| 79 } |
| 80 |
| 81 public void Shutdown() |
| 82 { |
| 83 DecrementShutdownRefcount(); |
| 84 } |
| 85 |
| 86 protected override bool ReleaseHandle() |
| 87 { |
| 88 Native.grpcsharp_completion_queue_destroy(handle); |
| 89 return true; |
| 90 } |
| 91 |
| 92 private void DecrementShutdownRefcount() |
| 93 { |
| 94 if (shutdownRefcount.Decrement() == 0) |
| 95 { |
| 96 Native.grpcsharp_completion_queue_shutdown(this); |
| 97 } |
| 98 } |
| 99 |
| 100 private void BeginOp() |
| 101 { |
| 102 bool success = false; |
| 103 shutdownRefcount.IncrementIfNonzero(ref success); |
| 104 GrpcPreconditions.CheckState(success, "Shutdown has already been cal
led"); |
| 105 } |
| 106 |
| 107 private void EndOp() |
| 108 { |
| 109 DecrementShutdownRefcount(); |
| 110 } |
| 111 |
| 112 // Allows declaring BeginOp and EndOp of a completion queue with a using
statement. |
| 113 // Declared as struct for better performance. |
| 114 public struct UsageScope : IDisposable |
| 115 { |
| 116 readonly CompletionQueueSafeHandle cq; |
| 117 |
| 118 public UsageScope(CompletionQueueSafeHandle cq) |
| 119 { |
| 120 this.cq = cq; |
| 121 this.cq.BeginOp(); |
| 122 } |
| 123 |
| 124 public void Dispose() |
| 125 { |
| 126 cq.EndOp(); |
| 127 } |
| 128 } |
| 129 } |
| 130 } |
OLD | NEW |