Mono Class Library: Mono.Unix NamespaceMono.Unix.UnixSignal Class |
See Also: UnixSignal Members
System.Threading.WaitHandle
Mono.Unix.UnixSignal
|
All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.
Unix signals are used (generally) for two things:
- Asynchronous alerts about events that occur within a process, such as illegal instruction (Mono.Unix.Native.Signum.SIGILL), floating point exception (Mono.Unix.Native.Signum.SIGFPE), bus error (Mono.Unix.Native.Signum.SIGBUS), invalid memory access (Mono.Unix.Native.Signum.SIGSEGV), I/O is possible (Mono.Unix.Native.Signum.SIGIO), etc.
- Asynchronous alerts of external "events", such as termanal hangup (Mono.Unix.Native.Signum.SIGHUP), keyboard interrupt (Mono.Unix.Native.Signum.SIGINT), broken pipe (Mono.Unix.Native.Signum.SIGPIPE), program-defined actions (Mono.Unix.Native.Signum.SIGUSR1, Mono.Unix.Native.Signum.SIGUSR2), child exit (Mono.Unix.Native.Signum.SIGCHLD), etc.
The key nature about signals is that they are asynchronous. In a single threaded program, when a signal occurs the executing thread is "hijacked" (no matter what it was previously doing) in order to execute a (optionally registered) signal handler, and then the thread resumes what it was previously doing (if possible; the signal handler may abort the process as part of its execution).
In a multi-threaded program, a thread is selected at random among the threads that can handle the signal, and that thread is hijacked to run the signal handler before continuing (while the other threads continue to execute in ignorance that a signal is occuring).
Tradtionally, Mono.Unix.Native.Stdlib.signal is used to register a signal handler for a signal, and the registered handler is invoked when the signal is generated. However, due to the "hijacking" nature of the signal handling process, what the signal handler could do is severely restricted: it must not invoke non-reentrant library functions, and can only modify global data and execute a limited set of system calls such as Mono.Unix.Native.Syscall.read and Mono.Unix.Native.Syscall.write.
Managed code throws an additional wrench in the works, as the platform invocation mechanism is neither reentrant nor signal safe. Consequently, managed code can not be safely and reliably used as a signal handler (as would traditionally be done with signal(2)), thus the Mono.Unix.UnixSignal type.
A Mono.Unix.UnixSignal instance represents the number of times that a Unix signal has been emitted. That's all. UnixSignal instances support:
- Accessing the number of times the Unix signal has been emitted via the UnixSignal.Count and UnixSignal.IsSet properties.
- Clearing the count with UnixSignal.Reset.
- Sleeping until a signal has been emitted via UnixSignal.WaitAny and UnixSignal.WaitOne.
In order to operate, this class uses an internal unmanaged signal handler to represent the count information. Consequently, the count will only be updated as long as no other mechanism is used to change the registered signal handler, such as Mono.Unix.Native.Stdlib.signal or Mono.Unix.Native.Stdlib.SetSignalAction. If you use Mono.Unix.Native.Stdlib.signal or Mono.Unix.Native.Stdlib.SetSignalAction for the same signal as a created UnixSignal instance, the UnixSignal instance will stop operating as documented. Don't Do That (TM).
There is currently a limit of 64 concurrent UnixSignal instances within a process.
[Edit]
Namespace: Mono.Unix
Assembly: Mono.Posix (in Mono.Posix.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0