-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
PosixSignalRegistration.cs
78 lines (68 loc) · 3.59 KB
/
PosixSignalRegistration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Runtime.Versioning;
namespace System.Runtime.InteropServices
{
/// <summary>Handles a <see cref="PosixSignal"/>.</summary>
public sealed partial class PosixSignalRegistration : IDisposable
{
/// <summary>The state associated with this registration.</summary>
/// <remarks>
/// This is separate from the registration instance so that this token may be stored
/// in a statically rooted table, with a finalizer on the registration able to remove it.
/// </remarks>
private Token? _token;
/// <summary>Registers a <paramref name="handler"/> that is invoked when the <paramref name="signal"/> occurs.</summary>
/// <param name="signal">The signal to register for.</param>
/// <param name="handler">The handler that gets invoked.</param>
/// <returns>A <see cref="PosixSignalRegistration"/> instance that can be disposed to unregister the handler.</returns>
/// <exception cref="ArgumentNullException"><paramref name="handler"/> is <see langword="null"/>.</exception>
/// <exception cref="PlatformNotSupportedException"><paramref name="signal"/> is not supported by the platform.</exception>
/// <exception cref="IOException">An error occurred while setting up the signal handling or while installing the handler for the specified signal.</exception>
/// <remarks>
/// Raw values can be provided for <paramref name="signal"/> on Unix by casting them to <see cref="PosixSignal"/>.
/// Default handling of the signal can be canceled through <see cref="PosixSignalContext.Cancel"/>.
/// <see cref="PosixSignal.SIGINT"/> and <see cref="PosixSignal.SIGQUIT"/> can be canceled on both
/// Windows and on Unix platforms; <see cref="PosixSignal.SIGTERM"/> can only be canceled on Unix.
/// On Unix, terminal configuration can be canceled for <see cref="PosixSignal.SIGCHLD"/> and <see cref="PosixSignal.SIGCONT"/>.
/// </remarks>
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public static PosixSignalRegistration Create(PosixSignal signal, Action<PosixSignalContext> handler)
{
ArgumentNullException.ThrowIfNull(handler);
return Register(signal, handler);
}
/// <summary>Initializes the registration to wrap the specified token.</summary>
private PosixSignalRegistration(Token token) => _token = token;
/// <summary>Unregister the handler.</summary>
public void Dispose()
{
Unregister();
GC.SuppressFinalize(this);
}
/// <summary>Unregister the handler.</summary>
~PosixSignalRegistration() => Unregister();
/// <summary>The state associated with a registration.</summary>
private sealed class Token
{
public Token(PosixSignal signal, Action<PosixSignalContext> handler)
{
Signal = signal;
Handler = handler;
}
public Token(PosixSignal signal, int sigNo, Action<PosixSignalContext> handler)
{
Signal = signal;
Handler = handler;
SigNo = sigNo;
}
public PosixSignal Signal { get; }
public Action<PosixSignalContext> Handler { get; }
public int SigNo { get; }
}
}
}