1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-04 08:54:20 -05:00
denoland-deno/cli/ops/signal.rs

140 lines
3.5 KiB
Rust
Raw Normal View History

2020-01-24 08:15:31 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{JsonOp, Value};
use crate::op_error::OpError;
use crate::state::State;
use deno_core::CoreIsolate;
use deno_core::ZeroCopyBuf;
2020-01-24 08:15:31 -05:00
#[cfg(unix)]
use super::dispatch_json::Deserialize;
#[cfg(unix)]
use futures::future::{poll_fn, FutureExt};
#[cfg(unix)]
use std::task::Waker;
#[cfg(unix)]
use tokio::signal::unix::{signal, Signal, SignalKind};
pub fn init(i: &mut CoreIsolate, s: &State) {
i.register_op("op_signal_bind", s.stateful_json_op2(op_signal_bind));
i.register_op("op_signal_unbind", s.stateful_json_op2(op_signal_unbind));
i.register_op("op_signal_poll", s.stateful_json_op2(op_signal_poll));
2020-01-24 08:15:31 -05:00
}
#[cfg(unix)]
/// The resource for signal stream.
/// The second element is the waker of polling future.
pub struct SignalStreamResource(pub Signal, pub Option<Waker>);
#[cfg(unix)]
#[derive(Deserialize)]
struct BindSignalArgs {
signo: i32,
}
#[cfg(unix)]
#[derive(Deserialize)]
struct SignalArgs {
rid: i32,
}
#[cfg(unix)]
fn op_signal_bind(
isolate: &mut CoreIsolate,
_state: &State,
2020-01-24 08:15:31 -05:00
args: Value,
2020-01-24 15:10:49 -05:00
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
2020-01-24 08:15:31 -05:00
let args: BindSignalArgs = serde_json::from_value(args)?;
let mut resource_table = isolate.resource_table.borrow_mut();
let rid = resource_table.add(
2020-01-24 08:15:31 -05:00
"signal",
Box::new(SignalStreamResource(
signal(SignalKind::from_raw(args.signo)).expect(""),
None,
)),
);
Ok(JsonOp::Sync(json!({
"rid": rid,
})))
}
#[cfg(unix)]
fn op_signal_poll(
isolate: &mut CoreIsolate,
_state: &State,
2020-01-24 08:15:31 -05:00
args: Value,
2020-01-24 15:10:49 -05:00
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
2020-01-24 08:15:31 -05:00
let args: SignalArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let resource_table = isolate.resource_table.clone();
2020-01-24 08:15:31 -05:00
let future = poll_fn(move |cx| {
let mut resource_table = resource_table.borrow_mut();
if let Some(mut signal) =
resource_table.get_mut::<SignalStreamResource>(rid)
{
2020-01-24 08:15:31 -05:00
signal.1 = Some(cx.waker().clone());
return signal.0.poll_recv(cx);
}
std::task::Poll::Ready(None)
})
.then(|result| async move { Ok(json!({ "done": result.is_none() })) });
Ok(JsonOp::AsyncUnref(future.boxed_local()))
2020-01-24 08:15:31 -05:00
}
#[cfg(unix)]
pub fn op_signal_unbind(
isolate: &mut CoreIsolate,
_state: &State,
2020-01-24 08:15:31 -05:00
args: Value,
2020-01-24 15:10:49 -05:00
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
2020-01-24 08:15:31 -05:00
let args: SignalArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let mut resource_table = isolate.resource_table.borrow_mut();
let resource = resource_table.get::<SignalStreamResource>(rid);
2020-01-24 08:15:31 -05:00
if let Some(signal) = resource {
if let Some(waker) = &signal.1 {
// Wakes up the pending poll if exists.
// This prevents the poll future from getting stuck forever.
waker.clone().wake();
}
}
resource_table
.close(rid)
.ok_or_else(OpError::bad_resource_id)?;
2020-01-24 08:15:31 -05:00
Ok(JsonOp::Sync(json!({})))
}
#[cfg(not(unix))]
pub fn op_signal_bind(
_isolate: &mut CoreIsolate,
_state: &State,
2020-01-24 08:15:31 -05:00
_args: Value,
2020-01-24 15:10:49 -05:00
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
2020-01-24 08:15:31 -05:00
unimplemented!();
}
#[cfg(not(unix))]
fn op_signal_unbind(
_isolate: &mut CoreIsolate,
_state: &State,
2020-01-24 08:15:31 -05:00
_args: Value,
2020-01-24 15:10:49 -05:00
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
2020-01-24 08:15:31 -05:00
unimplemented!();
}
#[cfg(not(unix))]
fn op_signal_poll(
_isolate: &mut CoreIsolate,
_state: &State,
2020-01-24 08:15:31 -05:00
_args: Value,
2020-01-24 15:10:49 -05:00
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
2020-01-24 08:15:31 -05:00
unimplemented!();
}