From 6b2d44a58c54f704c5d26c1acc14f0594aa4f806 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Mon, 18 Nov 2024 16:08:06 -0800 Subject: [PATCH 1/2] Assign each element a serial number For future thread-safety, keep the next serial number in a static atomic variable. --- app-proto/src/assembly.rs | 9 ++++++++- app-proto/src/outline.rs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 35b4417..a00081a 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -1,7 +1,7 @@ use nalgebra::{DMatrix, DVector}; use rustc_hash::FxHashMap; use slab::Slab; -use std::collections::BTreeSet; +use std::{collections::BTreeSet, sync::atomic::{AtomicU64, Ordering}}; use sycamore::prelude::*; use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */ @@ -13,6 +13,8 @@ pub type ConstraintKey = usize; pub type ElementColor = [f32; 3]; +static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0); + #[derive(Clone, PartialEq)] pub struct Element { pub id: String, @@ -20,6 +22,10 @@ pub struct Element { pub color: ElementColor, pub representation: Signal>, pub constraints: Signal>, + + // a serial number, assigned by `Element::new`, that uniquely identifies + // each element (until `NEXT_ELEMENT_SERIAL` wraps around) + pub serial: u64, // the configuration matrix column index that was assigned to this element // last time the assembly was realized @@ -39,6 +45,7 @@ impl Element { color: color, representation: create_signal(representation), constraints: create_signal(BTreeSet::default()), + serial: NEXT_ELEMENT_SERIAL.fetch_add(1, Ordering::SeqCst), column_index: 0 } } diff --git a/app-proto/src/outline.rs b/app-proto/src/outline.rs index ee1603f..e2cf49c 100644 --- a/app-proto/src/outline.rs +++ b/app-proto/src/outline.rs @@ -200,7 +200,7 @@ pub fn Outline() -> View { view=|(key, elt)| view! { ElementOutlineItem(key=key, element=elt) }, - key=|(key, _)| key.clone() + key=|(_, elt)| elt.serial ) } } -- 2.34.1 From 7bc3a9eeae91f719d441dff0eeb1bef6c6e1f89a Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Mon, 18 Nov 2024 20:29:53 -0800 Subject: [PATCH 2/2] Make the next element serial number thread-local In the last revision, the next element serial number was thread-safe, but that might be overdoing it. I suspect that elements will only ever be created from the main thread. --- app-proto/src/assembly.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index a00081a..bfbb2b8 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -1,7 +1,7 @@ use nalgebra::{DMatrix, DVector}; use rustc_hash::FxHashMap; use slab::Slab; -use std::{collections::BTreeSet, sync::atomic::{AtomicU64, Ordering}}; +use std::{cell::Cell, collections::BTreeSet}; use sycamore::prelude::*; use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */ @@ -13,7 +13,9 @@ pub type ConstraintKey = usize; pub type ElementColor = [f32; 3]; -static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0); +thread_local! { + static NEXT_ELEMENT_SERIAL: Cell = Cell::new(0); +} #[derive(Clone, PartialEq)] pub struct Element { @@ -39,13 +41,17 @@ impl Element { color: ElementColor, representation: DVector ) -> Element { + // take the next serial number + let serial = NEXT_ELEMENT_SERIAL.get(); + NEXT_ELEMENT_SERIAL.set(serial.wrapping_add(1)); + Element { id: id, label: label, color: color, representation: create_signal(representation), constraints: create_signal(BTreeSet::default()), - serial: NEXT_ELEMENT_SERIAL.fetch_add(1, Ordering::SeqCst), + serial: serial, column_index: 0 } } -- 2.34.1