Hide scheduler from RunOnFrameworkThread (#1725)

* Hide scheduler from RunOnFrameworkThread

Creating new tasks via Task.Run and alike would fetch the current
scheduler, which we do not want in case of running stuff from the
framework thread. Change is to prevent the standard library from seeing
the "current scheduler". If one wants to use `await` with an async
function to be run in the framework thread, one can use
`RunOnFrameworkThreadAwaitable` instead now.

* TaskSchedulerWidget: test better stuff

* TaskSchedulerWidget: add freeze tests

* More comments

* Make TaskFactory a getter method instead of property to avoid bad suggestions

* Why are there stuff still not pushed
This commit is contained in:
srkizer 2024-03-19 12:10:47 +09:00 committed by GitHub
parent c709ad1811
commit 5d473919a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 310 additions and 22 deletions

View file

@ -103,9 +103,6 @@ internal sealed class Framework : IInternalDisposableService, IFramework
/// <inheritdoc/>
public DateTime LastUpdateUTC { get; private set; } = DateTime.MinValue;
/// <inheritdoc/>
public TaskFactory FrameworkThreadTaskFactory { get; }
/// <inheritdoc/>
public TimeSpan UpdateDelta { get; private set; } = TimeSpan.Zero;
@ -125,6 +122,11 @@ internal sealed class Framework : IInternalDisposableService, IFramework
/// </summary>
internal bool DispatchUpdateEvents { get; set; } = true;
private TaskFactory FrameworkThreadTaskFactory { get; }
/// <inheritdoc/>
public TaskFactory GetTaskFactory() => this.FrameworkThreadTaskFactory;
/// <inheritdoc/>
public Task DelayTicks(long numTicks, CancellationToken cancellationToken = default)
{
@ -138,6 +140,38 @@ internal sealed class Framework : IInternalDisposableService, IFramework
return tcs.Task;
}
/// <inheritdoc/>
public Task RunOnFrameworkThreadAwaitable(Action action, CancellationToken cancellationToken = default)
{
if (cancellationToken == default)
cancellationToken = this.FrameworkThreadTaskFactory.CancellationToken;
return this.FrameworkThreadTaskFactory.StartNew(action, cancellationToken);
}
/// <inheritdoc/>
public Task<T> RunOnFrameworkThreadAwaitable<T>(Func<T> action, CancellationToken cancellationToken = default)
{
if (cancellationToken == default)
cancellationToken = this.FrameworkThreadTaskFactory.CancellationToken;
return this.FrameworkThreadTaskFactory.StartNew(action, cancellationToken);
}
/// <inheritdoc/>
public Task RunOnFrameworkThreadAwaitable(Func<Task> action, CancellationToken cancellationToken = default)
{
if (cancellationToken == default)
cancellationToken = this.FrameworkThreadTaskFactory.CancellationToken;
return this.FrameworkThreadTaskFactory.StartNew(action, cancellationToken).Unwrap();
}
/// <inheritdoc/>
public Task<T> RunOnFrameworkThreadAwaitable<T>(Func<Task<T>> action, CancellationToken cancellationToken = default)
{
if (cancellationToken == default)
cancellationToken = this.FrameworkThreadTaskFactory.CancellationToken;
return this.FrameworkThreadTaskFactory.StartNew(action, cancellationToken).Unwrap();
}
/// <inheritdoc/>
public Task<T> RunOnFrameworkThread<T>(Func<T> func) =>
this.IsInFrameworkUpdateThread || this.IsFrameworkUnloading ? Task.FromResult(func()) : this.RunOnTick(func);
@ -193,7 +227,9 @@ internal sealed class Framework : IInternalDisposableService, IFramework
this.DelayTicks(delayTicks, cancellationToken),
},
_ => func(),
cancellationToken);
cancellationToken,
TaskContinuationOptions.HideScheduler,
this.frameworkThreadTaskScheduler);
}
/// <inheritdoc/>
@ -218,7 +254,9 @@ internal sealed class Framework : IInternalDisposableService, IFramework
this.DelayTicks(delayTicks, cancellationToken),
},
_ => action(),
cancellationToken);
cancellationToken,
TaskContinuationOptions.HideScheduler,
this.frameworkThreadTaskScheduler);
}
/// <inheritdoc/>
@ -243,7 +281,9 @@ internal sealed class Framework : IInternalDisposableService, IFramework
this.DelayTicks(delayTicks, cancellationToken),
},
_ => func(),
cancellationToken).Unwrap();
cancellationToken,
TaskContinuationOptions.HideScheduler,
this.frameworkThreadTaskScheduler).Unwrap();
}
/// <inheritdoc/>
@ -268,7 +308,9 @@ internal sealed class Framework : IInternalDisposableService, IFramework
this.DelayTicks(delayTicks, cancellationToken),
},
_ => func(),
cancellationToken).Unwrap();
cancellationToken,
TaskContinuationOptions.HideScheduler,
this.frameworkThreadTaskScheduler).Unwrap();
}
/// <summary>
@ -491,9 +533,6 @@ internal class FrameworkPluginScoped : IInternalDisposableService, IFramework
/// <inheritdoc/>
public DateTime LastUpdateUTC => this.frameworkService.LastUpdateUTC;
/// <inheritdoc/>
public TaskFactory FrameworkThreadTaskFactory => this.frameworkService.FrameworkThreadTaskFactory;
/// <inheritdoc/>
public TimeSpan UpdateDelta => this.frameworkService.UpdateDelta;
@ -511,10 +550,29 @@ internal class FrameworkPluginScoped : IInternalDisposableService, IFramework
this.Update = null;
}
/// <inheritdoc/>
public TaskFactory GetTaskFactory() => this.frameworkService.GetTaskFactory();
/// <inheritdoc/>
public Task DelayTicks(long numTicks, CancellationToken cancellationToken = default) =>
this.frameworkService.DelayTicks(numTicks, cancellationToken);
/// <inheritdoc/>
public Task RunOnFrameworkThreadAwaitable(Action action, CancellationToken cancellationToken = default) =>
this.frameworkService.RunOnFrameworkThreadAwaitable(action, cancellationToken);
/// <inheritdoc/>
public Task<T> RunOnFrameworkThreadAwaitable<T>(Func<T> action, CancellationToken cancellationToken = default) =>
this.frameworkService.RunOnFrameworkThreadAwaitable(action, cancellationToken);
/// <inheritdoc/>
public Task RunOnFrameworkThreadAwaitable(Func<Task> action, CancellationToken cancellationToken = default) =>
this.frameworkService.RunOnFrameworkThreadAwaitable(action, cancellationToken);
/// <inheritdoc/>
public Task<T> RunOnFrameworkThreadAwaitable<T>(Func<Task<T>> action, CancellationToken cancellationToken = default) =>
this.frameworkService.RunOnFrameworkThreadAwaitable(action, cancellationToken);
/// <inheritdoc/>
public Task<T> RunOnFrameworkThread<T>(Func<T> func)
=> this.frameworkService.RunOnFrameworkThread(func);