From 02d0662294ff577ea45c0b230db5a40396838e33 Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Fri, 9 May 2025 20:49:41 +0200 Subject: [PATCH] create ruby thread --- src/runtimes/ruby.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index b1a712f..d705a0a 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -1,6 +1,7 @@ use std::{ cell::{LazyCell, OnceCell}, sync::{LazyLock, Mutex, OnceLock}, + thread::{self, JoinHandle}, }; use bevy::{ @@ -22,7 +23,9 @@ use crate::{ }; #[derive(Resource)] -pub struct RubyRuntime {} +pub struct RubyRuntime { + ruby_thread: Option>, +} #[derive(ScheduleLabel, Clone, PartialEq, Eq, Debug, Hash, Default)] pub struct RubySchedule; @@ -44,24 +47,30 @@ impl From for RubyScript { Self(value) } } - -fn hello(subject: String) -> String { - format!("hello, {}", subject) -} - struct RubyEngine(Cleanup); // TODO: Add SAFETY? unsafe impl Send for RubyEngine {} -// TODO: thread local static RUBY_ENGINE: LazyLock> = LazyLock::new(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); impl Default for RubyRuntime { fn default() -> Self { - LazyLock::force(&RUBY_ENGINE); - Self {} + let ruby_thread = thread::spawn(|| { + let _cleanup = LazyLock::force(&RUBY_ENGINE); + while true {} + }); + Self { + ruby_thread: Some(ruby_thread), + } + } +} + +impl Drop for RubyRuntime { + fn drop(&mut self) { + let ruby_thread = self.ruby_thread.take().unwrap(); + ruby_thread.join().unwrap(); } }