|
| 1 | +use core::fmt::Debug; |
| 2 | + |
| 3 | +/// POSIX signals enumeration |
| 4 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 5 | +#[repr(u8)] |
| 6 | +pub enum Signal_type { |
| 7 | + /// Hangup detected on controlling terminal or death of controlling process (SIGHUP) |
| 8 | + Hangup = 0, |
| 9 | + /// Interrupt from keyboard (SIGINT) |
| 10 | + Interrupt, |
| 11 | + /// Quit from keyboard (SIGQUIT) |
| 12 | + Quit, |
| 13 | + /// Unused : Illegal Instruction (SIGILL) |
| 14 | + Illegal_instruction, |
| 15 | + /// Trace/breakpoint trap (SIGTRAP) |
| 16 | + Trap, |
| 17 | + /// Abort signal from abort(3) (SIGABRT) |
| 18 | + Abort, |
| 19 | + /// Bus error (bad memory access) (SIGBUS) |
| 20 | + Bus_error, |
| 21 | + /// Floating-point exception (SIGFPE) |
| 22 | + Floating_point_exception, |
| 23 | + /// Kill signal (SIGKILL) |
| 24 | + Kill, |
| 25 | + /// User-defined signal 1 (SIGUSR1) |
| 26 | + User_1, |
| 27 | + /// Invalid memory reference (SIGSEGV) |
| 28 | + Segmentation_fault, |
| 29 | + /// User-defined signal 2 (SIGUSR2) |
| 30 | + User_2, |
| 31 | + /// Broken pipe: write to pipe with no readers (SIGPIPE) |
| 32 | + Broken_pipe, |
| 33 | + /// Timer signal from alarm(2) (SIGALRM) |
| 34 | + Alarm, |
| 35 | + /// Termination signal (SIGTERM) |
| 36 | + Termination, |
| 37 | + /// Stack fault on coprocessor (unused) (SIGSTKFLT) |
| 38 | + Stack_fault, |
| 39 | + /// Child stopped or terminated (SIGCHLD) |
| 40 | + Child, |
| 41 | + /// Continue if stopped (SIGCONT) |
| 42 | + Continue, |
| 43 | + /// Stop process (SIGSTOP) |
| 44 | + Stop, |
| 45 | + /// Stop typed at terminal (SIGTSTP) |
| 46 | + Terminal_stop, |
| 47 | + /// Terminal input for background process (SIGTTIN) |
| 48 | + Terminal_input, |
| 49 | + /// Terminal output for background process (SIGTTOU) |
| 50 | + Terminal_output, |
| 51 | + /// Urgent condition on socket (4.2BSD) (SIGURG) |
| 52 | + Urgent, |
| 53 | + /// CPU time limit exceeded (4.2BSD) (SIGXCPU) |
| 54 | + Cpu_time_limit_exceeded, |
| 55 | + /// File size limit exceeded (4.2BSD) (SIGXFSZ) |
| 56 | + File_size_limit_exceeded, |
| 57 | + /// Virtual alarm clock (4.2BSD) (SIGVTALRM) |
| 58 | + Virtual_alarm, |
| 59 | + /// Profiling timer expired (SIGPROF) |
| 60 | + Profiling_timer_expired, |
| 61 | + /// Window resize signal (4.3BSD, Sun) (SIGWINCH) |
| 62 | + Window_resize, |
| 63 | + /// I/O now possible (4.2BSD) (SIGIO) |
| 64 | + IO_Possible, |
| 65 | + /// Power failure (System V) (SIGPWR) |
| 66 | + Power_failure, |
| 67 | + /// Bad system call (SVr4) (SIGSYS) |
| 68 | + Bad_system_call, |
| 69 | +} |
| 70 | + |
| 71 | +impl Signal_type { |
| 72 | + pub const First: Self = Self::Hangup; |
| 73 | + pub const Last: Self = Self::Bad_system_call; |
| 74 | + |
| 75 | + pub const fn Get_discriminant(&self) -> u8 { |
| 76 | + *self as u8 |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[derive(Debug, Copy, Clone)] |
| 81 | +#[repr(transparent)] |
| 82 | +pub struct Signal_accumulator_type { |
| 83 | + Accumulator: u32, |
| 84 | +} |
| 85 | + |
| 86 | +impl Signal_accumulator_type { |
| 87 | + pub const fn New() -> Self { |
| 88 | + Self { Accumulator: 0 } |
| 89 | + } |
| 90 | + |
| 91 | + pub const fn Send(&mut self, Signal: Signal_type) { |
| 92 | + self.Accumulator |= 1 << Signal as u32; |
| 93 | + } |
| 94 | + |
| 95 | + pub const fn Clear(&mut self, Signal: Signal_type) { |
| 96 | + self.Accumulator &= !(1 << Signal as u32); |
| 97 | + } |
| 98 | + |
| 99 | + pub const fn Has_signal(&self, Signal: Signal_type) -> bool { |
| 100 | + self.Accumulator & (1 << Signal as u32) != 0 |
| 101 | + } |
| 102 | + |
| 103 | + pub fn Peek(&self) -> Option<Signal_type> { |
| 104 | + for Bit in Signal_type::First as u8..=Signal_type::Last as u8 { |
| 105 | + let Signal = unsafe { core::mem::transmute::<u8, Signal_type>(Bit) }; |
| 106 | + |
| 107 | + if self.Has_signal(Signal) { |
| 108 | + return Some(Signal); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + None |
| 113 | + } |
| 114 | + |
| 115 | + pub fn Pop(&mut self) -> Option<Signal_type> { |
| 116 | + if let Some(Signal) = self.Peek() { |
| 117 | + self.Clear(Signal); |
| 118 | + |
| 119 | + Some(Signal) |
| 120 | + } else { |
| 121 | + None |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(test)] |
| 127 | +mod Tests { |
| 128 | + use super::*; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn Test_send_and_has_signal() { |
| 132 | + let mut acc = Signal_accumulator_type::New(); |
| 133 | + acc.Send(Signal_type::Interrupt); |
| 134 | + assert!(acc.Has_signal(Signal_type::Interrupt)); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn Test_clear_signal() { |
| 139 | + let mut acc = Signal_accumulator_type::New(); |
| 140 | + acc.Send(Signal_type::Quit); |
| 141 | + acc.Clear(Signal_type::Quit); |
| 142 | + assert!(!acc.Has_signal(Signal_type::Quit)); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn Test_peek_and_pop() { |
| 147 | + let mut acc = Signal_accumulator_type::New(); |
| 148 | + acc.Send(Signal_type::Hangup); |
| 149 | + acc.Send(Signal_type::User_1); |
| 150 | + assert_eq!(acc.Peek(), Some(Signal_type::Hangup)); |
| 151 | + assert_eq!(acc.Pop(), Some(Signal_type::Hangup)); |
| 152 | + assert_eq!(acc.Peek(), Some(Signal_type::User_1)); |
| 153 | + assert_eq!(acc.Pop(), Some(Signal_type::User_1)); |
| 154 | + assert_eq!(acc.Pop(), None); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn Test_signal_discriminant() { |
| 159 | + assert_eq!( |
| 160 | + Signal_type::Power_failure.Get_discriminant(), |
| 161 | + Signal_type::Power_failure as u8 |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments