mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-11-24 22:42:53 +01:00
31 lines
1.0 KiB
C#
31 lines
1.0 KiB
C#
using System.Runtime.InteropServices;
|
|
|
|
namespace Phantom.Utils.Runtime;
|
|
|
|
public static class PosixSignals {
|
|
public static void RegisterCancellation(CancellationTokenSource cancellationTokenSource, Action? callback = null) {
|
|
var shutdown = new CancellationCallback(cancellationTokenSource, callback).Run;
|
|
PosixSignalRegistration.Create(PosixSignal.SIGINT, shutdown);
|
|
PosixSignalRegistration.Create(PosixSignal.SIGTERM, shutdown);
|
|
PosixSignalRegistration.Create(PosixSignal.SIGQUIT, shutdown);
|
|
}
|
|
|
|
private sealed class CancellationCallback {
|
|
private readonly CancellationTokenSource cancellationTokenSource;
|
|
private readonly Action? callback;
|
|
|
|
public CancellationCallback(CancellationTokenSource cancellationTokenSource, Action? callback) {
|
|
this.cancellationTokenSource = cancellationTokenSource;
|
|
this.callback = callback;
|
|
}
|
|
|
|
public void Run(PosixSignalContext context) {
|
|
context.Cancel = true;
|
|
if (!cancellationTokenSource.IsCancellationRequested) {
|
|
cancellationTokenSource.Cancel();
|
|
callback?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|