Skip to content

Commit

Permalink
change Timer to own thread class and fix sleep adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Jun 24, 2024
1 parent db9912b commit dbbb955
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
6 changes: 5 additions & 1 deletion Project-Aurora/AuroraCommon/Utils/SingleConcurrentThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ public void Trigger()

private void TriggerPool()
{
if (_worker.IsShuttingdown)
{
return;
}
// (_worker.CurrentWorkItemsCount == 0 || _worker.InUseThreads == 0) part wakes the worker when program freezes more than 1 sec
if (_worker.WaitingCallbacks <= 1 && (_worker.CurrentWorkItemsCount == 0 || _worker.InUseThreads == 0))
if (_worker.WaitingCallbacks <= 1 && (_worker.CurrentWorkItemsCount <= 1 || _worker.InUseThreads == 0))
{
_worker.QueueWorkItem(_updateAction);
}
Expand Down
38 changes: 14 additions & 24 deletions Project-Aurora/Project-Aurora/Profiles/LightingStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
using AuroraRgb.Settings;
using AuroraRgb.Settings.Layers;
using AuroraRgb.Utils;
using Common.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using AssemblyExtensions = AuroraRgb.Utils.AssemblyExtensions;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace AuroraRgb.Profiles;
Expand Down Expand Up @@ -81,6 +83,8 @@ public LightingStateManager(Task<PluginManager> pluginManager, Task<IpcListener?
_isOverlayActiveProfile = evt => evt.IsOverlayEnabled &&
Array.Exists(evt.Config.ProcessNames, processRunning);

_updateTimer = new SingleConcurrentThread("LightingStateManager", TimerUpdate);

bool ProcessRunning(string name) => _runningProcessMonitor.Result.IsProcessRunning(name);
}

Expand All @@ -102,7 +106,7 @@ public async Task Initialize()

// Register all layer types that are in the Aurora.Settings.Layers namespace.
// Do not register all that are inside the assembly since some are application-specific (e.g. minecraft health layer)
var layerTypes = from type in Assembly.GetExecutingAssembly().GetLoadableTypes()
var layerTypes = from type in AssemblyExtensions.GetLoadableTypes(Assembly.GetExecutingAssembly())
where type.GetInterfaces().Contains(typeof(ILayerHandler))
let name = type.Name.CamelCaseToSpaceCase()
let meta = type.GetCustomAttribute<LayerHandlerMetaAttribute>()
Expand Down Expand Up @@ -135,7 +139,7 @@ where type.GetInterfaces().Contains(typeof(ILayerHandler))

private static IEnumerable<Application> EnumerateDefaultApps()
{
return from type in Assembly.GetExecutingAssembly().GetLoadableTypes()
return from type in AssemblyExtensions.GetLoadableTypes(Assembly.GetExecutingAssembly())
where (type.BaseType == typeof(Application) || type.BaseType == typeof(GsiApplication)) && type != typeof(GenericApplication) && type != typeof(GsiApplication)
let inst = (Application)Activator.CreateInstance(type)
orderby inst.Config.Name
Expand Down Expand Up @@ -325,7 +329,7 @@ public bool RegisterLayer<T>() where T : ILayerHandler
return Events[value];
}

private Timer? _updateTimer;
private SingleConcurrentThread _updateTimer;

private long _nextProcessNameUpdate;
private long _currentTick;
Expand All @@ -339,29 +343,16 @@ public string? PreviewProfileKey {
}

private readonly Stopwatch _watch = new();

private readonly Semaphore _updateLock = new(1, 1);
private bool _locked;

public Task InitUpdate()
{
_watch.Start();
_updateTimer = new Timer(_ =>
{
TimerUpdate();
}, null, 0, Timeout.Infinite);
_updateTimer.Trigger();
return Task.CompletedTask;
}

private void TimerUpdate()
{
if (_locked)
{
return;
}
_updateLock.WaitOne();
_locked = true;

GC.WaitForPendingFinalizers();
if (Debugger.IsAttached)
{
Expand All @@ -384,11 +375,10 @@ private void TimerUpdate()
}
}
_currentTick += _watch.ElapsedMilliseconds;
_updateTimer?.Change(
Math.Max(Global.Configuration.UpdateDelay - _watch.ElapsedMilliseconds, Global.Configuration.UpdateDelay), Timeout.Infinite);
_watch.Reset();
_locked = false;
_updateLock.Release();
var millisecondsTimeout = Math.Max(Global.Configuration.UpdateDelay - _watch.ElapsedMilliseconds, 1);
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsTimeout));
_updateTimer.Trigger();
_watch.Restart();
}

private void UpdateProcess()
Expand Down Expand Up @@ -684,7 +674,7 @@ public void Dispose()
_initializeCancelSource.Cancel();
Task.WaitAll(_initTasks.ToArray());
_initializeCancelSource.Dispose();
_updateTimer?.Dispose();
_updateTimer?.Dispose(200);
_updateTimer = null;
foreach (var (_, lightEvent) in Events)
lightEvent.Dispose();
Expand All @@ -696,7 +686,7 @@ public async Task DisposeAsync()
await Task.WhenAll(_initTasks.ToArray());
_initializeCancelSource.Dispose();
if (_updateTimer != null)
await _updateTimer.DisposeAsync();
_updateTimer.Dispose(200);
_updateTimer = null;
foreach (var (_, lightEvent) in Events)
lightEvent.Dispose();
Expand Down

0 comments on commit dbbb955

Please sign in to comment.