2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-13 14:51:15 -04:00
|
|
|
use crate::deno_error::permission_denied;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::global_state::ThreadSafeGlobalState;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::global_timer::GlobalTimer;
|
2019-06-09 09:08:20 -04:00
|
|
|
use crate::import_map::ImportMap;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::metrics::Metrics;
|
2019-10-01 18:51:05 -04:00
|
|
|
use crate::ops::JsonOp;
|
2019-11-14 12:10:25 -05:00
|
|
|
use crate::ops::MinimalOp;
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::permissions::DenoPermissions;
|
2020-01-21 03:49:47 -05:00
|
|
|
use crate::web_worker::WebWorker;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::worker::WorkerChannels;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::Buf;
|
|
|
|
use deno_core::CoreOp;
|
|
|
|
use deno_core::ErrBox;
|
|
|
|
use deno_core::Loader;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::Op;
|
|
|
|
use deno_core::PinnedBuf;
|
|
|
|
use deno_core::ResourceTable;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::channel::mpsc;
|
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::future::TryFutureExt;
|
2019-06-11 10:34:39 -04:00
|
|
|
use rand::rngs::StdRng;
|
|
|
|
use rand::SeedableRng;
|
2019-10-01 18:51:05 -04:00
|
|
|
use serde_json::Value;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std;
|
2019-04-01 15:09:59 -04:00
|
|
|
use std::collections::HashMap;
|
2019-04-09 13:11:25 -04:00
|
|
|
use std::ops::Deref;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2019-07-31 13:16:03 -04:00
|
|
|
use std::str;
|
2019-11-09 15:07:14 -05:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::sync::atomic::Ordering;
|
2019-04-09 13:11:25 -04:00
|
|
|
use std::sync::Arc;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::sync::Mutex;
|
2019-11-14 12:10:25 -05:00
|
|
|
use std::sync::MutexGuard;
|
2019-04-08 16:22:40 -04:00
|
|
|
use std::time::Instant;
|
2020-01-21 11:50:06 -05:00
|
|
|
use tokio::sync::Mutex as AsyncMutex;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2019-05-11 10:23:19 -04:00
|
|
|
/// Isolate cannot be passed between threads but ThreadSafeState can.
|
|
|
|
/// ThreadSafeState satisfies Send and Sync. So any state that needs to be
|
|
|
|
/// accessed outside the main V8 thread should be inside ThreadSafeState.
|
2019-04-09 13:11:25 -04:00
|
|
|
pub struct ThreadSafeState(Arc<State>);
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2019-04-09 13:11:25 -04:00
|
|
|
pub struct State {
|
2019-11-04 10:38:52 -05:00
|
|
|
pub global_state: ThreadSafeGlobalState,
|
2019-11-24 10:42:30 -05:00
|
|
|
pub permissions: Arc<Mutex<DenoPermissions>>,
|
2019-11-04 10:38:52 -05:00
|
|
|
pub main_module: Option<ModuleSpecifier>,
|
2020-01-21 11:50:06 -05:00
|
|
|
pub worker_channels: WorkerChannels,
|
2019-06-09 09:08:20 -04:00
|
|
|
/// When flags contains a `.import_map_path` option, the content of the
|
|
|
|
/// import map file will be resolved and set.
|
|
|
|
pub import_map: Option<ImportMap>,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub metrics: Metrics,
|
|
|
|
pub global_timer: Mutex<GlobalTimer>,
|
2020-01-21 03:49:47 -05:00
|
|
|
pub workers: Mutex<HashMap<u32, WebWorker>>,
|
2020-01-17 18:43:53 -05:00
|
|
|
pub loading_workers: Mutex<HashMap<u32, mpsc::Receiver<Result<(), ErrBox>>>>,
|
2019-11-09 15:07:14 -05:00
|
|
|
pub next_worker_id: AtomicUsize,
|
2019-04-08 16:22:40 -04:00
|
|
|
pub start_time: Instant,
|
2019-06-11 10:34:39 -04:00
|
|
|
pub seeded_rng: Option<Mutex<StdRng>>,
|
2019-11-14 12:10:25 -05:00
|
|
|
pub resource_table: Mutex<ResourceTable>,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 13:11:25 -04:00
|
|
|
impl Clone for ThreadSafeState {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ThreadSafeState(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for ThreadSafeState {
|
|
|
|
type Target = Arc<State>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 18:58:00 -04:00
|
|
|
impl ThreadSafeState {
|
2019-11-14 12:10:25 -05:00
|
|
|
pub fn lock_resource_table(&self) -> MutexGuard<ResourceTable> {
|
|
|
|
self.resource_table.lock().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:51:05 -04:00
|
|
|
/// Wrap core `OpDispatcher` to collect metrics.
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn core_op<D>(
|
2019-06-17 21:02:08 -04:00
|
|
|
&self,
|
2019-10-01 18:51:05 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&[u8], Option<PinnedBuf>) -> CoreOp
|
|
|
|
where
|
|
|
|
D: Fn(&[u8], Option<PinnedBuf>) -> CoreOp,
|
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
|
|
|
move |control: &[u8], zero_copy: Option<PinnedBuf>| -> CoreOp {
|
|
|
|
let bytes_sent_control = control.len();
|
|
|
|
let bytes_sent_zero_copy =
|
|
|
|
zero_copy.as_ref().map(|b| b.len()).unwrap_or(0);
|
|
|
|
|
|
|
|
let op = dispatcher(control, zero_copy);
|
|
|
|
state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);
|
|
|
|
|
|
|
|
match op {
|
|
|
|
Op::Sync(buf) => {
|
|
|
|
state.metrics_op_completed(buf.len());
|
|
|
|
Op::Sync(buf)
|
|
|
|
}
|
|
|
|
Op::Async(fut) => {
|
|
|
|
let state = state.clone();
|
2019-11-16 19:17:47 -05:00
|
|
|
let result_fut = fut.map_ok(move |buf: Buf| {
|
2019-12-23 09:59:44 -05:00
|
|
|
state.metrics_op_completed(buf.len());
|
2019-10-01 18:51:05 -04:00
|
|
|
buf
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
|
|
|
Op::Async(result_fut.boxed())
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
2020-01-21 12:01:10 -05:00
|
|
|
Op::AsyncUnref(fut) => {
|
|
|
|
let state = state.clone();
|
|
|
|
let result_fut = fut.map_ok(move |buf: Buf| {
|
|
|
|
state.metrics_op_completed(buf.len());
|
|
|
|
buf
|
|
|
|
});
|
|
|
|
Op::AsyncUnref(result_fut.boxed())
|
|
|
|
}
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 12:10:25 -05:00
|
|
|
/// This is a special function that provides `state` argument to dispatcher.
|
|
|
|
pub fn stateful_minimal_op<D>(
|
|
|
|
&self,
|
|
|
|
dispatcher: D,
|
2019-11-16 19:17:47 -05:00
|
|
|
) -> impl Fn(i32, Option<PinnedBuf>) -> Pin<Box<MinimalOp>>
|
2019-11-14 12:10:25 -05:00
|
|
|
where
|
2019-11-16 19:17:47 -05:00
|
|
|
D: Fn(&ThreadSafeState, i32, Option<PinnedBuf>) -> Pin<Box<MinimalOp>>,
|
2019-11-14 12:10:25 -05:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
move |rid: i32, zero_copy: Option<PinnedBuf>| -> Pin<Box<MinimalOp>> {
|
2019-11-14 12:10:25 -05:00
|
|
|
dispatcher(&state, rid, zero_copy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:51:05 -04:00
|
|
|
/// This is a special function that provides `state` argument to dispatcher.
|
|
|
|
///
|
|
|
|
/// NOTE: This only works with JSON dispatcher.
|
|
|
|
/// This is a band-aid for transition to `Isolate.register_op` API as most of our
|
|
|
|
/// ops require `state` argument.
|
|
|
|
pub fn stateful_op<D>(
|
|
|
|
&self,
|
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(Value, Option<PinnedBuf>) -> Result<JsonOp, ErrBox>
|
|
|
|
where
|
|
|
|
D: Fn(&ThreadSafeState, Value, Option<PinnedBuf>) -> Result<JsonOp, ErrBox>,
|
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
|
|
|
move |args: Value, zero_copy: Option<PinnedBuf>| -> Result<JsonOp, ErrBox> {
|
|
|
|
dispatcher(&state, args, zero_copy)
|
|
|
|
}
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:35:38 -04:00
|
|
|
impl Loader for ThreadSafeState {
|
2019-06-09 09:08:20 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-08-07 12:55:39 -04:00
|
|
|
is_main: bool,
|
2019-08-13 14:51:15 -04:00
|
|
|
is_dyn_import: bool,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
2019-08-07 12:55:39 -04:00
|
|
|
if !is_main {
|
2019-06-09 09:08:20 -04:00
|
|
|
if let Some(import_map) = &self.import_map {
|
2019-06-12 15:00:08 -04:00
|
|
|
let result = import_map.resolve(specifier, referrer)?;
|
2019-10-03 09:16:06 -04:00
|
|
|
if let Some(r) = result {
|
|
|
|
return Ok(r);
|
2019-06-09 09:08:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 14:51:15 -04:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_import(specifier, referrer)?;
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
if is_dyn_import {
|
|
|
|
self.check_dyn_import(&module_specifier)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(module_specifier)
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an absolute url, load its source code.
|
2019-06-12 19:55:59 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 09:33:23 -05:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-05 11:56:18 -05:00
|
|
|
) -> Pin<Box<deno_core::SourceCodeInfoFuture>> {
|
2019-06-05 16:35:38 -04:00
|
|
|
self.metrics.resolve_count.fetch_add(1, Ordering::SeqCst);
|
2019-08-07 12:55:39 -04:00
|
|
|
let module_url_specified = module_specifier.to_string();
|
2019-11-04 10:38:52 -05:00
|
|
|
let fut = self
|
|
|
|
.global_state
|
2019-11-25 09:33:23 -05:00
|
|
|
.fetch_compiled_module(module_specifier, maybe_referrer)
|
2020-01-05 11:56:18 -05:00
|
|
|
.map_ok(|compiled_module| deno_core::SourceCodeInfo {
|
2019-07-31 13:16:03 -04:00
|
|
|
// Real module name, might be different from initial specifier
|
|
|
|
// due to redirections.
|
|
|
|
code: compiled_module.code,
|
2019-08-07 12:55:39 -04:00
|
|
|
module_url_specified,
|
|
|
|
module_url_found: compiled_module.name,
|
2019-11-04 10:38:52 -05:00
|
|
|
});
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fut.boxed()
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:11:25 -04:00
|
|
|
impl ThreadSafeState {
|
2019-11-09 15:07:14 -05:00
|
|
|
pub fn create_channels() -> (WorkerChannels, WorkerChannels) {
|
|
|
|
let (in_tx, in_rx) = mpsc::channel::<Buf>(1);
|
|
|
|
let (out_tx, out_rx) = mpsc::channel::<Buf>(1);
|
2019-11-04 10:38:52 -05:00
|
|
|
let internal_channels = WorkerChannels {
|
2019-11-09 15:07:14 -05:00
|
|
|
sender: out_tx,
|
2020-01-21 11:50:06 -05:00
|
|
|
receiver: Arc::new(AsyncMutex::new(in_rx)),
|
2019-06-09 09:08:20 -04:00
|
|
|
};
|
2019-11-04 10:38:52 -05:00
|
|
|
let external_channels = WorkerChannels {
|
2019-11-09 15:07:14 -05:00
|
|
|
sender: in_tx,
|
2020-01-21 11:50:06 -05:00
|
|
|
receiver: Arc::new(AsyncMutex::new(out_rx)),
|
2019-07-31 13:16:03 -04:00
|
|
|
};
|
2019-11-09 15:07:14 -05:00
|
|
|
(internal_channels, external_channels)
|
|
|
|
}
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-11-09 15:07:14 -05:00
|
|
|
pub fn new(
|
|
|
|
global_state: ThreadSafeGlobalState,
|
2019-11-24 10:42:30 -05:00
|
|
|
// If Some(perm), use perm. Else copy from global_state.
|
|
|
|
shared_permissions: Option<Arc<Mutex<DenoPermissions>>>,
|
2019-11-09 15:07:14 -05:00
|
|
|
main_module: Option<ModuleSpecifier>,
|
|
|
|
internal_channels: WorkerChannels,
|
|
|
|
) -> Result<Self, ErrBox> {
|
2019-11-04 10:38:52 -05:00
|
|
|
let import_map: Option<ImportMap> =
|
|
|
|
match global_state.flags.import_map_path.as_ref() {
|
|
|
|
None => None,
|
|
|
|
Some(file_path) => Some(ImportMap::load(file_path)?),
|
|
|
|
};
|
2019-06-11 14:35:03 -04:00
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
let seeded_rng = match global_state.flags.seed {
|
|
|
|
Some(seed) => Some(Mutex::new(StdRng::seed_from_u64(seed))),
|
|
|
|
None => None,
|
2019-11-03 10:39:27 -05:00
|
|
|
};
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
let permissions = if let Some(perm) = shared_permissions {
|
|
|
|
perm
|
|
|
|
} else {
|
|
|
|
Arc::new(Mutex::new(global_state.permissions.clone()))
|
|
|
|
};
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2019-07-31 07:58:41 -04:00
|
|
|
let state = State {
|
2019-11-04 10:38:52 -05:00
|
|
|
global_state,
|
|
|
|
main_module,
|
|
|
|
permissions,
|
2019-06-09 09:08:20 -04:00
|
|
|
import_map,
|
2020-01-21 11:50:06 -05:00
|
|
|
worker_channels: internal_channels,
|
2019-11-04 10:38:52 -05:00
|
|
|
metrics: Metrics::default(),
|
2019-03-14 19:17:52 -04:00
|
|
|
global_timer: Mutex::new(GlobalTimer::new()),
|
2019-11-09 15:07:14 -05:00
|
|
|
workers: Mutex::new(HashMap::new()),
|
2020-01-17 18:43:53 -05:00
|
|
|
loading_workers: Mutex::new(HashMap::new()),
|
2019-11-09 15:07:14 -05:00
|
|
|
next_worker_id: AtomicUsize::new(0),
|
2019-04-08 16:22:40 -04:00
|
|
|
start_time: Instant::now(),
|
2019-06-11 10:34:39 -04:00
|
|
|
seeded_rng,
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2019-11-14 12:10:25 -05:00
|
|
|
resource_table: Mutex::new(ResourceTable::default()),
|
2019-07-31 07:58:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ThreadSafeState(Arc::new(state)))
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2020-01-21 03:49:47 -05:00
|
|
|
pub fn add_child_worker(&self, worker: WebWorker) -> u32 {
|
2019-11-09 15:07:14 -05:00
|
|
|
let worker_id = self.next_worker_id.fetch_add(1, Ordering::Relaxed) as u32;
|
|
|
|
let mut workers_tl = self.workers.lock().unwrap();
|
|
|
|
workers_tl.insert(worker_id, worker);
|
|
|
|
worker_id
|
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
#[inline]
|
2020-01-20 09:45:44 -05:00
|
|
|
pub fn check_read(&self, path: &Path) -> Result<(), ErrBox> {
|
|
|
|
self.permissions.lock().unwrap().check_read(path)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-20 09:45:44 -05:00
|
|
|
pub fn check_write(&self, path: &Path) -> Result<(), ErrBox> {
|
|
|
|
self.permissions.lock().unwrap().check_write(path)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn check_env(&self) -> Result<(), ErrBox> {
|
2019-11-24 10:42:30 -05:00
|
|
|
self.permissions.lock().unwrap().check_env()
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-10-23 10:19:27 -04:00
|
|
|
pub fn check_net(&self, hostname: &str, port: u16) -> Result<(), ErrBox> {
|
2019-11-24 10:42:30 -05:00
|
|
|
self.permissions.lock().unwrap().check_net(hostname, port)
|
2019-05-08 19:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-08-13 14:51:15 -04:00
|
|
|
pub fn check_net_url(&self, url: &url::Url) -> Result<(), ErrBox> {
|
2019-11-24 10:42:30 -05:00
|
|
|
self.permissions.lock().unwrap().check_net_url(url)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn check_run(&self) -> Result<(), ErrBox> {
|
2019-11-24 10:42:30 -05:00
|
|
|
self.permissions.lock().unwrap().check_run()
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-05 15:30:20 -05:00
|
|
|
#[inline]
|
2020-01-20 09:45:44 -05:00
|
|
|
pub fn check_plugin(&self, filename: &Path) -> Result<(), ErrBox> {
|
2019-12-05 15:30:20 -05:00
|
|
|
self.permissions.lock().unwrap().check_plugin(filename)
|
|
|
|
}
|
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
pub fn check_dyn_import(
|
2020-01-04 05:20:52 -05:00
|
|
|
&self,
|
2019-08-13 14:51:15 -04:00
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let u = module_specifier.as_url();
|
|
|
|
match u.scheme() {
|
|
|
|
"http" | "https" => {
|
|
|
|
self.check_net_url(u)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"file" => {
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = u
|
2019-08-13 14:51:15 -04:00
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
self.check_read(Path::new(&path))?;
|
2019-08-13 14:51:15 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(permission_denied()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg(test)]
|
2019-11-09 15:07:14 -05:00
|
|
|
pub fn mock(
|
|
|
|
argv: Vec<String>,
|
|
|
|
internal_channels: WorkerChannels,
|
|
|
|
) -> ThreadSafeState {
|
2019-11-04 10:38:52 -05:00
|
|
|
let module_specifier = if argv.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let module_specifier = ModuleSpecifier::resolve_url_or_path(&argv[0])
|
|
|
|
.expect("Invalid entry module");
|
|
|
|
Some(module_specifier)
|
|
|
|
};
|
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
ThreadSafeState::new(
|
2019-11-04 10:38:52 -05:00
|
|
|
ThreadSafeGlobalState::mock(argv),
|
2019-11-24 10:42:30 -05:00
|
|
|
None,
|
2019-11-04 10:38:52 -05:00
|
|
|
module_specifier,
|
2019-11-09 15:07:14 -05:00
|
|
|
internal_channels,
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.unwrap()
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metrics_op_dispatched(
|
|
|
|
&self,
|
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
|
|
|
) {
|
|
|
|
self.metrics.ops_dispatched.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_control
|
|
|
|
.fetch_add(bytes_sent_control, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_data
|
|
|
|
.fetch_add(bytes_sent_data, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metrics_op_completed(&self, bytes_received: usize) {
|
|
|
|
self.metrics.ops_completed.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_received
|
|
|
|
.fetch_add(bytes_received, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 13:11:25 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thread_safe() {
|
|
|
|
fn f<S: Send + Sync>(_: S) {}
|
2019-11-09 15:07:14 -05:00
|
|
|
let (int, _) = ThreadSafeState::create_channels();
|
|
|
|
f(ThreadSafeState::mock(
|
|
|
|
vec![String::from("./deno"), String::from("hello.js")],
|
|
|
|
int,
|
|
|
|
));
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|