From 41c2159aaab900fe59bac30796a7e9ca2dc2e53d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 21 Oct 2018 17:51:21 +0200 Subject: [PATCH 1/2] Use features that are now available in netstandard. Various features were moved into `IRuntimePlatform` because they weren't available on netstandard 1.x. They're now available in netstandard 2 so use them directly. --- src/Avalonia.Base/Platform/IRuntimePlatform.cs | 3 --- src/Avalonia.Controls/AppBuilderBase.cs | 2 +- src/Avalonia.DotNetFrameworkRuntime/AppBuilder.cs | 4 ++-- src/Linux/Avalonia.LinuxFramebuffer/Mice.cs | 2 +- .../PortableXaml/AvaloniaRuntimeTypeProvider.cs | 4 +--- src/Shared/PlatformSupport/AssetLoader.cs | 2 +- src/Shared/PlatformSupport/StandardRuntimePlatform.cs | 4 ---- 7 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Base/Platform/IRuntimePlatform.cs b/src/Avalonia.Base/Platform/IRuntimePlatform.cs index 9adb98e689..a0d5d611b3 100644 --- a/src/Avalonia.Base/Platform/IRuntimePlatform.cs +++ b/src/Avalonia.Base/Platform/IRuntimePlatform.cs @@ -5,10 +5,7 @@ namespace Avalonia.Platform { public interface IRuntimePlatform { - Assembly[] GetLoadedAssemblies(); - void PostThreadPoolItem(Action cb); IDisposable StartSystemTimer(TimeSpan interval, Action tick); - string GetStackTrace(); RuntimePlatformInfo GetRuntimeInfo(); IUnmanagedBlob AllocBlob(int size); } diff --git a/src/Avalonia.Controls/AppBuilderBase.cs b/src/Avalonia.Controls/AppBuilderBase.cs index 9561282274..376714b20b 100644 --- a/src/Avalonia.Controls/AppBuilderBase.cs +++ b/src/Avalonia.Controls/AppBuilderBase.cs @@ -222,7 +222,7 @@ namespace Avalonia.Controls private void SetupAvaloniaModules() { - var moduleInitializers = from assembly in AvaloniaLocator.Current.GetService().GetLoadedAssemblies() + var moduleInitializers = from assembly in AppDomain.CurrentDomain.GetAssemblies() from attribute in assembly.GetCustomAttributes() where attribute.ForWindowingSubsystem == "" || attribute.ForWindowingSubsystem == WindowingSubsystemName diff --git a/src/Avalonia.DotNetFrameworkRuntime/AppBuilder.cs b/src/Avalonia.DotNetFrameworkRuntime/AppBuilder.cs index bae97f503d..fe791e156b 100644 --- a/src/Avalonia.DotNetFrameworkRuntime/AppBuilder.cs +++ b/src/Avalonia.DotNetFrameworkRuntime/AppBuilder.cs @@ -55,7 +55,7 @@ namespace Avalonia LoadAssembliesInDirectory(); - var windowingSubsystemAttribute = (from assembly in RuntimePlatform.GetLoadedAssemblies() + var windowingSubsystemAttribute = (from assembly in AppDomain.CurrentDomain.GetAssemblies() from attribute in assembly.GetCustomAttributes() where attribute.RequiredOS == os && CheckEnvironment(attribute.EnvironmentChecker) orderby attribute.Priority ascending @@ -65,7 +65,7 @@ namespace Avalonia throw new InvalidOperationException("No windowing subsystem found. Are you missing assembly references?"); } - var renderingSubsystemAttribute = (from assembly in RuntimePlatform.GetLoadedAssemblies() + var renderingSubsystemAttribute = (from assembly in AppDomain.CurrentDomain.GetAssemblies() from attribute in assembly.GetCustomAttributes() where attribute.RequiredOS == os && CheckEnvironment(attribute.EnvironmentChecker) where attribute.RequiresWindowingSubsystem == null diff --git a/src/Linux/Avalonia.LinuxFramebuffer/Mice.cs b/src/Linux/Avalonia.LinuxFramebuffer/Mice.cs index 291374a75e..4fa57dbf00 100644 --- a/src/Linux/Avalonia.LinuxFramebuffer/Mice.cs +++ b/src/Linux/Avalonia.LinuxFramebuffer/Mice.cs @@ -22,7 +22,7 @@ namespace Avalonia.LinuxFramebuffer _height = height; } - public void Start() => AvaloniaLocator.Current.GetService().PostThreadPoolItem(Worker); + public void Start() => ThreadPool.UnsafeQueueUserWorkItem(_ => Worker(), null); private void Worker() { diff --git a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs index cdb5fc3416..eb52e317b8 100644 --- a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs +++ b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs @@ -91,9 +91,7 @@ namespace Avalonia.Markup.Xaml.Context private void ScanNewAssemblies() { - IEnumerable assemblies = AvaloniaLocator.Current - .GetService() - ?.GetLoadedAssemblies(); + IEnumerable assemblies = AppDomain.CurrentDomain.GetAssemblies(); if (assemblies != null) { diff --git a/src/Shared/PlatformSupport/AssetLoader.cs b/src/Shared/PlatformSupport/AssetLoader.cs index d73c98415e..3392baee0a 100644 --- a/src/Shared/PlatformSupport/AssetLoader.cs +++ b/src/Shared/PlatformSupport/AssetLoader.cs @@ -157,7 +157,7 @@ namespace Avalonia.Shared.PlatformSupport AssemblyDescriptor rv; if (!AssemblyNameCache.TryGetValue(name, out rv)) { - var loadedAssemblies = AvaloniaLocator.Current.GetService().GetLoadedAssemblies(); + var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); var match = loadedAssemblies.FirstOrDefault(a => a.GetName().Name == name); if (match != null) { diff --git a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs index beed847539..613cf2baf6 100644 --- a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs +++ b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs @@ -13,15 +13,11 @@ namespace Avalonia.Shared.PlatformSupport { internal partial class StandardRuntimePlatform : IRuntimePlatform { - public void PostThreadPoolItem(Action cb) => ThreadPool.UnsafeQueueUserWorkItem(_ => cb(), null); - public Assembly[] GetLoadedAssemblies() => AppDomain.CurrentDomain.GetAssemblies(); public IDisposable StartSystemTimer(TimeSpan interval, Action tick) { return new Timer(_ => tick(), null, interval, interval); } - public string GetStackTrace() => Environment.StackTrace; - public IUnmanagedBlob AllocBlob(int size) => new UnmanagedBlob(this, size); class UnmanagedBlob : IUnmanagedBlob From da55b4b3eddf6613323cada6767a5a46f16b7f90 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 21 Oct 2018 17:53:26 +0200 Subject: [PATCH 2/2] Don't pinvoke to compare threads. This was implemented in native code for netstandard 1, but now we can do it in managed code. Much faster. --- src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs | 3 --- src/Windows/Avalonia.Win32/Win32Platform.cs | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index c3c867d3fb..f0e2c1f689 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -751,9 +751,6 @@ namespace Avalonia.Win32.Interop [DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string lpModuleName); - [DllImport("kernel32.dll")] - public static extern uint GetCurrentThreadId(); - [DllImport("user32.dll")] public static extern int GetSystemMetrics(SystemMetric smIndex); diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 33bee4a82a..4b9c0efde7 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -41,7 +41,7 @@ namespace Avalonia.Win32 class Win32Platform : IPlatformThreadingInterface, IPlatformSettings, IWindowingPlatform, IPlatformIconLoader { private static readonly Win32Platform s_instance = new Win32Platform(); - private static uint _uiThread; + private static Thread _uiThread; private UnmanagedMethods.WndProc _wndProcDelegate; private IntPtr _hwnd; private readonly List _delegates = new List(); @@ -93,7 +93,7 @@ namespace Avalonia.Win32 .Bind().ToConstant(s_instance); Win32GlManager.Initialize(); UseDeferredRendering = deferredRendering; - _uiThread = UnmanagedMethods.GetCurrentThreadId(); + _uiThread = Thread.CurrentThread; if (OleContext.Current != null) AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); @@ -157,7 +157,7 @@ namespace Avalonia.Win32 new IntPtr(SignalL)); } - public bool CurrentThreadIsLoopThread => _uiThread == UnmanagedMethods.GetCurrentThreadId(); + public bool CurrentThreadIsLoopThread => _uiThread == Thread.CurrentThread; public event Action Signaled;