From cdba5ab6fc633606aaa6f95d0825832c3ac6fe5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 8 Feb 2020 20:34:31 +0100 Subject: refactor: rename ThreadSafeState, use RefCell for mutable state (#3931) * rename ThreadSafeState to State * State stores InnerState wrapped in Rc and RefCell --- cli/ops/process.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'cli/ops/process.rs') diff --git a/cli/ops/process.rs b/cli/ops/process.rs index 1ffd6b78e..e93bcbc0f 100644 --- a/cli/ops/process.rs +++ b/cli/ops/process.rs @@ -4,12 +4,11 @@ use super::io::StreamResource; use crate::deno_error::bad_resource; use crate::ops::json_op; use crate::signal::kill; -use crate::state::ThreadSafeState; +use crate::state::State; use deno_core::*; use futures; use futures::future::FutureExt; use futures::future::TryFutureExt; -use futures::task::SpawnExt; use std; use std::convert::From; use std::future::Future; @@ -22,7 +21,7 @@ use tokio::process::Command; #[cfg(unix)] use std::os::unix::process::ExitStatusExt; -pub fn init(i: &mut Isolate, s: &ThreadSafeState) { +pub fn init(i: &mut Isolate, s: &State) { i.register_op("run", s.core_op(json_op(s.stateful_op(op_run)))); i.register_op( "run_status", @@ -31,12 +30,10 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) { i.register_op("kill", s.core_op(json_op(s.stateful_op(op_kill)))); } -fn clone_file( - rid: u32, - state: &ThreadSafeState, -) -> Result { - let mut table = state.lock_resource_table(); - let repr = table +fn clone_file(rid: u32, state: &State) -> Result { + let mut state = state.borrow_mut(); + let repr = state + .resource_table .get_mut::(rid) .ok_or_else(bad_resource)?; let file = match repr { @@ -76,7 +73,7 @@ struct ChildResource { } fn op_run( - state: &ThreadSafeState, + state: &State, args: Value, _zero_copy: Option, ) -> Result { @@ -131,7 +128,8 @@ fn op_run( let mut child = c.spawn()?; let pid = child.id(); - let mut table = state_.lock_resource_table(); + let mut state = state_.borrow_mut(); + let table = &mut state.resource_table; let stdin_rid = match child.stdin.take() { Some(child_stdin) => { @@ -180,7 +178,7 @@ fn op_run( pub struct ChildStatus { rid: ResourceId, - state: ThreadSafeState, + state: State, } impl Future for ChildStatus { @@ -188,8 +186,9 @@ impl Future for ChildStatus { fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { let inner = self.get_mut(); - let mut table = inner.state.lock_resource_table(); - let child_resource = table + let mut state = inner.state.borrow_mut(); + let child_resource = state + .resource_table .get_mut::(inner.rid) .ok_or_else(bad_resource)?; let child = &mut child_resource.child; @@ -204,7 +203,7 @@ struct RunStatusArgs { } fn op_run_status( - state: &ThreadSafeState, + state: &State, args: Value, _zero_copy: Option, ) -> Result { @@ -239,10 +238,7 @@ fn op_run_status( })) }; - let pool = futures::executor::ThreadPool::new().unwrap(); - let handle = pool.spawn_with_handle(future).unwrap(); - - Ok(JsonOp::Async(handle.boxed())) + Ok(JsonOp::Async(future.boxed_local())) } #[derive(Deserialize)] @@ -252,7 +248,7 @@ struct KillArgs { } fn op_kill( - state: &ThreadSafeState, + state: &State, args: Value, _zero_copy: Option, ) -> Result { -- cgit v1.2.3