Remove ThreadedScheduler

This commit is contained in:
Dean Herbert
2018-03-19 18:13:45 +09:00
parent 0a145d56be
commit f084650c5e
3 changed files with 44 additions and 77 deletions

View File

@@ -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">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue">&lt;Policy Inspect="False" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalFunctions/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb"&gt;&lt;ExtraRule Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>

View File

@@ -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;

View File

@@ -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;
}
}