From e8998ebf9324169ba45910a66acc088c4d565614 Mon Sep 17 00:00:00 2001 From: jrmoulton Date: Sun, 20 Oct 2024 17:01:09 -0600 Subject: [PATCH] crash any other threads with an error message crash any other threads with an error message if signals are acessed from that thread. It would be better to have a solution that makes it so that at compile time users would get an error if signals are used improperly, but at least now there will be an error message --- reactive/src/runtime.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/reactive/src/runtime.rs b/reactive/src/runtime.rs index e9cebe5e..c86f5b0a 100644 --- a/reactive/src/runtime.rs +++ b/reactive/src/runtime.rs @@ -3,6 +3,7 @@ use std::{ cell::{Cell, RefCell}, collections::{HashMap, HashSet}, rc::Rc, + sync::atomic::{AtomicBool, Ordering}, }; use smallvec::SmallVec; @@ -13,8 +14,14 @@ use crate::{ signal::Signal, }; +static CREATED: AtomicBool = AtomicBool::new(false); thread_local! { - pub(crate) static RUNTIME: Runtime = Runtime::new(); + pub(crate) static RUNTIME: Runtime = { + if CREATED.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_err() { + panic!("RUNTIME must only be created once. You are probably using signals from multiple threads. All signals need to be accessed exclusively from the main thread."); + } + Runtime::new() + }; } /// The internal reactive Runtime which stores all the reactive system states in a