diff options
author | Craig Tiller <ctiller@google.com> | 2016-10-03 12:56:57 -0700 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2016-10-03 12:56:57 -0700 |
commit | 787d58f9e36487d0703425b9b1d30dd658c42728 (patch) | |
tree | 417293b2a9b1198b6af5d7e91b970c9d304e8352 /src/csharp | |
parent | 7af400c53579c3b036bd75558febc9f0d724763c (diff) | |
parent | 9070ab6610bd8cd0dbe6922a049a3b094c908bd1 (diff) |
Merge github.com:grpc/grpc into direct-calls
Diffstat (limited to 'src/csharp')
-rw-r--r-- | src/csharp/Grpc.Core/Internal/PlatformApis.cs | 11 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs | 29 |
2 files changed, 36 insertions, 4 deletions
diff --git a/src/csharp/Grpc.Core/Internal/PlatformApis.cs b/src/csharp/Grpc.Core/Internal/PlatformApis.cs index 15391ddc64..406204e0a7 100644 --- a/src/csharp/Grpc.Core/Internal/PlatformApis.cs +++ b/src/csharp/Grpc.Core/Internal/PlatformApis.cs @@ -50,6 +50,7 @@ namespace Grpc.Core.Internal static readonly bool isMacOSX; static readonly bool isWindows; static readonly bool isMono; + static readonly bool isNetCore; static PlatformApis() { @@ -57,6 +58,7 @@ namespace Grpc.Core.Internal isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); isMacOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + isNetCore = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core"); #else var platform = Environment.OSVersion.Platform; @@ -64,6 +66,7 @@ namespace Grpc.Core.Internal isMacOSX = (platform == PlatformID.Unix && GetUname() == "Darwin"); isLinux = (platform == PlatformID.Unix && !isMacOSX); isWindows = (platform == PlatformID.Win32NT || platform == PlatformID.Win32S || platform == PlatformID.Win32Windows); + isNetCore = false; #endif isMono = Type.GetType("Mono.Runtime") != null; } @@ -88,6 +91,14 @@ namespace Grpc.Core.Internal get { return isMono; } } + /// <summary> + /// true if running on .NET Core (CoreCLR), false otherwise. + /// </summary> + public static bool IsNetCore + { + get { return isNetCore; } + } + public static bool Is64Bit { get { return IntPtr.Size == 8; } diff --git a/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs b/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs index dc629bd714..31e1402849 100644 --- a/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs +++ b/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs @@ -44,10 +44,9 @@ namespace Grpc.Core.Internal { /// <summary> /// Represents a dynamically loaded unmanaged library in a (partially) platform independent manner. - /// An important difference in library loading semantics is that on Windows, once we load a dynamic library using LoadLibrary, - /// that library becomes instantly available for <c>DllImport</c> P/Invoke calls referring to the same library name. - /// On Unix systems, dlopen has somewhat different semantics, so we need to use dlsym and <c>Marshal.GetDelegateForFunctionPointer</c> - /// to obtain delegates to native methods. + /// First, the native library is loaded using dlopen (on Unix systems) or using LoadLibrary (on Windows). + /// dlsym or GetProcAddress are then used to obtain symbol addresses. <c>Marshal.GetDelegateForFunctionPointer</c> + /// transforms the addresses into delegates to native methods. /// See http://stackoverflow.com/questions/13461989/p-invoke-to-dynamically-loaded-library-on-mono. /// </summary> internal class UnmanagedLibrary @@ -114,6 +113,10 @@ namespace Grpc.Core.Internal { return Mono.dlsym(this.handle, symbolName); } + if (PlatformApis.IsNetCore) + { + return CoreCLR.dlsym(this.handle, symbolName); + } return Linux.dlsym(this.handle, symbolName); } if (PlatformApis.IsMacOSX) @@ -149,6 +152,10 @@ namespace Grpc.Core.Internal { return Mono.dlopen(libraryPath, RTLD_GLOBAL + RTLD_LAZY); } + if (PlatformApis.IsNetCore) + { + return CoreCLR.dlopen(libraryPath, RTLD_GLOBAL + RTLD_LAZY); + } return Linux.dlopen(libraryPath, RTLD_GLOBAL + RTLD_LAZY); } if (PlatformApis.IsMacOSX) @@ -215,5 +222,19 @@ namespace Grpc.Core.Internal [DllImport("__Internal")] internal static extern IntPtr dlsym(IntPtr handle, string symbol); } + + /// <summary> + /// Similarly as for Mono on Linux, we load symbols for + /// dlopen and dlsym from the "libcoreclr.so", + /// to avoid the dependency on libc-dev Linux. + /// </summary> + private static class CoreCLR + { + [DllImport("libcoreclr.so")] + internal static extern IntPtr dlopen(string filename, int flags); + + [DllImport("libcoreclr.so")] + internal static extern IntPtr dlsym(IntPtr handle, string symbol); + } } } |