Skip to content

Commit

Permalink
improve inter-process message events
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Feb 11, 2024
1 parent 0e05404 commit a08de57
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Project-Aurora/AuroraCommon/Data/MemorySharedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Common.Data;

public class MemorySharedArray<T> : SignaledMemoryObject, IEnumerable<T> where T : struct
public sealed class MemorySharedArray<T> : SignaledMemoryObject, IEnumerable<T> where T : struct
{
public int Count { get; }

Expand Down
2 changes: 1 addition & 1 deletion Project-Aurora/AuroraCommon/Data/MemorySharedStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Common.Data;

public class MemorySharedStruct<T> : SignaledMemoryObject where T : struct
public sealed class MemorySharedStruct<T> : SignaledMemoryObject where T : struct
{
private static readonly int ElementSize = Marshal.SizeOf(typeof(T));

Expand Down
29 changes: 17 additions & 12 deletions Project-Aurora/AuroraCommon/Utils/MemorySharedEventThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,24 @@ internal static void RemoveObject(SignaledMemoryObject o)
}
}

private class HandlesAndThread
private sealed class HandlesAndThread
{
private const int MaxHandles = 64;

private CancellationTokenSource _cancellation = new();
private CancellationTokenSource CancelToken
{
get => _cancellation;
set
{
var old = _cancellation;
_cancellation = value;
_handles[0] = value.Token.WaitHandle;
old.Cancel();
old.Dispose();
}
}

private Thread _thread = new(() => { });

private Action[] _actions = [() => { }];
Expand All @@ -41,7 +54,7 @@ private class HandlesAndThread

internal HandlesAndThread()
{
_handles = [_cancellation.Token.WaitHandle];
_handles = [CancelToken.Token.WaitHandle];
_thread.Start();
}

Expand Down Expand Up @@ -93,11 +106,7 @@ internal void AddToThread(SignaledMemoryObject o)
o.UpdateRequestedHandle
}).ToArray();

_cancellation.Cancel();
_thread.Join();

_cancellation = new CancellationTokenSource();
_handles[0] = _cancellation.Token.WaitHandle;
CancelToken = new CancellationTokenSource();
_thread = CreateThread();
_thread.Start();
}
Expand All @@ -118,11 +127,7 @@ internal void RemoveIfExists(SignaledMemoryObject o)
_actions = _actions.Where((_, i) => i != updatedHandle && i != requestedHandle).ToArray();
_handles = _handles.Where((_, i) => i != updatedHandle && i != requestedHandle).ToArray();

_cancellation.Cancel();
_thread.Join();

_cancellation = new CancellationTokenSource();
_handles[0] = _cancellation.Token.WaitHandle;
CancelToken = new CancellationTokenSource();
_thread = CreateThread();
_thread.Start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -35,43 +36,42 @@ private async void Control_DeviceManager_Loaded(object? sender, RoutedEventArgs
return;
}

await LoadDeviceManager();
await FirstInit();
}

private async Task LoadDeviceManager()
private async Task FirstInit()
{
var deviceConfig = await ConfigManager.LoadDeviceConfig();
var deviceManager = await _deviceManager;

await Task.Run(() => UpdateControls(deviceConfig)).ConfigureAwait(false);
await Task.Run(() => UpdateControls(deviceConfig, deviceManager.DeviceContainers)).ConfigureAwait(false);

var deviceManager = await _deviceManager;
deviceManager.DevicesUpdated += DeviceManagerOnDevicesUpdated();
Loaded += (_, _) => deviceManager.DevicesUpdated -= DeviceManagerOnDevicesUpdated();

Unloaded += (_, _) => deviceManager.DevicesUpdated -= DeviceManagerOnDevicesUpdated();
return;

EventHandler DeviceManagerOnDevicesUpdated()
EventHandler<DevicesUpdatedEventArgs> DeviceManagerOnDevicesUpdated()
{
return ManagerOnDevicesUpdated;

async void ManagerOnDevicesUpdated(object? o, EventArgs eventArgs)
async void ManagerOnDevicesUpdated(object? o, DevicesUpdatedEventArgs eventArgs)
{
await UpdateControls(deviceConfig);
await UpdateControls(deviceConfig, eventArgs.DeviceContainers);
}
}
}

