rt: move signal handlers under a RwLock
This commit is contained in:
parent
1e96666fda
commit
33fb12db59
@ -7,6 +7,8 @@ use abi::{
|
||||
process::{ExitCode, ProcessOption, Signal, SignalEntryData, ThreadOption},
|
||||
};
|
||||
|
||||
use crate::sync::rwlock::RwLock;
|
||||
|
||||
/// Describes how a signal should be handled
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum SignalHandler {
|
||||
@ -20,9 +22,11 @@ pub enum SignalHandler {
|
||||
C(unsafe extern "C" fn(c_int)),
|
||||
}
|
||||
|
||||
// TODO RwLock here
|
||||
unsafe impl Sync for SignalHandler {}
|
||||
|
||||
const MAX_SIGNALS: usize = 16;
|
||||
static mut TABLE: [SignalHandler; MAX_SIGNALS] = [const { SignalHandler::Terminate }; MAX_SIGNALS];
|
||||
static TABLE: RwLock<[SignalHandler; MAX_SIGNALS]> =
|
||||
RwLock::new([const { SignalHandler::Terminate }; MAX_SIGNALS]);
|
||||
|
||||
// TODO remove unused for i686
|
||||
#[allow(unused)]
|
||||
@ -33,7 +37,7 @@ unsafe extern "C" fn common_signal_entry(data: &SignalEntryData) -> ! {
|
||||
terminate_by_signal(data.signal);
|
||||
}
|
||||
|
||||
match TABLE[index] {
|
||||
match TABLE.read()[index] {
|
||||
SignalHandler::Rust(function) => function(data.signal),
|
||||
SignalHandler::C(function) => {
|
||||
let signum = data.signal.into_raw() as i32;
|
||||
@ -59,7 +63,8 @@ fn terminate_by_signal(signal: Signal) -> ! {
|
||||
/// Marked as unsafe due to being thread-unsafe. Will be lifted once I port RwLock into the runtime
|
||||
/// crate.
|
||||
pub unsafe fn set_handler(signal: Signal, handler: SignalHandler) -> SignalHandler {
|
||||
let entry = &mut TABLE[signal.into_raw() as usize];
|
||||
let mut table = TABLE.write();
|
||||
let entry = &mut table[signal.into_raw() as usize];
|
||||
core::mem::replace(entry, handler)
|
||||
}
|
||||
|
||||
|
@ -179,3 +179,5 @@ impl<T> Drop for RwLockWriteGuard<'_, T> {
|
||||
unsafe { self.lock.release_write() };
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T: Sync> Sync for RwLock<T> {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user