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/signal.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'cli/ops/signal.rs') diff --git a/cli/ops/signal.rs b/cli/ops/signal.rs index f815cb57c..8d70f9fe8 100644 --- a/cli/ops/signal.rs +++ b/cli/ops/signal.rs @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{JsonOp, Value}; use crate::ops::json_op; -use crate::state::ThreadSafeState; +use crate::state::State; use deno_core::*; #[cfg(unix)] @@ -17,7 +17,7 @@ use std::task::Waker; #[cfg(unix)] use tokio::signal::unix::{signal, Signal, SignalKind}; -pub fn init(i: &mut Isolate, s: &ThreadSafeState) { +pub fn init(i: &mut Isolate, s: &State) { i.register_op( "signal_bind", s.core_op(json_op(s.stateful_op(op_signal_bind))), @@ -51,13 +51,13 @@ struct SignalArgs { #[cfg(unix)] fn op_signal_bind( - state: &ThreadSafeState, + state: &State, args: Value, _zero_copy: Option, ) -> Result { let args: BindSignalArgs = serde_json::from_value(args)?; - let mut table = state.lock_resource_table(); - let rid = table.add( + let mut state = state.borrow_mut(); + let rid = state.resource_table.add( "signal", Box::new(SignalStreamResource( signal(SignalKind::from_raw(args.signo)).expect(""), @@ -71,7 +71,7 @@ fn op_signal_bind( #[cfg(unix)] fn op_signal_poll( - state: &ThreadSafeState, + state: &State, args: Value, _zero_copy: Option, ) -> Result { @@ -80,8 +80,10 @@ fn op_signal_poll( let state_ = state.clone(); let future = poll_fn(move |cx| { - let mut table = state_.lock_resource_table(); - if let Some(mut signal) = table.get_mut::(rid) { + let mut state = state_.borrow_mut(); + if let Some(mut signal) = + state.resource_table.get_mut::(rid) + { signal.1 = Some(cx.waker().clone()); return signal.0.poll_recv(cx); } @@ -94,14 +96,14 @@ fn op_signal_poll( #[cfg(unix)] pub fn op_signal_unbind( - state: &ThreadSafeState, + state: &State, args: Value, _zero_copy: Option, ) -> Result { let args: SignalArgs = serde_json::from_value(args)?; let rid = args.rid as u32; - let mut table = state.lock_resource_table(); - let resource = table.get::(rid); + let mut state = state.borrow_mut(); + let resource = state.resource_table.get::(rid); if let Some(signal) = resource { if let Some(waker) = &signal.1 { // Wakes up the pending poll if exists. @@ -109,13 +111,13 @@ pub fn op_signal_unbind( waker.clone().wake(); } } - table.close(rid).ok_or_else(bad_resource)?; + state.resource_table.close(rid).ok_or_else(bad_resource)?; Ok(JsonOp::Sync(json!({}))) } #[cfg(not(unix))] pub fn op_signal_bind( - _state: &ThreadSafeState, + _state: &State, _args: Value, _zero_copy: Option, ) -> Result { @@ -124,7 +126,7 @@ pub fn op_signal_bind( #[cfg(not(unix))] fn op_signal_unbind( - _state: &ThreadSafeState, + _state: &State, _args: Value, _zero_copy: Option, ) -> Result { @@ -133,7 +135,7 @@ fn op_signal_unbind( #[cfg(not(unix))] fn op_signal_poll( - _state: &ThreadSafeState, + _state: &State, _args: Value, _zero_copy: Option, ) -> Result { -- cgit v1.2.3