|
|
@ -5,11 +5,9 @@ using System.Runtime.InteropServices; |
|
|
|
namespace Google.GRPC.Core |
|
|
|
namespace Google.GRPC.Core |
|
|
|
{ |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Encapsulates initialization and shutdown of GRPC C core library. |
|
|
|
/// Encapsulates initialization and shutdown of gRPC library. |
|
|
|
/// You should not need to initialize it manually, as static constructors |
|
|
|
|
|
|
|
/// should load the library when needed. |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public static class GrpcEnvironment |
|
|
|
public class GrpcEnvironment |
|
|
|
{ |
|
|
|
{ |
|
|
|
const int THREAD_POOL_SIZE = 1; |
|
|
|
const int THREAD_POOL_SIZE = 1; |
|
|
|
|
|
|
|
|
|
|
@ -20,21 +18,24 @@ namespace Google.GRPC.Core |
|
|
|
static extern void grpcsharp_shutdown(); |
|
|
|
static extern void grpcsharp_shutdown(); |
|
|
|
|
|
|
|
|
|
|
|
static object staticLock = new object(); |
|
|
|
static object staticLock = new object(); |
|
|
|
static bool initCalled = false; |
|
|
|
static volatile GrpcEnvironment instance; |
|
|
|
static bool shutdownCalled = false; |
|
|
|
|
|
|
|
|
|
|
|
readonly GrpcThreadPool threadPool; |
|
|
|
static GrpcThreadPool threadPool = new GrpcThreadPool(THREAD_POOL_SIZE); |
|
|
|
bool isClosed; |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <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> |
|
|
|
/// </summary> |
|
|
|
public static void EnsureInitialized() { |
|
|
|
public static void Initialize() { |
|
|
|
lock(staticLock) |
|
|
|
lock(staticLock) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!initCalled) |
|
|
|
if (instance == null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
initCalled = true; |
|
|
|
instance = new GrpcEnvironment(); |
|
|
|
GrpcInit(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -47,45 +48,55 @@ namespace Google.GRPC.Core |
|
|
|
{ |
|
|
|
{ |
|
|
|
lock(staticLock) |
|
|
|
lock(staticLock) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (initCalled && !shutdownCalled) |
|
|
|
if (instance != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
shutdownCalled = true; |
|
|
|
instance.Close(); |
|
|
|
GrpcShutdown(); |
|
|
|
instance = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static GrpcThreadPool ThreadPool |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
get |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var inst = instance; |
|
|
|
|
|
|
|
if (inst == null) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
throw new InvalidOperationException("GRPC environment not initialized"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return inst.threadPool; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Initializes GRPC C Core library. |
|
|
|
/// Creates gRPC environment. |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
private static void GrpcInit() |
|
|
|
private GrpcEnvironment() |
|
|
|
{ |
|
|
|
{ |
|
|
|
grpcsharp_init(); |
|
|
|
grpcsharp_init(); |
|
|
|
|
|
|
|
threadPool = new GrpcThreadPool(THREAD_POOL_SIZE); |
|
|
|
threadPool.Start(); |
|
|
|
threadPool.Start(); |
|
|
|
// TODO: use proper logging here |
|
|
|
// TODO: use proper logging here |
|
|
|
Console.WriteLine("GRPC initialized."); |
|
|
|
Console.WriteLine("GRPC initialized."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Shutdown GRPC C Core library. |
|
|
|
/// Shuts down this environment. |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
private static void GrpcShutdown() |
|
|
|
private void Close() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if (isClosed) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
throw new InvalidOperationException("Close has already been called"); |
|
|
|
|
|
|
|
} |
|
|
|
threadPool.Stop(); |
|
|
|
threadPool.Stop(); |
|
|
|
grpcsharp_shutdown(); |
|
|
|
grpcsharp_shutdown(); |
|
|
|
|
|
|
|
isClosed = true; |
|
|
|
|
|
|
|
|
|
|
|
// TODO: use proper logging here |
|
|
|
// TODO: use proper logging here |
|
|
|
Console.WriteLine("GRPC shutdown."); |
|
|
|
Console.WriteLine("GRPC shutdown."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
internal static GrpcThreadPool ThreadPool |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
get |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return threadPool; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|