diff options
| author | Satya Rohith <me@satyarohith.com> | 2024-03-14 01:22:53 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-13 19:52:53 +0000 |
| commit | bbc211906dcd5043af549250343cd7b42fb45043 (patch) | |
| tree | 49868b4b758290dcec6668f95495ce9d79ad6a86 /runtime/web_worker.rs | |
| parent | 0fd8f549e2194223eca2d4b17f4e96cd5a0f5fd5 (diff) | |
fix(ext/node): make worker ids sequential (#22884)
Diffstat (limited to 'runtime/web_worker.rs')
| -rw-r--r-- | runtime/web_worker.rs | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 82da9de9e..31930be39 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -55,33 +55,40 @@ use std::cell::RefCell; use std::fmt; use std::rc::Rc; use std::sync::atomic::AtomicBool; +use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering; use std::sync::Arc; use std::task::Context; use std::task::Poll; -#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] -pub enum WebWorkerType { - Classic, - Module, -} +static WORKER_ID_COUNTER: AtomicU32 = AtomicU32::new(1); -#[derive( - Debug, Default, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, -)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct WorkerId(u32); +impl WorkerId { + pub fn new() -> WorkerId { + let id = WORKER_ID_COUNTER.fetch_add(1, Ordering::SeqCst); + WorkerId(id) + } +} impl fmt::Display for WorkerId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "worker-{}", self.0) } } -impl WorkerId { - pub fn next(&self) -> Option<WorkerId> { - self.0.checked_add(1).map(WorkerId) +impl Default for WorkerId { + fn default() -> Self { + Self::new() } } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum WebWorkerType { + Classic, + Module, +} + /// Events that are sent to host from child /// worker. pub enum WorkerControlEvent { @@ -630,11 +637,13 @@ impl WebWorker { v8::String::new(scope, &format!("{}", self.id)) .unwrap() .into(); + let id: v8::Local<v8::Value> = + v8::Integer::new(scope, self.id.0 as i32).into(); bootstrap_fn .call( scope, undefined.into(), - &[args, name_str, id_str, worker_data], + &[args, name_str, id_str, id, worker_data], ) .unwrap(); } |
