Skip to content

Commit d98c697

Browse files
committed
add generation checking to new_with_thread
1 parent 93d21ca commit d98c697

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/high/squirrel.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,24 @@ impl<T: PushToSquirrelVm + SQVMName> SuspendThread<T> {
498498
F: FnMut() -> T + Send + 'static,
499499
T: Send + Sync + 'static,
500500
{
501-
use crate::high::engine_sync::{async_execute, AsyncEngineMessage};
501+
use crate::{
502+
high::engine_sync::{async_execute, AsyncEngineMessage},
503+
mid::squirrel::sqvm_to_context,
504+
};
502505

503506
if !Self::is_thread_and_throw_error(thread_sqvm, SQFUNCTIONS.from_sqvm(thread_sqvm)) {
504507
return Self::new();
505508
}
506509

507510
unsafe { thread_sqvm.read().uiRef += 1 };
508511

512+
let context = unsafe { sqvm_to_context(thread_sqvm) };
513+
let generation = match context {
514+
ScriptContext::SERVER => SQVM_SERVER_GENERATION.load(Ordering::Relaxed),
515+
ScriptContext::CLIENT => SQVM_CLIENT_GENERATION.load(Ordering::Relaxed),
516+
ScriptContext::UI => SQVM_UI_GENERATION.load(Ordering::Relaxed),
517+
};
518+
509519
let thread_sqvm = unsafe { UnsafeHandle::new(thread_sqvm) };
510520
std::thread::spawn(move || {
511521
let result = thread_func();
@@ -517,6 +527,21 @@ impl<T: PushToSquirrelVm + SQVMName> SuspendThread<T> {
517527

518528
unsafe { thread_sqvm.read().uiRef -= 1 };
519529

530+
match context {
531+
ScriptContext::SERVER
532+
if SQVM_SERVER_GENERATION.load(Ordering::Relaxed) == generation => {}
533+
ScriptContext::CLIENT
534+
if SQVM_CLIENT_GENERATION.load(Ordering::Relaxed) == generation => {}
535+
ScriptContext::UI
536+
if SQVM_UI_GENERATION.load(Ordering::Relaxed) == generation => {}
537+
_ => {
538+
log::warn!(
539+
"a async sq function call was called in the wrong sqvm generation"
540+
);
541+
return;
542+
}
543+
}
544+
520545
result.push_to_sqvm(thread_sqvm, sq_functions);
521546
unsafe { resume_thread(thread_sqvm, sq_functions) };
522547
}))

0 commit comments

Comments
 (0)