feat: support SharedArrayBuffer sharing between workers (#11040)

This commit adds support for sharing SABs between workers.
This commit is contained in:
Luca Casonato 2021-07-06 19:42:52 +02:00 committed by GitHub
parent 672a88f272
commit bdfad23dd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 152 additions and 0 deletions

View file

@ -123,6 +123,9 @@ fn create_web_worker_callback(
get_error_class_fn: Some(&crate::errors::get_error_class_name),
blob_store: program_state.blob_store.clone(),
broadcast_channel: program_state.broadcast_channel.clone(),
shared_array_buffer_store: Some(
program_state.shared_array_buffer_store.clone(),
),
};
let (mut worker, external_handle) = WebWorker::from_options(
@ -209,6 +212,9 @@ pub fn create_main_worker(
}),
blob_store: program_state.blob_store.clone(),
broadcast_channel: program_state.broadcast_channel.clone(),
shared_array_buffer_store: Some(
program_state.shared_array_buffer_store.clone(),
),
};
let mut worker = MainWorker::from_options(main_module, permissions, &options);

View file

@ -15,6 +15,7 @@ use crate::module_graph::TypeLib;
use crate::source_maps::SourceMapGetter;
use crate::specifier_handler::FetchHandler;
use crate::version;
use deno_core::SharedArrayBufferStore;
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_runtime::deno_web::BlobStore;
use deno_runtime::inspector_server::InspectorServer;
@ -55,6 +56,7 @@ pub struct ProgramState {
pub ca_data: Option<Vec<u8>>,
pub blob_store: BlobStore,
pub broadcast_channel: InMemoryBroadcastChannel,
pub shared_array_buffer_store: SharedArrayBufferStore,
}
impl ProgramState {
@ -81,6 +83,7 @@ impl ProgramState {
let blob_store = BlobStore::default();
let broadcast_channel = InMemoryBroadcastChannel::default();
let shared_array_buffer_store = SharedArrayBufferStore::default();
let file_fetcher = FileFetcher::new(
http_cache,
@ -148,6 +151,7 @@ impl ProgramState {
ca_data,
blob_store,
broadcast_channel,
shared_array_buffer_store,
};
Ok(Arc::new(program_state))
}

View file

@ -248,6 +248,7 @@ pub async fn run(
origin_storage_dir: None,
blob_store,
broadcast_channel,
shared_array_buffer_store: None,
};
let mut worker =
MainWorker::from_options(main_module.clone(), permissions, &options);

View file

@ -0,0 +1,9 @@
self.postMessage("ready");
globalThis.addEventListener("message", (e) => {
const bytes1 = new Uint8Array(e.data[0]);
const bytes2 = new Uint8Array(e.data[1]);
bytes1[0] = 1;
bytes2[0] = 2;
self.postMessage("done");
});

View file

@ -789,6 +789,34 @@ Deno.test({
},
});
Deno.test({
name: "worker SharedArrayBuffer",
fn: async function (): Promise<void> {
const promise = deferred();
const workerOptions: WorkerOptions = { type: "module" };
const w = new Worker(
new URL("shared_array_buffer.ts", import.meta.url).href,
workerOptions,
);
const sab1 = new SharedArrayBuffer(1);
const sab2 = new SharedArrayBuffer(1);
const bytes1 = new Uint8Array(sab1);
const bytes2 = new Uint8Array(sab2);
assertEquals(bytes1[0], 0);
assertEquals(bytes2[0], 0);
w.onmessage = (): void => {
w.postMessage([sab1, sab2]);
w.onmessage = (): void => {
assertEquals(bytes1[0], 1);
assertEquals(bytes2[0], 2);
promise.resolve();
};
};
await promise;
w.terminate();
},
});
Deno.test({
name: "Send MessagePorts from / to workers",
fn: async function (): Promise<void> {