Skip to content

Commit

Permalink
fix: HasThreadAccess should be true on Wasm when threading not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jul 3, 2024
1 parent 1d1f8c9 commit f7b0954
Show file tree
Hide file tree
Showing 14 changed files with 10 additions and 67 deletions.
7 changes: 1 addition & 6 deletions src/Uno.UI.Dispatching/Dispatching/DispatcherQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ public static DispatcherQueue GetForCurrentThread()
{
if (_current == null) // Do not even check for thread access if we already have a value!
{
// This check is disabled on WASM until threading support is enabled, since HasThreadAccess is currently user-configured (and defaults to false).
if (
#if __WASM__
NativeDispatcher.IsThreadingSupported &&
#endif
!NativeDispatcher.Main.HasThreadAccess)
if (!NativeDispatcher.Main.HasThreadAccess)
{
return default;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Uno.UI.Dispatching/Native/NativeDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ private NativeDispatcher()
/// </summary>
internal static void CheckThreadAccess()
{
// This check is disabled on WASM unless threading is enabled
if (
#if __WASM__
NativeDispatcher.IsThreadingSupported &&
#endif
!Main.HasThreadAccess)
if (!Main.HasThreadAccess)
{
throw new InvalidOperationException("The application called an interface that was marshalled for a different thread.");
}
Expand Down
4 changes: 1 addition & 3 deletions src/Uno.UI.Dispatching/Native/NativeDispatcher.unittests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ namespace Uno.UI.Dispatching
{
internal sealed partial class NativeDispatcher
{
internal static bool HasThreadAccessOverride { get; set; } = true;

private bool GetHasThreadAccess() => HasThreadAccessOverride;
private bool GetHasThreadAccess() => true;

private bool HasWork =>
_queues[0].Count +
Expand Down
5 changes: 1 addition & 4 deletions src/Uno.UI.Dispatching/Native/NativeDispatcher.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ partial void Initialize()
= Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES")
?.Split(',').Contains("threads", StringComparer.OrdinalIgnoreCase) ?? false;

// Always reschedule, otherwise we may end up in live-lock.
internal static bool HasThreadAccessOverride { get; set; }

private bool GetHasThreadAccess()
=> IsThreadingSupported ? Environment.CurrentManagedThreadId == 1 : HasThreadAccessOverride;
=> !IsThreadingSupported || Environment.CurrentManagedThreadId == 1;

/// <summary>
/// Provide an action that will delegate the dispatch of CoreDispatcher work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,6 @@ public static async void PressKeySequence(string keys, UIElement element = null)
async Task RaiseOnElementDispatcherAsync(UIElement element, RoutedEvent routedEvent, RoutedEventArgs args)
{
bool raiseSynchronously = element.Dispatcher.HasThreadAccess;
#if __WASM__
if (!Uno.UI.Dispatching.NativeDispatcher.IsThreadingSupported)
{
raiseSynchronously = true;
}
#endif

if (raiseSynchronously)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ internal static bool HasDispatcherAccess
{
get
{
#if __WASM__ // TODO Uno: To be adjusted for #2302
return false;
#else
return WindowHelper.RootElementDispatcher.HasThreadAccess;
#endif
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/Uno.UI.RuntimeTests/MUX/Utilities/RunOnUIThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public static void Execute(CoreApplicationView whichView, Action action)
{
Exception exception = null;
var dispatcher = whichView.Dispatcher;
if (dispatcher.HasThreadAccess
#if __WASM__
|| !Uno.UI.Dispatching.NativeDispatcher.IsThreadingSupported
#endif
)
if (dispatcher.HasThreadAccess)
{
action();
}
Expand Down Expand Up @@ -122,11 +118,7 @@ public static async Task ExecuteAsync(CoreApplicationView whichView, Func<Task>
{
Exception exception = null;
var dispatcher = whichView.Dispatcher;
if (dispatcher.HasThreadAccess
#if __WASM__
|| !Uno.UI.Dispatching.NativeDispatcher.IsThreadingSupported
#endif
)
if (dispatcher.HasThreadAccess)
{
await task();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public async Task When_ScheduleNonRepeatingWorkItem()
}
}

#if !WINAPPSDK && !__WASM__ // CoreDispatcher.Main.HasThreadAccess is always false on WASM ...
#if !WINAPPSDK
[TestMethod]
public async Task When_Tick_Then_RunningOnDispatcher()
{
Expand Down
2 changes: 0 additions & 2 deletions src/Uno.UI/UI/Xaml/DependencyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,10 @@ internal bool IsUnoType
/// <returns>A <see cref="DependencyProperty"/> instance, otherwise null it not found.</returns>
internal static DependencyProperty GetProperty(Type type, string name)
{
#if !__WASM__
if (!FeatureConfiguration.DependencyProperty.DisableThreadingCheck && !NativeDispatcher.Main.HasThreadAccess)
{
throw new InvalidOperationException("The dependency property system should not be accessed from non UI thread.");
}
#endif

_searchPropertyCacheEntry.Update(type, name);

Expand Down
6 changes: 1 addition & 5 deletions src/Uno.UI/UI/Xaml/DispatcherConditionalDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public DispatcherConditionalDisposable(object target, WeakReference conditionSou

protected override void TargetFinalized()
{
if (CoreDispatcher.Main.HasThreadAccess
#if __WASM__
|| !NativeDispatcher.IsThreadingSupported
#endif
)
if (CoreDispatcher.Main.HasThreadAccess)
{
DispatchedTargetFinalized();
}
Expand Down
6 changes: 1 addition & 5 deletions src/Uno.UI/UI/Xaml/DragDrop/DragDropManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public DragDropManager(InputManager inputManager)
/// <inheritdoc />
public void BeginDragAndDrop(CoreDragInfo info, ICoreDropOperationTarget? target = null)
{
if (
#if __WASM__
Uno.UI.Dispatching.NativeDispatcher.IsThreadingSupported &&
#endif
_inputManager.ContentRoot.Dispatcher is { } dispatcher &&
if (_inputManager.ContentRoot.Dispatcher is { } dispatcher &&
!dispatcher.HasThreadAccess)
{
_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => BeginDragAndDrop(info, target));
Expand Down
7 changes: 1 addition & 6 deletions src/Uno.UWP/System/DispatcherQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ public static DispatcherQueue GetForCurrentThread()
{
if (_current == null) // Do not even check for thread access if we already have a value!
{
// This check is disabled on WASM until threading support is enabled, since HasThreadAccess is currently user-configured (and defaults to false).
if (
#if __WASM__
NativeDispatcher.IsThreadingSupported &&
#endif
!NativeDispatcher.Main.HasThreadAccess)
if (!NativeDispatcher.Main.HasThreadAccess)
{
return default;
}
Expand Down
6 changes: 0 additions & 6 deletions src/Uno.UWP/UI/Core/CoreDispatcher.unittests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ namespace Windows.UI.Core
{
public sealed partial class CoreDispatcher
{
public static bool HasThreadAccessOverride
{
get => NativeDispatcher.HasThreadAccessOverride;
set => NativeDispatcher.HasThreadAccessOverride = value;
}

public void ProcessEvents(CoreProcessEventsOption options)
=> _inner.ProcessEvents((Uno.UI.Dispatching.CoreProcessEventsOption)options);
}
Expand Down
3 changes: 0 additions & 3 deletions src/Uno.UWP/UI/Core/CoreDispatcher.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ internal static Action<Action, NativeDispatcherPriority> DispatchOverride
get => NativeDispatcher.DispatchOverride;
set => NativeDispatcher.DispatchOverride = value;
}

// Always reschedule, otherwise we may end up in live-lock.
internal static bool HasThreadAccessOverride => NativeDispatcher.HasThreadAccessOverride;
}
}

0 comments on commit f7b0954

Please sign in to comment.