Compare commits

..

2 Commits

Author SHA1 Message Date
Aaron Fenyes
7bc3a9eeae 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.
2024-11-18 20:29:53 -08:00
Aaron Fenyes
6b2d44a58c Assign each element a serial number
For future thread-safety, keep the next serial number in a static atomic
variable.
2024-11-18 16:08:06 -08:00
2 changed files with 15 additions and 2 deletions

View File

@ -1,7 +1,7 @@
use nalgebra::{DMatrix, DVector};
use rustc_hash::FxHashMap;
use slab::Slab;
use std::collections::BTreeSet;
use std::{cell::Cell, collections::BTreeSet};
use sycamore::prelude::*;
use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */
@ -13,6 +13,10 @@ pub type ConstraintKey = usize;
pub type ElementColor = [f32; 3];
thread_local! {
static NEXT_ELEMENT_SERIAL: Cell<u64> = Cell::new(0);
}
#[derive(Clone, PartialEq)]
pub struct Element {
pub id: String,
@ -20,6 +24,10 @@ pub struct Element {
pub color: ElementColor,
pub representation: Signal<DVector<f64>>,
pub constraints: Signal<BTreeSet<ConstraintKey>>,
// 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
@ -33,12 +41,17 @@ impl Element {
color: ElementColor,
representation: DVector<f64>
) -> 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: serial,
column_index: 0
}
}

View File

@ -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
)
}
}