|
|
|
@ -5,11 +5,9 @@ using System.Runtime.InteropServices; |
|
|
|
|
namespace Google.GRPC.Core |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// Encapsulates initialization and shutdown of GRPC C core library. |
|
|
|
|
/// You should not need to initialize it manually, as static constructors |
|
|
|
|
/// should load the library when needed. |
|
|
|
|
/// Encapsulates initialization and shutdown of gRPC library. |
|
|
|
|
/// </summary> |
|
|
|
|
public static class GrpcEnvironment |
|
|
|
|
public class GrpcEnvironment |
|
|
|
|
{ |
|
|
|
|
const int THREAD_POOL_SIZE = 1; |
|
|
|
|
|
|
|
|
@ -20,21 +18,24 @@ namespace Google.GRPC.Core |
|
|
|
|
static extern void grpcsharp_shutdown(); |
|
|
|
|
|
|
|
|
|
static object staticLock = new object(); |
|
|
|
|
static bool initCalled = false; |
|
|
|
|
static bool shutdownCalled = false; |
|
|
|
|
|
|
|
|
|
static GrpcThreadPool threadPool = new GrpcThreadPool(THREAD_POOL_SIZE); |
|
|
|
|
static volatile GrpcEnvironment instance; |
|
|
|
|
|
|
|
|
|
readonly GrpcThreadPool threadPool; |
|
|
|
|
bool isClosed; |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Makes sure GRPC environment is initialized. |
|
|
|
|
/// Makes sure GRPC environment is initialized. Subsequent invocations don't have any |
|
|
|
|
/// effect unless you call Shutdown first. |
|
|
|
|
/// Although normal use cases assume you will call this just once in your application's |
|
|
|
|
/// lifetime (and call Shutdown once you're done), for the sake of easier testing it's |
|
|
|
|
/// allowed to initialize the environment again after it has been successfully shutdown. |
|
|
|
|
/// </summary> |
|
|
|
|
public static void EnsureInitialized() { |
|
|
|
|
public static void Initialize() { |
|
|
|
|
lock(staticLock) |
|
|
|
|
{ |
|
|
|
|
if (!initCalled) |
|
|
|
|
if (instance == null) |
|
|
|
|
{ |
|
|
|
|
initCalled = true; |
|
|
|
|
GrpcInit(); |
|
|
|
|
instance = new GrpcEnvironment(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -47,45 +48,55 @@ namespace Google.GRPC.Core |
|
|
|
|
{ |
|
|
|
|
lock(staticLock) |
|
|
|
|
{ |
|
|
|
|
if (initCalled && !shutdownCalled) |
|
|
|
|
if (instance != null) |
|
|
|
|
{ |
|
|
|
|
shutdownCalled = true; |
|
|
|
|
GrpcShutdown(); |
|
|
|
|
instance.Close(); |
|
|
|
|
instance = null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
internal static GrpcThreadPool ThreadPool |
|
|
|
|
{ |
|
|
|
|
get |
|
|
|
|
{ |
|
|
|
|
var inst = instance; |
|
|
|
|
if (inst == null) |
|
|
|
|
{ |
|
|
|
|
throw new InvalidOperationException("GRPC environment not initialized"); |
|
|
|
|
} |
|
|
|
|
return inst.threadPool; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Initializes GRPC C Core library. |
|
|
|
|
/// Creates gRPC environment. |
|
|
|
|
/// </summary> |
|
|
|
|
private static void GrpcInit() |
|
|
|
|
private GrpcEnvironment() |
|
|
|
|
{ |
|
|
|
|
grpcsharp_init(); |
|
|
|
|
threadPool = new GrpcThreadPool(THREAD_POOL_SIZE); |
|
|
|
|
threadPool.Start(); |
|
|
|
|
// TODO: use proper logging here |
|
|
|
|
Console.WriteLine("GRPC initialized."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Shutdown GRPC C Core library. |
|
|
|
|
/// Shuts down this environment. |
|
|
|
|
/// </summary> |
|
|
|
|
private static void GrpcShutdown() |
|
|
|
|
private void Close() |
|
|
|
|
{ |
|
|
|
|
if (isClosed) |
|
|
|
|
{ |
|
|
|
|
throw new InvalidOperationException("Close has already been called"); |
|
|
|
|
} |
|
|
|
|
threadPool.Stop(); |
|
|
|
|
grpcsharp_shutdown(); |
|
|
|
|
isClosed = true; |
|
|
|
|
|
|
|
|
|
// TODO: use proper logging here |
|
|
|
|
Console.WriteLine("GRPC shutdown."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
internal static GrpcThreadPool ThreadPool |
|
|
|
|
{ |
|
|
|
|
get |
|
|
|
|
{ |
|
|
|
|
return threadPool; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|