From 2debbdacb935cfe1eb7bb8d1f40a5063b339d90b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 8 Apr 2019 17:10:00 -0400 Subject: Merge Worker and Isolate types (#2078) Reduces generics. --- cli/isolate_state.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'cli/isolate_state.rs') diff --git a/cli/isolate_state.rs b/cli/isolate_state.rs index 313f4f6ce..a672f5ee2 100644 --- a/cli/isolate_state.rs +++ b/cli/isolate_state.rs @@ -5,25 +5,23 @@ use crate::flags; use crate::global_timer::GlobalTimer; use crate::modules::Modules; use crate::permissions::DenoPermissions; +use crate::resources; use crate::resources::ResourceId; -use crate::workers::UserWorkerBehavior; -use crate::workers::Worker; +use crate::worker::Worker; use deno::Buf; use futures::future::Shared; -use futures::sync::mpsc as async_mpsc; use std; use std::collections::HashMap; use std::env; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Arc; use std::sync::Mutex; use std::time::Instant; +use tokio::sync::mpsc as async_mpsc; pub type WorkerSender = async_mpsc::Sender; pub type WorkerReceiver = async_mpsc::Receiver; pub type WorkerChannels = (WorkerSender, WorkerReceiver); -pub type UserWorkerTable = - HashMap>>; +pub type UserWorkerTable = HashMap>; // AtomicU64 is currently unstable #[derive(Default)] @@ -48,22 +46,23 @@ pub struct IsolateState { pub flags: flags::DenoFlags, pub metrics: Metrics, pub modules: Mutex, - pub worker_channels: Option>, + pub worker_channels: Mutex, pub global_timer: Mutex, pub workers: Mutex, - pub is_worker: bool, pub start_time: Instant, + pub resource: resources::Resource, } impl IsolateState { - pub fn new( - flags: flags::DenoFlags, - argv_rest: Vec, - worker_channels: Option, - is_worker: bool, - ) -> Self { + pub fn new(flags: flags::DenoFlags, argv_rest: Vec) -> Self { let custom_root = env::var("DENO_DIR").map(|s| s.into()).ok(); + let (worker_in_tx, worker_in_rx) = async_mpsc::channel::(1); + let (worker_out_tx, worker_out_rx) = async_mpsc::channel::(1); + let internal_channels = (worker_out_tx, worker_in_rx); + let external_channels = (worker_in_tx, worker_out_rx); + let resource = resources::add_worker(external_channels); + Self { dir: deno_dir::DenoDir::new(custom_root).unwrap(), argv: argv_rest, @@ -71,11 +70,11 @@ impl IsolateState { flags, metrics: Metrics::default(), modules: Mutex::new(Modules::new()), - worker_channels: worker_channels.map(Mutex::new), + worker_channels: Mutex::new(internal_channels), global_timer: Mutex::new(GlobalTimer::new()), workers: Mutex::new(UserWorkerTable::new()), - is_worker, start_time: Instant::now(), + resource, } } @@ -126,7 +125,7 @@ impl IsolateState { let argv = vec![String::from("./deno"), String::from("hello.js")]; // For debugging: argv.push_back(String::from("-D")); let (flags, rest_argv) = flags::set_flags(argv).unwrap(); - IsolateState::new(flags, rest_argv, None, false) + IsolateState::new(flags, rest_argv) } pub fn metrics_op_dispatched( @@ -153,8 +152,3 @@ impl IsolateState { .fetch_add(bytes_received, Ordering::SeqCst); } } - -/// Provides state getter function -pub trait IsolateStateContainer { - fn state(&self) -> Arc; -} -- cgit v1.2.3