mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Move static triggers array to signal.rs
This commit is contained in:
@@ -19,6 +19,8 @@ use crate::pyobject::{
|
||||
IdProtocol, ItemProtocol, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
|
||||
};
|
||||
use crate::scope::{NameProtocol, Scope};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use crate::stdlib::signal::check_signals;
|
||||
use crate::vm::VirtualMachine;
|
||||
use indexmap::IndexMap;
|
||||
use itertools::Itertools;
|
||||
@@ -163,7 +165,7 @@ impl Frame {
|
||||
/// Execute a single instruction.
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
fn execute_instruction(&self, vm: &VirtualMachine) -> FrameResult {
|
||||
vm.check_signals();
|
||||
check_signals(vm);
|
||||
let instruction = self.fetch_instruction();
|
||||
|
||||
flame_guard!(format!("Frame::execute_instruction({:?})", instruction));
|
||||
|
||||
@@ -39,7 +39,7 @@ mod os;
|
||||
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
|
||||
mod pwd;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod signal;
|
||||
pub mod signal;
|
||||
|
||||
use crate::pyobject::PyObjectRef;
|
||||
|
||||
|
||||
@@ -2,14 +2,31 @@ use crate::obj::objfunction::PyFunctionRef;
|
||||
use crate::obj::objint::PyIntRef;
|
||||
use crate::pyobject::PyObjectRef;
|
||||
use crate::pyobject::PyResult;
|
||||
use crate::vm::{VirtualMachine, TRIGGERS};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use num_traits::cast::ToPrimitive;
|
||||
|
||||
use nix::sys::signal;
|
||||
|
||||
// Signal triggers
|
||||
// TODO: 64
|
||||
const NSIG: usize = 10;
|
||||
|
||||
static mut TRIGGERS: [AtomicBool; NSIG] = [
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
];
|
||||
|
||||
extern "C" fn run_signal(signum: i32) {
|
||||
unsafe {
|
||||
TRIGGERS[signum as usize].store(true, Ordering::Relaxed);
|
||||
@@ -28,6 +45,18 @@ fn signal(signalnum: PyIntRef, handler: PyFunctionRef, vm: &VirtualMachine) -> P
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn check_signals(vm: &VirtualMachine) {
|
||||
for (signum, handler) in vm.signal_handlers.borrow().iter() {
|
||||
if *signum as usize >= NSIG {
|
||||
panic!("Signum bigger then NSIG");
|
||||
}
|
||||
let triggerd = unsafe { TRIGGERS[*signum as usize].swap(false, Ordering::Relaxed) };
|
||||
if triggerd {
|
||||
vm.invoke(handler.clone(), vec![]).expect("Test");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let ctx = &vm.ctx;
|
||||
|
||||
|
||||
30
vm/src/vm.rs
30
vm/src/vm.rs
@@ -9,7 +9,6 @@ use std::collections::hash_map::HashMap;
|
||||
use std::collections::hash_set::HashSet;
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
|
||||
use crate::builtins;
|
||||
@@ -43,23 +42,6 @@ use num_bigint::BigInt;
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
use rustpython_compiler::{compile, error::CompileError};
|
||||
|
||||
// Signal triggers
|
||||
// TODO: 64
|
||||
pub const NSIG: usize = 10;
|
||||
|
||||
pub static mut TRIGGERS: [AtomicBool; NSIG] = [
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
AtomicBool::new(false),
|
||||
];
|
||||
|
||||
// use objects::objects;
|
||||
|
||||
// Objects are live when they are on stack, or referenced by a name (for now)
|
||||
@@ -1163,18 +1145,6 @@ impl VirtualMachine {
|
||||
pub fn current_exception(&self) -> Option<PyObjectRef> {
|
||||
self.exceptions.borrow().last().cloned()
|
||||
}
|
||||
|
||||
pub fn check_signals(&self) {
|
||||
for (signum, handler) in self.signal_handlers.borrow().iter() {
|
||||
if *signum as usize >= NSIG {
|
||||
panic!("Signum bigger then NSIG");
|
||||
}
|
||||
let triggerd = unsafe { TRIGGERS[*signum as usize].swap(false, Ordering::Relaxed) };
|
||||
if triggerd {
|
||||
self.invoke(handler.clone(), vec![]).expect("Test");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for VirtualMachine {
|
||||
|
||||
Reference in New Issue
Block a user