diff --git a/bindings/ruby/ext/ruby_whisper.c b/bindings/ruby/ext/ruby_whisper.c index 654a1900b..9a79fe6fe 100644 --- a/bindings/ruby/ext/ruby_whisper.c +++ b/bindings/ruby/ext/ruby_whisper.c @@ -34,6 +34,9 @@ ID id_cache; ID id_n_processors; ID id_extended; ID id_start_log_callback_thread; +ID id_log_callback_thread; +ID id_alive; +ID id_join; static bool is_log_callback_finalized = false; static bool is_ruby_log_callback_present = false; @@ -203,6 +206,9 @@ void Init_whisper() { id_n_processors = rb_intern("n_processors"); id_extended = rb_intern("extended"); id_start_log_callback_thread = rb_intern("start_log_callback_thread"); + id_log_callback_thread = rb_intern("@log_callback_thread"); + id_alive = rb_intern("alive?"); + id_join = rb_intern("join"); mWhisper = rb_define_module("Whisper"); rb_require("whisper/log_settable"); diff --git a/bindings/ruby/ext/ruby_whisper_log.c b/bindings/ruby/ext/ruby_whisper_log_queue.c similarity index 92% rename from bindings/ruby/ext/ruby_whisper_log.c rename to bindings/ruby/ext/ruby_whisper_log_queue.c index 713ccf227..6d623a2f3 100644 --- a/bindings/ruby/ext/ruby_whisper_log.c +++ b/bindings/ruby/ext/ruby_whisper_log_queue.c @@ -3,6 +3,10 @@ #define LOG_QUEUE_CAPACITY 256 #define LOG_DEFAULT_CAPACITY 1024 +extern ID id_log_callback_thread; +extern ID id_alive; +extern ID id_join; + void ruby_whisper_log_queue_initialize(ruby_whisper_log_queue *log_queue) { @@ -41,7 +45,7 @@ ruby_whisper_log_queue_open(ruby_whisper_log_queue *log_queue) } void -ruby_whisper_log_queue_close(ruby_whisper_log_queue *log_queue) +ruby_whisper_log_queue_close(ruby_whisper_log_queue *log_queue, VALUE *mod) { rb_nativethread_lock_lock(&log_queue->lock); @@ -49,6 +53,11 @@ ruby_whisper_log_queue_close(ruby_whisper_log_queue *log_queue) rb_native_cond_broadcast(&log_queue->cond); rb_nativethread_lock_unlock(&log_queue->lock); + + VALUE log_callback_thread = rb_ivar_get(*mod, id_log_callback_thread); + if (!NIL_P(log_callback_thread) && RTEST(rb_funcall(log_callback_thread, id_alive, 0))) { + rb_funcall(log_callback_thread, id_join, 0); + } } static size_t diff --git a/bindings/ruby/ext/ruby_whisper_parakeet.c b/bindings/ruby/ext/ruby_whisper_parakeet.c index f805d5477..cb1233622 100644 --- a/bindings/ruby/ext/ruby_whisper_parakeet.c +++ b/bindings/ruby/ext/ruby_whisper_parakeet.c @@ -9,7 +9,7 @@ extern ID id_start_log_callback_thread; extern void ruby_whisper_log_queue_initialize(ruby_whisper_log_queue *log_queue); extern void ruby_whisper_log_queue_open(ruby_whisper_log_queue *log_queue); -extern void ruby_whisper_log_queue_close(ruby_whisper_log_queue *log_queue); +extern void ruby_whisper_log_queue_close(ruby_whisper_log_queue *log_queue, VALUE *mod); extern void ruby_whisper_log_queue_enqueue(ruby_whisper_log_queue *log_queue, enum ggml_log_level level, const char *text); extern VALUE ruby_whisper_log_queue_drain(ruby_whisper_log_queue *log_queue); @@ -46,22 +46,12 @@ ruby_whisper_parakeet_s_log_set(VALUE self, VALUE log_callback, VALUE user_data) static void ruby_whisper_parakeet_end_proc(VALUE args) { - ID id_log_callback_thread = rb_intern("@log_callback_thread"); - ID id_alive = rb_intern("alive?"); - ID id_join = rb_intern("join"); - - ruby_whisper_log_queue_close(¶keet_log_queue); - VALUE log_callback_thread = rb_ivar_get(mParakeet, id_log_callback_thread); - if (!NIL_P(log_callback_thread) && RTEST(rb_funcall(log_callback_thread, id_alive, 0))) { - rb_funcall(log_callback_thread, id_join, 0); - } + ruby_whisper_log_queue_close(¶keet_log_queue, &mParakeet); } void init_ruby_whisper_parakeet() { - id_start_log_callback_thread = rb_intern("start_log_callback_thread"); - ruby_whisper_log_queue_initialize(¶keet_log_queue); rb_define_singleton_method(mParakeet, "log_set", ruby_whisper_parakeet_s_log_set, 2);