From f540d31a1b2a5acece3d765b93b887049160726a Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Mon, 29 Jul 2019 18:46:51 +0300 Subject: [PATCH] Move static triggers array to signal.rs --- vm/src/frame.rs | 4 +++- vm/src/stdlib/mod.rs | 2 +- vm/src/stdlib/signal.rs | 33 +++++++++++++++++++++++++++++++-- vm/src/vm.rs | 30 ------------------------------ 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 969327b11..0b67df1dc 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -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)); diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs index ae0f0580a..4e39081a0 100644 --- a/vm/src/stdlib/mod.rs +++ b/vm/src/stdlib/mod.rs @@ -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; diff --git a/vm/src/stdlib/signal.rs b/vm/src/stdlib/signal.rs index bd054fe97..8a8604327 100644 --- a/vm/src/stdlib/signal.rs +++ b/vm/src/stdlib/signal.rs @@ -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; diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 15978d4c0..bbfea7fb6 100644 --- a/vm/src/vm.rs +++ b/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 { 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 {