fix deadlock when log getting pid

This commit is contained in:
WangRunji 2019-04-12 13:42:19 +08:00
parent 3defc8b9aa
commit 6d432b0a03
3 changed files with 12 additions and 2 deletions

@ -1,5 +1,5 @@
pub use self::process::{Status, IDLE_PROCESS}; pub use self::process::{Status, IDLE_PROCESS};
pub use self::task::{get_current, run_task}; pub use self::task::{get_current, run_task, current_pid};
pub use self::process_table::{get}; pub use self::process_table::{get};
pub use self::exit::{do_exit, do_wait4, ChildProcessFilter}; pub use self::exit::{do_exit, do_wait4, ChildProcessFilter};
pub use self::spawn::{do_spawn, FileAction}; pub use self::spawn::{do_spawn, FileAction};

@ -68,6 +68,12 @@ thread_local! {
static _CURRENT_PROCESS_PTR: Cell<*const SgxMutex<Process>> = { static _CURRENT_PROCESS_PTR: Cell<*const SgxMutex<Process>> = {
Cell::new(0 as *const SgxMutex<Process>) Cell::new(0 as *const SgxMutex<Process>)
}; };
// for log getting pid without locking process
static _PID: Cell<pid_t> = Cell::new(0);
}
pub fn current_pid() -> pid_t {
_PID.with(|p| p.get())
} }
pub fn get_current() -> ProcessRef { pub fn get_current() -> ProcessRef {
@ -81,6 +87,9 @@ pub fn get_current() -> ProcessRef {
} }
fn set_current(process: &ProcessRef) { fn set_current(process: &ProcessRef) {
let pid = process.lock().unwrap().get_pid();
_PID.with(|p| p.set(pid));
let process_ref_clone = process.clone(); let process_ref_clone = process.clone();
let process_ptr = Arc::into_raw(process_ref_clone); let process_ptr = Arc::into_raw(process_ref_clone);
@ -90,6 +99,7 @@ fn set_current(process: &ProcessRef) {
} }
fn reset_current() { fn reset_current() {
_PID.with(|p| p.set(0));
let mut process_ptr = _CURRENT_PROCESS_PTR.with(|cp| cp.replace(0 as *const SgxMutex<Process>)); let mut process_ptr = _CURRENT_PROCESS_PTR.with(|cp| cp.replace(0 as *const SgxMutex<Process>));
// Prevent memory leakage // Prevent memory leakage

@ -26,7 +26,7 @@ impl Log for SimpleLogger {
"\u{1B}[{}m[{:>5}][{}] {}\u{1B}[0m", "\u{1B}[{}m[{:>5}][{}] {}\u{1B}[0m",
color as u8, color as u8,
record.level(), record.level(),
crate::process::do_getpid(), crate::process::current_pid(),
record.args() record.args()
); );
} }