mirror of
https://github.com/servo/servo.git
synced 2025-08-26 23:58:20 +01:00
Use GenericChannel for script_chan (#38645)
Motivation: Using our GenericChannel abstraction allows us to optimize IPC in single-process mode to just use cross-beam channel. To keep the diff low, and get early feedback, this PR only tackles a single channel, but the intention is to port all ipc channels to the generic channel, which allows us to skip serializing and deserializing messages in single process mode. Based on: - https://github.com/servo/servo/pull/38638 - https://github.com/servo/servo/pull/38636 Testing: Covered by existing tests --------- Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
parent
73e0f2f7e6
commit
8587536755
12 changed files with 36 additions and 22 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -727,6 +727,7 @@ dependencies = [
|
||||||
"malloc_size_of_derive",
|
"malloc_size_of_derive",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"serde",
|
"serde",
|
||||||
|
"servo_config",
|
||||||
"servo_malloc_size_of",
|
"servo_malloc_size_of",
|
||||||
"time",
|
"time",
|
||||||
"webrender_api",
|
"webrender_api",
|
||||||
|
|
|
@ -11,15 +11,15 @@ use std::marker::PhantomData;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
use base::generic_channel::GenericSender;
|
||||||
use ipc_channel::Error;
|
use ipc_channel::Error;
|
||||||
use ipc_channel::ipc::IpcSender;
|
|
||||||
use script_traits::ScriptThreadMessage;
|
use script_traits::ScriptThreadMessage;
|
||||||
|
|
||||||
static CURRENT_EVENT_LOOP_ID: AtomicUsize = AtomicUsize::new(0);
|
static CURRENT_EVENT_LOOP_ID: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#event-loop>
|
/// <https://html.spec.whatwg.org/multipage/#event-loop>
|
||||||
pub struct EventLoop {
|
pub struct EventLoop {
|
||||||
script_chan: IpcSender<ScriptThreadMessage>,
|
script_chan: GenericSender<ScriptThreadMessage>,
|
||||||
dont_send_or_sync: PhantomData<Rc<()>>,
|
dont_send_or_sync: PhantomData<Rc<()>>,
|
||||||
id: usize,
|
id: usize,
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ impl Drop for EventLoop {
|
||||||
|
|
||||||
impl EventLoop {
|
impl EventLoop {
|
||||||
/// Create a new event loop from the channel to its script thread.
|
/// Create a new event loop from the channel to its script thread.
|
||||||
pub fn new(script_chan: IpcSender<ScriptThreadMessage>) -> Rc<EventLoop> {
|
pub fn new(script_chan: GenericSender<ScriptThreadMessage>) -> Rc<EventLoop> {
|
||||||
let id = CURRENT_EVENT_LOOP_ID.fetch_add(1, Ordering::Relaxed);
|
let id = CURRENT_EVENT_LOOP_ID.fetch_add(1, Ordering::Relaxed);
|
||||||
Rc::new(EventLoop {
|
Rc::new(EventLoop {
|
||||||
script_chan,
|
script_chan,
|
||||||
|
@ -57,6 +57,8 @@ impl EventLoop {
|
||||||
|
|
||||||
/// Send a message to the event loop.
|
/// Send a message to the event loop.
|
||||||
pub fn send(&self, msg: ScriptThreadMessage) -> Result<(), Error> {
|
pub fn send(&self, msg: ScriptThreadMessage) -> Result<(), Error> {
|
||||||
self.script_chan.send(msg)
|
self.script_chan
|
||||||
|
.send(msg)
|
||||||
|
.map_err(|_err| Box::new(ipc_channel::ErrorKind::Custom("SendError".into())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ use background_hang_monitor_api::{
|
||||||
BackgroundHangMonitorControlMsg, BackgroundHangMonitorRegister, HangMonitorAlert,
|
BackgroundHangMonitorControlMsg, BackgroundHangMonitorRegister, HangMonitorAlert,
|
||||||
};
|
};
|
||||||
use base::Epoch;
|
use base::Epoch;
|
||||||
|
use base::generic_channel::{GenericReceiver, GenericSender};
|
||||||
use base::id::{
|
use base::id::{
|
||||||
BrowsingContextId, HistoryStateId, PipelineId, PipelineNamespace, PipelineNamespaceId,
|
BrowsingContextId, HistoryStateId, PipelineId, PipelineNamespace, PipelineNamespaceId,
|
||||||
PipelineNamespaceRequest, WebViewId,
|
PipelineNamespaceRequest, WebViewId,
|
||||||
|
@ -239,7 +240,8 @@ impl Pipeline {
|
||||||
(script_chan, (None, None, None))
|
(script_chan, (None, None, None))
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
let (script_chan, script_port) = ipc::channel().expect("Pipeline script chan");
|
let (script_chan, script_port) =
|
||||||
|
base::generic_channel::channel().expect("Pipeline script chan");
|
||||||
|
|
||||||
// Route messages coming from content to devtools as appropriate.
|
// Route messages coming from content to devtools as appropriate.
|
||||||
let script_to_devtools_ipc_sender =
|
let script_to_devtools_ipc_sender =
|
||||||
|
@ -482,9 +484,9 @@ pub struct UnprivilegedPipelineContent {
|
||||||
mem_profiler_chan: profile_mem::ProfilerChan,
|
mem_profiler_chan: profile_mem::ProfilerChan,
|
||||||
viewport_details: ViewportDetails,
|
viewport_details: ViewportDetails,
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
script_chan: IpcSender<ScriptThreadMessage>,
|
script_chan: GenericSender<ScriptThreadMessage>,
|
||||||
load_data: LoadData,
|
load_data: LoadData,
|
||||||
script_port: IpcReceiver<ScriptThreadMessage>,
|
script_port: GenericReceiver<ScriptThreadMessage>,
|
||||||
opts: Opts,
|
opts: Opts,
|
||||||
prefs: Box<Preferences>,
|
prefs: Box<Preferences>,
|
||||||
pipeline_namespace_id: PipelineNamespaceId,
|
pipeline_namespace_id: PipelineNamespaceId,
|
||||||
|
|
|
@ -13,6 +13,7 @@ use std::sync::{Arc, LazyLock};
|
||||||
|
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
use base::Epoch;
|
use base::Epoch;
|
||||||
|
use base::generic_channel::GenericSender;
|
||||||
use base::id::{PipelineId, WebViewId};
|
use base::id::{PipelineId, WebViewId};
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use compositing_traits::CrossProcessCompositorApi;
|
use compositing_traits::CrossProcessCompositorApi;
|
||||||
|
@ -25,7 +26,6 @@ use fnv::FnvHashMap;
|
||||||
use fonts::{FontContext, FontContextWebFontMethods};
|
use fonts::{FontContext, FontContextWebFontMethods};
|
||||||
use fonts_traits::StylesheetWebFontLoadFinishedCallback;
|
use fonts_traits::StylesheetWebFontLoadFinishedCallback;
|
||||||
use fxhash::FxHashMap;
|
use fxhash::FxHashMap;
|
||||||
use ipc_channel::ipc::IpcSender;
|
|
||||||
use layout_api::wrapper_traits::LayoutNode;
|
use layout_api::wrapper_traits::LayoutNode;
|
||||||
use layout_api::{
|
use layout_api::{
|
||||||
IFrameSizes, Layout, LayoutConfig, LayoutDamage, LayoutFactory, OffsetParentResponse,
|
IFrameSizes, Layout, LayoutConfig, LayoutDamage, LayoutFactory, OffsetParentResponse,
|
||||||
|
@ -135,7 +135,7 @@ pub struct LayoutThread {
|
||||||
is_iframe: bool,
|
is_iframe: bool,
|
||||||
|
|
||||||
/// The channel on which messages can be sent to the script thread.
|
/// The channel on which messages can be sent to the script thread.
|
||||||
script_chan: IpcSender<ScriptThreadMessage>,
|
script_chan: GenericSender<ScriptThreadMessage>,
|
||||||
|
|
||||||
/// The channel on which messages can be sent to the time profiler.
|
/// The channel on which messages can be sent to the time profiler.
|
||||||
time_profiler_chan: profile_time::ProfilerChan,
|
time_profiler_chan: profile_time::ProfilerChan,
|
||||||
|
|
|
@ -17,6 +17,7 @@ use std::time::{Duration, Instant};
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use base::cross_process_instant::CrossProcessInstant;
|
use base::cross_process_instant::CrossProcessInstant;
|
||||||
|
use base::generic_channel::GenericSender;
|
||||||
use base::id::{BrowsingContextId, PipelineId, WebViewId};
|
use base::id::{BrowsingContextId, PipelineId, WebViewId};
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
#[cfg(feature = "bluetooth")]
|
#[cfg(feature = "bluetooth")]
|
||||||
|
@ -3043,7 +3044,7 @@ impl Window {
|
||||||
time_profiler_chan: TimeProfilerChan,
|
time_profiler_chan: TimeProfilerChan,
|
||||||
devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
|
devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
|
||||||
constellation_chan: ScriptToConstellationChan,
|
constellation_chan: ScriptToConstellationChan,
|
||||||
control_chan: IpcSender<ScriptThreadMessage>,
|
control_chan: GenericSender<ScriptThreadMessage>,
|
||||||
pipeline_id: PipelineId,
|
pipeline_id: PipelineId,
|
||||||
parent_info: Option<PipelineId>,
|
parent_info: Option<PipelineId>,
|
||||||
viewport_details: ViewportDetails,
|
viewport_details: ViewportDetails,
|
||||||
|
@ -3292,7 +3293,7 @@ impl Window {
|
||||||
#[derive(MallocSizeOf)]
|
#[derive(MallocSizeOf)]
|
||||||
pub(crate) struct CSSErrorReporter {
|
pub(crate) struct CSSErrorReporter {
|
||||||
pub(crate) pipelineid: PipelineId,
|
pub(crate) pipelineid: PipelineId,
|
||||||
pub(crate) script_chan: IpcSender<ScriptThreadMessage>,
|
pub(crate) script_chan: GenericSender<ScriptThreadMessage>,
|
||||||
}
|
}
|
||||||
unsafe_no_jsmanaged_fields!(CSSErrorReporter);
|
unsafe_no_jsmanaged_fields!(CSSErrorReporter);
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ use std::cell::RefCell;
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
use std::result::Result;
|
use std::result::Result;
|
||||||
|
|
||||||
|
use base::generic_channel::GenericSender;
|
||||||
use base::id::PipelineId;
|
use base::id::PipelineId;
|
||||||
#[cfg(feature = "bluetooth")]
|
#[cfg(feature = "bluetooth")]
|
||||||
use bluetooth_traits::BluetoothRequest;
|
use bluetooth_traits::BluetoothRequest;
|
||||||
|
@ -331,7 +332,7 @@ pub(crate) struct ScriptThreadSenders {
|
||||||
|
|
||||||
/// A [`Sender`] that sends messages to the `Constellation`.
|
/// A [`Sender`] that sends messages to the `Constellation`.
|
||||||
#[no_trace]
|
#[no_trace]
|
||||||
pub(crate) constellation_sender: IpcSender<ScriptThreadMessage>,
|
pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
|
||||||
|
|
||||||
/// A [`Sender`] that sends messages to the `Constellation` associated with
|
/// A [`Sender`] that sends messages to the `Constellation` associated with
|
||||||
/// particular pipelines.
|
/// particular pipelines.
|
||||||
|
|
|
@ -871,9 +871,7 @@ impl ScriptThread {
|
||||||
JS_AddInterruptCallback(cx, Some(interrupt_callback));
|
JS_AddInterruptCallback(cx, Some(interrupt_callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ask the router to proxy IPC messages from the control port to us.
|
let constellation_receiver = state.constellation_receiver.into_inner();
|
||||||
let constellation_receiver =
|
|
||||||
ROUTER.route_ipc_receiver_to_new_crossbeam_receiver(state.constellation_receiver);
|
|
||||||
|
|
||||||
// Ask the router to proxy IPC messages from the devtools to us.
|
// Ask the router to proxy IPC messages from the devtools to us.
|
||||||
let devtools_server_sender = state.devtools_server_sender;
|
let devtools_server_sender = state.devtools_server_sender;
|
||||||
|
|
|
@ -20,6 +20,7 @@ malloc_size_of = { workspace = true }
|
||||||
malloc_size_of_derive = { workspace = true }
|
malloc_size_of_derive = { workspace = true }
|
||||||
parking_lot = { workspace = true }
|
parking_lot = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
|
servo_config = { path = "../../config" }
|
||||||
time = { workspace = true }
|
time = { workspace = true }
|
||||||
webrender_api = { workspace = true }
|
webrender_api = { workspace = true }
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use ipc_channel::router::ROUTER;
|
use ipc_channel::router::ROUTER;
|
||||||
|
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
static GENERIC_CHANNEL_USAGE_ERROR_PANIC_MSG: &str = "May not send a crossbeam channel over an IPC channel. \
|
static GENERIC_CHANNEL_USAGE_ERROR_PANIC_MSG: &str = "May not send a crossbeam channel over an IPC channel. \
|
||||||
|
@ -65,6 +66,12 @@ impl<T: Serialize> GenericSender<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Serialize> MallocSizeOf for GenericSender<T> {
|
||||||
|
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SendError;
|
pub struct SendError;
|
||||||
pub type SendResult = Result<(), SendError>;
|
pub type SendResult = Result<(), SendError>;
|
||||||
|
@ -145,11 +152,11 @@ where
|
||||||
/// Creates a Servo channel that can select different channel implementations based on multiprocess
|
/// Creates a Servo channel that can select different channel implementations based on multiprocess
|
||||||
/// mode or not. If the scenario doesn't require message to pass process boundary, a simple
|
/// mode or not. If the scenario doesn't require message to pass process boundary, a simple
|
||||||
/// crossbeam channel is preferred.
|
/// crossbeam channel is preferred.
|
||||||
pub fn channel<T>(multiprocess: bool) -> Option<(GenericSender<T>, GenericReceiver<T>)>
|
pub fn channel<T>() -> Option<(GenericSender<T>, GenericReceiver<T>)>
|
||||||
where
|
where
|
||||||
T: for<'de> Deserialize<'de> + Serialize,
|
T: for<'de> Deserialize<'de> + Serialize,
|
||||||
{
|
{
|
||||||
if multiprocess {
|
if servo_config::opts::get().multiprocess {
|
||||||
ipc_channel::ipc::channel()
|
ipc_channel::ipc::channel()
|
||||||
.map(|(tx, rx)| (GenericSender::Ipc(tx), GenericReceiver::Ipc(rx)))
|
.map(|(tx, rx)| (GenericSender::Ipc(tx), GenericReceiver::Ipc(rx)))
|
||||||
.ok()
|
.ok()
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub fn webgl_channel<T>() -> Option<(WebGLSender<T>, WebGLReceiver<T>)>
|
||||||
where
|
where
|
||||||
T: for<'de> Deserialize<'de> + Serialize,
|
T: for<'de> Deserialize<'de> + Serialize,
|
||||||
{
|
{
|
||||||
base::generic_channel::channel(servo_config::opts::get().multiprocess)
|
base::generic_channel::channel()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Entry point channel type used for sending WebGLMsg messages to the WebGL renderer.
|
/// Entry point channel type used for sending WebGLMsg messages to the WebGL renderer.
|
||||||
|
|
|
@ -20,6 +20,7 @@ use std::thread::JoinHandle;
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
use atomic_refcell::AtomicRefCell;
|
use atomic_refcell::AtomicRefCell;
|
||||||
use base::Epoch;
|
use base::Epoch;
|
||||||
|
use base::generic_channel::GenericSender;
|
||||||
use base::id::{BrowsingContextId, PipelineId, WebViewId};
|
use base::id::{BrowsingContextId, PipelineId, WebViewId};
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use compositing_traits::CrossProcessCompositorApi;
|
use compositing_traits::CrossProcessCompositorApi;
|
||||||
|
@ -30,7 +31,6 @@ use euclid::default::{Point2D as UntypedPoint2D, Rect};
|
||||||
use fnv::FnvHashMap;
|
use fnv::FnvHashMap;
|
||||||
use fonts::{FontContext, SystemFontServiceProxy};
|
use fonts::{FontContext, SystemFontServiceProxy};
|
||||||
use fxhash::FxHashMap;
|
use fxhash::FxHashMap;
|
||||||
use ipc_channel::ipc::IpcSender;
|
|
||||||
pub use layout_damage::LayoutDamage;
|
pub use layout_damage::LayoutDamage;
|
||||||
use libc::c_void;
|
use libc::c_void;
|
||||||
use malloc_size_of::{MallocSizeOf as MallocSizeOfTrait, MallocSizeOfOps, malloc_size_of_is_0};
|
use malloc_size_of::{MallocSizeOf as MallocSizeOfTrait, MallocSizeOfOps, malloc_size_of_is_0};
|
||||||
|
@ -197,7 +197,7 @@ pub struct LayoutConfig {
|
||||||
pub webview_id: WebViewId,
|
pub webview_id: WebViewId,
|
||||||
pub url: ServoUrl,
|
pub url: ServoUrl,
|
||||||
pub is_iframe: bool,
|
pub is_iframe: bool,
|
||||||
pub script_chan: IpcSender<ScriptThreadMessage>,
|
pub script_chan: GenericSender<ScriptThreadMessage>,
|
||||||
pub image_cache: Arc<dyn ImageCache>,
|
pub image_cache: Arc<dyn ImageCache>,
|
||||||
pub font_context: Arc<FontContext>,
|
pub font_context: Arc<FontContext>,
|
||||||
pub time_profiler_chan: time::ProfilerChan,
|
pub time_profiler_chan: time::ProfilerChan,
|
||||||
|
|
|
@ -15,6 +15,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use background_hang_monitor_api::BackgroundHangMonitorRegister;
|
use background_hang_monitor_api::BackgroundHangMonitorRegister;
|
||||||
use base::cross_process_instant::CrossProcessInstant;
|
use base::cross_process_instant::CrossProcessInstant;
|
||||||
|
use base::generic_channel::{GenericReceiver, GenericSender};
|
||||||
use base::id::{BrowsingContextId, HistoryStateId, PipelineId, PipelineNamespaceId, WebViewId};
|
use base::id::{BrowsingContextId, HistoryStateId, PipelineId, PipelineNamespaceId, WebViewId};
|
||||||
#[cfg(feature = "bluetooth")]
|
#[cfg(feature = "bluetooth")]
|
||||||
use bluetooth_traits::BluetoothRequest;
|
use bluetooth_traits::BluetoothRequest;
|
||||||
|
@ -307,9 +308,9 @@ pub struct InitialScriptState {
|
||||||
/// Loading into a Secure Context
|
/// Loading into a Secure Context
|
||||||
pub inherited_secure_context: Option<bool>,
|
pub inherited_secure_context: Option<bool>,
|
||||||
/// A channel with which messages can be sent to us (the script thread).
|
/// A channel with which messages can be sent to us (the script thread).
|
||||||
pub constellation_sender: IpcSender<ScriptThreadMessage>,
|
pub constellation_sender: GenericSender<ScriptThreadMessage>,
|
||||||
/// A port on which messages sent by the constellation to script can be received.
|
/// A port on which messages sent by the constellation to script can be received.
|
||||||
pub constellation_receiver: IpcReceiver<ScriptThreadMessage>,
|
pub constellation_receiver: GenericReceiver<ScriptThreadMessage>,
|
||||||
/// A channel on which messages can be sent to the constellation from script.
|
/// A channel on which messages can be sent to the constellation from script.
|
||||||
pub pipeline_to_constellation_sender: ScriptToConstellationChan,
|
pub pipeline_to_constellation_sender: ScriptToConstellationChan,
|
||||||
/// A handle to register script-(and associated layout-)threads for hang monitoring.
|
/// A handle to register script-(and associated layout-)threads for hang monitoring.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue