mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
ui: hold the merge worker until pending reply checks finish
This commit is contained in:
5 files changed
+102
-23
No files matched your search
@@ -8,6 +8,9 @@ description = "VectorCraft tool state machines: pointer events in, commands and
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[features]
|
||||
testing = []
|
||||
|
||||
[dependencies]
|
||||
vectorcraft-geom = { workspace = true }
|
||||
vectorcraft-color = { workspace = true }
|
||||
|
||||
@@ -33,6 +33,9 @@ mod live_paint;
|
||||
mod lookup;
|
||||
mod map;
|
||||
|
||||
#[cfg(all(feature = "testing", not(target_arch = "wasm32")))]
|
||||
pub mod testing;
|
||||
|
||||
use cache::{BACKGROUND_SEGMENTS, CacheKey, RegionCache};
|
||||
pub use fill_area::{AreaFinder, EnclosedArea, FillAreaTool, every_layer, fill_area_roots};
|
||||
#[cfg(test)]
|
||||
@@ -260,7 +263,13 @@ struct PendingMerge {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn spawn_merge(map: Arc<BuilderMap>, touched: Touched, gap: f64, inputs: Vec<MergeInput>) -> Option<Receiver<MergeGeometry>> {
|
||||
let (tx, done) = std::sync::mpsc::channel();
|
||||
#[cfg(feature = "testing")]
|
||||
let gate = testing::take();
|
||||
let spawned = std::thread::Builder::new().name("vectorcraft-shape-builder-merge".into()).spawn(move || {
|
||||
#[cfg(feature = "testing")]
|
||||
if let Some(gate) = gate {
|
||||
gate.wait();
|
||||
}
|
||||
// The tool may have dropped the receiver meanwhile (another tool, another document).
|
||||
let _ = tx.send(merge_geometry(&map, &touched, gap, &inputs));
|
||||
});
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//! Channel control for the next Shape Builder merge started on the calling thread.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::{Receiver, RecvError, Sender, channel};
|
||||
|
||||
thread_local! {
|
||||
static NEXT: RefCell<Option<WorkerGate>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// Holds a merge worker until released. Dropping the gate also releases the worker.
|
||||
pub struct MergeGate {
|
||||
id: Arc<()>,
|
||||
started: Receiver<()>,
|
||||
release: Option<Sender<()>>,
|
||||
_thread: PhantomData<Rc<()>>,
|
||||
}
|
||||
|
||||
impl MergeGate {
|
||||
/// Waits until the worker reaches the gate.
|
||||
pub fn wait_started(&self) -> Result<(), RecvError> {
|
||||
self.started.recv()
|
||||
}
|
||||
|
||||
/// Allows the worker to compute the merge.
|
||||
pub fn release(&mut self) {
|
||||
if let Some(release) = self.release.take() {
|
||||
let _ = release.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MergeGate {
|
||||
fn drop(&mut self) {
|
||||
NEXT.with_borrow_mut(|next| {
|
||||
if next.as_ref().is_some_and(|gate| Arc::ptr_eq(&gate.id, &self.id)) {
|
||||
*next = None;
|
||||
}
|
||||
});
|
||||
self.release();
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct WorkerGate {
|
||||
id: Arc<()>,
|
||||
started: Sender<()>,
|
||||
release: Receiver<()>,
|
||||
}
|
||||
|
||||
impl WorkerGate {
|
||||
pub(super) fn wait(self) {
|
||||
let _ = self.started.send(());
|
||||
let _ = self.release.recv();
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn take() -> Option<WorkerGate> {
|
||||
NEXT.with_borrow_mut(Option::take)
|
||||
}
|
||||
|
||||
/// Pauses the next merge started on this thread. Returns an error if a gate is already waiting.
|
||||
pub fn pause_next_merge() -> Result<MergeGate, &'static str> {
|
||||
NEXT.with_borrow_mut(|next| {
|
||||
if next.is_some() {
|
||||
return Err("a Shape Builder merge gate is already waiting on this thread");
|
||||
}
|
||||
let id = Arc::new(());
|
||||
let (started_tx, started) = channel();
|
||||
let (release, release_rx) = channel();
|
||||
*next = Some(WorkerGate { id: id.clone(), started: started_tx, release: release_rx });
|
||||
Ok(MergeGate { id, started, release: Some(release), _thread: PhantomData })
|
||||
})
|
||||
}
|
||||
@@ -101,6 +101,7 @@ egui_kittest = { workspace = true, features = ["wgpu", "snapshot"] }
|
||||
egui_extras = { workspace = true }
|
||||
vectorcraft-apischema = { workspace = true }
|
||||
vectorcraft-testkit = { workspace = true }
|
||||
vectorcraft-tools = { workspace = true, features = ["testing"] }
|
||||
# A variable font built in code (no font file is committed).
|
||||
vectorcraft-text = { workspace = true, features = ["test-fonts"] }
|
||||
wat = { workspace = true }
|
||||
|
||||
@@ -912,6 +912,7 @@ fn pointer_replies_name_the_key_object() {
|
||||
|
||||
/// A Shape Builder click on a large selection merges on another thread: the frames go on, and
|
||||
/// the `ui.pointer` reply comes once the merge is applied, with the selection after it.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn pointer_reply_waits_for_a_merge_on_another_thread() {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
@@ -933,7 +934,9 @@ fn pointer_reply_waits_for_a_merge_on_another_thread() {
|
||||
assert_eq!(hover.try_recv().unwrap()["ok"], true, "a hover is answered at once");
|
||||
app.session.tool_poll(app.view_info(), true).unwrap();
|
||||
let revision = app.session.active().unwrap().revision;
|
||||
let mut merge = vectorcraft_tools::builder::testing::pause_next_merge().unwrap();
|
||||
let click = send(&mut app, json!([{"kind": "down", "x": 15, "y": 10}, {"kind": "up", "x": 15, "y": 10}]));
|
||||
merge.wait_started().unwrap();
|
||||
assert!(click.try_recv().is_err() && app.session.tool_editing(), "the click is not answered before the merge is applied");
|
||||
// A hover sent meanwhile is answered at once; a click is made after the merge, and its
|
||||
// reply waits until then.
|
||||
@@ -944,32 +947,20 @@ fn pointer_reply_waits_for_a_merge_on_another_thread() {
|
||||
assert!(press.try_recv().is_err(), "a Cmd-click during the merge waits for it");
|
||||
let hover = send(&mut app, json!([{"kind": "move", "x": 45, "y": 10}]));
|
||||
assert_eq!(hover.try_recv().map(|r| r["ok"].clone()), Ok(json!(true)), "a hover after the held Cmd-click");
|
||||
let start = std::time::Instant::now();
|
||||
let reply = loop {
|
||||
if let Ok(r) = click.try_recv() {
|
||||
break r;
|
||||
}
|
||||
assert!(app.session.tool_editing(), "the merge is being computed until the reply goes");
|
||||
assert_eq!(app.session.active().unwrap().revision, revision);
|
||||
assert!(start.elapsed() < std::time::Duration::from_secs(60), "no reply");
|
||||
std::thread::sleep(std::time::Duration::from_millis(2));
|
||||
// A frame: the canvas checks on the tool, then the replies are answered.
|
||||
let r = app.session.tool_poll(app.view_info(), false);
|
||||
crate::canvas::apply_requests(&mut app, r);
|
||||
app.run_control(&ctx);
|
||||
};
|
||||
assert!(app.session.tool_editing(), "the merge is being computed until the reply goes");
|
||||
assert_eq!(app.session.active().unwrap().revision, revision);
|
||||
merge.release();
|
||||
let r = app.session.tool_poll(app.view_info(), true);
|
||||
crate::canvas::apply_requests(&mut app, r);
|
||||
app.run_control(&ctx);
|
||||
let reply = click.try_recv().expect("the merge was applied before answering the click");
|
||||
assert_eq!(reply["ok"], true, "{reply}");
|
||||
assert_eq!(reply["result"]["selection"].as_array().map(Vec::len), Some(41), "{reply}: the selection after the merge");
|
||||
assert_ne!(app.session.active().unwrap().revision, revision);
|
||||
let press = loop {
|
||||
if let Ok(r) = press.try_recv() {
|
||||
break r;
|
||||
}
|
||||
assert!(start.elapsed() < std::time::Duration::from_secs(60), "no reply to the Cmd-click");
|
||||
let r = app.session.tool_poll(app.view_info(), false);
|
||||
crate::canvas::apply_requests(&mut app, r);
|
||||
app.run_control(&ctx);
|
||||
};
|
||||
let r = app.session.tool_poll(app.view_info(), true);
|
||||
crate::canvas::apply_requests(&mut app, r);
|
||||
app.run_control(&ctx);
|
||||
let press = press.try_recv().expect("the held Cmd-click was applied after the merge");
|
||||
assert_eq!(press["result"]["selection"].as_array().map(Vec::len), Some(0), "{press}: the lent selection tool deselected after the merge");
|
||||
assert_eq!(press["result"]["tool"], "shapeBuilder", "{press}");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user