private async Task UpdateControls(DeviceConfig deviceConfig)
private async Task UpdateControls(DeviceConfig deviceConfig, IEnumerable<DeviceContainer> eventArgs)
{
var deviceManager = await _deviceManager;
var isDeviceManagerUp = await deviceManager.IsDeviceManagerUp();
var deviceContainers = isDeviceManagerUp ? deviceManager.DeviceContainers : [];
Dispatcher.BeginInvoke(() =>
{
LstDevices.Children.Clear();
NoDevManTextBlock.Visibility = isDeviceManagerUp ? Visibility.Collapsed : Visibility.Visible;
});
foreach (var deviceContainer in deviceContainers)
foreach (var deviceContainer in eventArgs)
{
Dispatcher.BeginInvoke(() =>
{
Expand All @@ -86,11 +86,11 @@ private async Task UpdateControls(DeviceConfig deviceConfig)

if (!isDeviceManagerUp)
{
await WaitForDeviceManager(deviceConfig);
await WaitForDeviceManager();
}
}

private async Task WaitForDeviceManager(DeviceConfig deviceConfig)
private async Task WaitForDeviceManager()
{
var runningProcessMonitor = await ProcessesModule.RunningProcessMonitor;
runningProcessMonitor.ProcessStarted += RunningProcessMonitorOnRunningProcessesChanged;
Expand All @@ -105,8 +105,6 @@ async void RunningProcessMonitorOnRunningProcessesChanged(object? sender, Proces

await Task.Delay(1000); // wait for pipe
runningProcessMonitor.ProcessStarted -= RunningProcessMonitorOnRunningProcessesChanged;

await UpdateControls(deviceConfig);
}
}

Expand Down
13 changes: 9 additions & 4 deletions Project-Aurora/Project-Aurora/Devices/DeviceManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
Expand All @@ -16,6 +17,11 @@

namespace Aurora.Devices;

public sealed class DevicesUpdatedEventArgs(IEnumerable<DeviceContainer> deviceContainers) : EventArgs
{
public IEnumerable<DeviceContainer> DeviceContainers { get; } = deviceContainers;
}

public sealed class DeviceManager : IDisposable
{
public const string DeviceManagerProcess = "AuroraDeviceManager";
Expand All @@ -25,7 +31,7 @@ public sealed class DeviceManager : IDisposable

private bool _disposed;

public event EventHandler? DevicesUpdated;
public event EventHandler<DevicesUpdatedEventArgs>? DevicesUpdated;

public List<DeviceContainer> DeviceContainers { get; } = [];

Expand Down Expand Up @@ -93,7 +99,7 @@ private void UpdateDevices()
return new DeviceContainer(device, this);
}));

DevicesUpdated?.Invoke(this, EventArgs.Empty);
DevicesUpdated?.Invoke(this, new DevicesUpdatedEventArgs(DeviceContainers.ToImmutableList()));
}

private void StartDmProcess()
Expand All @@ -106,7 +112,6 @@ private void StartDmProcess()
};
_process = Process.Start(updaterProc);
_process?.WaitForExitAsync().ContinueWith(DeviceManagerClosed);
UpdateDevices();
}

private void DeviceManagerClosed(Task processTask)
Expand Down Expand Up @@ -150,7 +155,7 @@ public async Task ShutdownDevices()
process.Kill();
}
await process.WaitForExitAsync();
DevicesUpdated?.Invoke(this, EventArgs.Empty);
DevicesUpdated?.Invoke(this, new DevicesUpdatedEventArgs([]));
}
}

Expand Down

0 comments on commit a08de57

Please sign in to comment.