mirror of
https://github.com/SK-la/osu-framework.git
synced 2026-03-15 03:20:30 +00:00
Remove ThreadedScheduler
This commit is contained in:
@@ -598,6 +598,7 @@ Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-frame
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Constants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue"><Policy Inspect="False" Prefix="" Suffix="" Style="AaBb" /></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalConstants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalFunctions/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb"><ExtraRule Prefix="_" Suffix="" Style="aaBb" /></Policy></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
||||
|
||||
@@ -7,8 +7,10 @@ using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Framework.Threading;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using osu.Framework.Threading;
|
||||
|
||||
namespace osu.Framework.Logging
|
||||
{
|
||||
@@ -55,17 +57,8 @@ namespace osu.Framework.Logging
|
||||
/// </summary>
|
||||
public static Storage Storage
|
||||
{
|
||||
private get
|
||||
{
|
||||
return storage;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
storage = value ?? throw new ArgumentNullException(nameof(value));
|
||||
lock (flush_sync_lock)
|
||||
backgroundScheduler.Enabled = true;
|
||||
}
|
||||
private get { return storage; }
|
||||
set { storage = value ?? throw new ArgumentNullException(nameof(value)); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -338,7 +331,7 @@ namespace osu.Framework.Logging
|
||||
if (!Enabled)
|
||||
return;
|
||||
|
||||
backgroundScheduler.Add(delegate
|
||||
scheduler.Add(delegate
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -371,7 +364,7 @@ namespace osu.Framework.Logging
|
||||
private void clear()
|
||||
{
|
||||
lock (flush_sync_lock)
|
||||
backgroundScheduler.Add(() => Storage.Delete(Filename));
|
||||
scheduler.Add(() => Storage.Delete(Filename));
|
||||
}
|
||||
|
||||
private bool headerAdded;
|
||||
@@ -390,7 +383,15 @@ namespace osu.Framework.Logging
|
||||
|
||||
private static readonly List<string> filters = new List<string>();
|
||||
private static readonly Dictionary<string, Logger> static_loggers = new Dictionary<string, Logger>();
|
||||
private static ThreadedScheduler backgroundScheduler = new ThreadedScheduler(@"Logger", startEnabled: false);
|
||||
private static Task writerTask;
|
||||
private static CancellationTokenSource cancellationToken;
|
||||
|
||||
private static readonly Scheduler scheduler = new Scheduler();
|
||||
|
||||
static Logger()
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pause execution until all logger writes have completed and file handles have been closed.
|
||||
@@ -400,8 +401,33 @@ namespace osu.Framework.Logging
|
||||
{
|
||||
lock (flush_sync_lock)
|
||||
{
|
||||
backgroundScheduler.Dispose();
|
||||
backgroundScheduler = new ThreadedScheduler(@"Logger") { Enabled = storage != null };
|
||||
if (writerTask != null)
|
||||
{
|
||||
cancellationToken.Cancel();
|
||||
writerTask.Wait(500);
|
||||
}
|
||||
|
||||
int performUpdate() => Storage != null ? scheduler.Update() : 0;
|
||||
|
||||
cancellationToken = new CancellationTokenSource();
|
||||
writerTask = Task.Factory.StartNew(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
performUpdate();
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
while (performUpdate() > 0)
|
||||
{
|
||||
// perform all remaining writes before exiting.
|
||||
}
|
||||
}
|
||||
}, cancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||
}
|
||||
|
||||
NewEntry = null;
|
||||
|
||||
@@ -298,64 +298,4 @@ namespace osu.Framework.Threading
|
||||
return ExecutionTime == other.ExecutionTime ? -1 : ExecutionTime.CompareTo(other.ExecutionTime);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A scheduler which doesn't require manual updates (and never uses the main thread).
|
||||
/// </summary>
|
||||
public class ThreadedScheduler : Scheduler, IDisposable
|
||||
{
|
||||
private readonly Thread workerThread;
|
||||
|
||||
/// <summary>
|
||||
/// Whether scheduled tasks should be run. Disabling temporarily pauses all execution.
|
||||
/// </summary>
|
||||
public bool Enabled;
|
||||
|
||||
public ThreadedScheduler(string threadName = null, int runInterval = 50, bool startEnabled = true)
|
||||
{
|
||||
Enabled = startEnabled;
|
||||
|
||||
workerThread = new Thread(() =>
|
||||
{
|
||||
while (!isDisposed)
|
||||
{
|
||||
if (Enabled) Update();
|
||||
Thread.Sleep(runInterval);
|
||||
}
|
||||
})
|
||||
{
|
||||
IsBackground = true,
|
||||
Name = threadName
|
||||
};
|
||||
|
||||
workerThread.Start();
|
||||
}
|
||||
|
||||
#region IDisposable Support
|
||||
|
||||
~ThreadedScheduler()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
private bool isDisposed;
|
||||
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (!isDisposed)
|
||||
{
|
||||
isDisposed = true;
|
||||
workerThread.Join();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override bool IsMainThread => false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user