OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Runtime.InteropServices; |
| 36 using System.Threading.Tasks; |
| 37 using Grpc.Core.Internal; |
| 38 using Grpc.Core.Logging; |
| 39 using Grpc.Core.Utils; |
| 40 |
| 41 namespace Grpc.Core |
| 42 { |
| 43 /// <summary> |
| 44 /// Encapsulates initialization and shutdown of gRPC library. |
| 45 /// </summary> |
| 46 public class GrpcEnvironment |
| 47 { |
| 48 const int THREAD_POOL_SIZE = 4; |
| 49 |
| 50 static object staticLock = new object(); |
| 51 static GrpcEnvironment instance; |
| 52 static int refCount; |
| 53 |
| 54 static ILogger logger = new ConsoleLogger(); |
| 55 |
| 56 readonly GrpcThreadPool threadPool; |
| 57 readonly CompletionRegistry completionRegistry; |
| 58 readonly DebugStats debugStats = new DebugStats(); |
| 59 bool isClosed; |
| 60 |
| 61 /// <summary> |
| 62 /// Returns a reference-counted instance of initialized gRPC environment
. |
| 63 /// Subsequent invocations return the same instance unless reference cou
nt has dropped to zero previously. |
| 64 /// </summary> |
| 65 internal static GrpcEnvironment AddRef() |
| 66 { |
| 67 lock (staticLock) |
| 68 { |
| 69 refCount++; |
| 70 if (instance == null) |
| 71 { |
| 72 instance = new GrpcEnvironment(); |
| 73 } |
| 74 return instance; |
| 75 } |
| 76 } |
| 77 |
| 78 /// <summary> |
| 79 /// Decrements the reference count for currently active environment and
shuts down the gRPC environment if reference count drops to zero. |
| 80 /// (and blocks until the environment has been fully shutdown). |
| 81 /// </summary> |
| 82 internal static void Release() |
| 83 { |
| 84 lock (staticLock) |
| 85 { |
| 86 GrpcPreconditions.CheckState(refCount > 0); |
| 87 refCount--; |
| 88 if (refCount == 0) |
| 89 { |
| 90 instance.Close(); |
| 91 instance = null; |
| 92 } |
| 93 } |
| 94 } |
| 95 |
| 96 internal static int GetRefCount() |
| 97 { |
| 98 lock (staticLock) |
| 99 { |
| 100 return refCount; |
| 101 } |
| 102 } |
| 103 |
| 104 /// <summary> |
| 105 /// Gets application-wide logger used by gRPC. |
| 106 /// </summary> |
| 107 /// <value>The logger.</value> |
| 108 public static ILogger Logger |
| 109 { |
| 110 get |
| 111 { |
| 112 return logger; |
| 113 } |
| 114 } |
| 115 |
| 116 /// <summary> |
| 117 /// Sets the application-wide logger that should be used by gRPC. |
| 118 /// </summary> |
| 119 public static void SetLogger(ILogger customLogger) |
| 120 { |
| 121 GrpcPreconditions.CheckNotNull(customLogger, "customLogger"); |
| 122 logger = customLogger; |
| 123 } |
| 124 |
| 125 /// <summary> |
| 126 /// Creates gRPC environment. |
| 127 /// </summary> |
| 128 private GrpcEnvironment() |
| 129 { |
| 130 GrpcNativeInit(); |
| 131 completionRegistry = new CompletionRegistry(this); |
| 132 threadPool = new GrpcThreadPool(this, THREAD_POOL_SIZE); |
| 133 threadPool.Start(); |
| 134 } |
| 135 |
| 136 /// <summary> |
| 137 /// Gets the completion registry used by this gRPC environment. |
| 138 /// </summary> |
| 139 internal CompletionRegistry CompletionRegistry |
| 140 { |
| 141 get |
| 142 { |
| 143 return this.completionRegistry; |
| 144 } |
| 145 } |
| 146 |
| 147 /// <summary> |
| 148 /// Gets the completion queue used by this gRPC environment. |
| 149 /// </summary> |
| 150 internal CompletionQueueSafeHandle CompletionQueue |
| 151 { |
| 152 get |
| 153 { |
| 154 return this.threadPool.CompletionQueue; |
| 155 } |
| 156 } |
| 157 |
| 158 /// <summary> |
| 159 /// Gets the completion queue used by this gRPC environment. |
| 160 /// </summary> |
| 161 internal DebugStats DebugStats |
| 162 { |
| 163 get |
| 164 { |
| 165 return this.debugStats; |
| 166 } |
| 167 } |
| 168 |
| 169 /// <summary> |
| 170 /// Gets version of gRPC C core. |
| 171 /// </summary> |
| 172 internal static string GetCoreVersionString() |
| 173 { |
| 174 var ptr = NativeMethods.Get().grpcsharp_version_string(); // the po
inter is not owned |
| 175 return Marshal.PtrToStringAnsi(ptr); |
| 176 } |
| 177 |
| 178 internal static void GrpcNativeInit() |
| 179 { |
| 180 NativeMethods.Get().grpcsharp_init(); |
| 181 } |
| 182 |
| 183 internal static void GrpcNativeShutdown() |
| 184 { |
| 185 NativeMethods.Get().grpcsharp_shutdown(); |
| 186 } |
| 187 |
| 188 /// <summary> |
| 189 /// Shuts down this environment. |
| 190 /// </summary> |
| 191 private void Close() |
| 192 { |
| 193 if (isClosed) |
| 194 { |
| 195 throw new InvalidOperationException("Close has already been call
ed"); |
| 196 } |
| 197 threadPool.Stop(); |
| 198 GrpcNativeShutdown(); |
| 199 isClosed = true; |
| 200 |
| 201 debugStats.CheckOK(); |
| 202 } |
| 203 } |
| 204 } |
OLD | NEW |