From a62c44d722c69118caf6b301634f2f86e89c45f0 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 27 Apr 2026 08:52:56 +0900 Subject: [PATCH] Use rb_thread_call_with_gvl instead of global variable --- bindings/ruby/ext/ruby_whisper.c | 19 +++---------------- bindings/ruby/ext/ruby_whisper_context.c | 8 -------- bindings/ruby/ext/ruby_whisper_params.c | 14 -------------- bindings/ruby/ext/ruby_whisper_transcribe.cpp | 5 ----- 4 files changed, 3 insertions(+), 43 deletions(-) diff --git a/bindings/ruby/ext/ruby_whisper.c b/bindings/ruby/ext/ruby_whisper.c index acced19d..9227dba6 100644 --- a/bindings/ruby/ext/ruby_whisper.c +++ b/bindings/ruby/ext/ruby_whisper.c @@ -30,7 +30,6 @@ ID id_n_processors; static bool is_log_callback_finalized = false; static bool is_ruby_log_callback_present = false; -static _Thread_local bool is_without_gvl = false; // High level API extern VALUE ruby_whisper_segment_allocate(VALUE klass); @@ -108,18 +107,6 @@ static VALUE ruby_whisper_s_finalize_log_callback(VALUE self, VALUE id) { return Qnil; } -void -ruby_whisper_gvl_locked(void) -{ - is_without_gvl = false; -} - -void -ruby_whisper_gvl_unlocked(void) -{ - is_without_gvl = true; -} - typedef struct { int level; const char * buffer; @@ -153,10 +140,10 @@ ruby_whisper_log_callback(enum ggml_log_level level, const char * buffer, void * buffer, rb_iv_get(mWhisper, "user_data") }; - if (is_without_gvl) { - rb_thread_call_with_gvl(call_log_callbacks, (void *)&args); - } else { + if (ruby_thread_has_gvl_p()) { call_log_callbacks((void *)&args); + } else { + rb_thread_call_with_gvl(call_log_callbacks, (void *)&args); } } diff --git a/bindings/ruby/ext/ruby_whisper_context.c b/bindings/ruby/ext/ruby_whisper_context.c index 42f6fbdb..2428aeff 100644 --- a/bindings/ruby/ext/ruby_whisper_context.c +++ b/bindings/ruby/ext/ruby_whisper_context.c @@ -23,8 +23,6 @@ extern VALUE ruby_whisper_transcribe(int argc, VALUE *argv, VALUE self); extern VALUE rb_whisper_model_s_new(VALUE context); extern VALUE rb_whisper_segment_s_new(VALUE context, int index); extern void prepare_transcription(ruby_whisper_params *rwp, VALUE *context, int n_processors); -extern void ruby_whisper_gvl_locked(void); -extern void ruby_whisper_gvl_unlocked(void); ID transcribe_option_names[1]; @@ -454,8 +452,6 @@ release_samples(VALUE rb_parsed_args) static void* full_without_gvl(void *rb_args) { - ruby_whisper_gvl_unlocked(); - full_without_gvl_args *args = (full_without_gvl_args *)rb_args; args->result = whisper_full(args->context, *args->params, args->samples, args->n_samples); return NULL; @@ -492,7 +488,6 @@ full_body(VALUE rb_args) rwp->abort_callback_container, }; rb_thread_call_without_gvl(full_without_gvl, (void *)&full_without_gvl_args, full_ubf, (void *)&full_ubf_args); - ruby_whisper_gvl_locked(); return INT2NUM(full_without_gvl_args.result); } @@ -534,8 +529,6 @@ VALUE ruby_whisper_full(int argc, VALUE *argv, VALUE self) static void* full_parallel_without_gvl(void *rb_args) { - ruby_whisper_gvl_unlocked(); - full_parallel_without_gvl_args *args = (full_parallel_without_gvl_args *)rb_args; args->result = whisper_full_parallel(args->context, *args->params, args->samples, args->n_samples, args->n_processors); return NULL; @@ -565,7 +558,6 @@ full_parallel_body(VALUE rb_args) rwp->abort_callback_container, }; rb_thread_call_without_gvl(full_parallel_without_gvl, (void *)&full_parallel_without_gvl_args, full_ubf, (void *)&full_ubf_args); - ruby_whisper_gvl_locked(); return INT2NUM(full_parallel_without_gvl_args.result); } diff --git a/bindings/ruby/ext/ruby_whisper_params.c b/bindings/ruby/ext/ruby_whisper_params.c index 93d76486..2aae7c12 100644 --- a/bindings/ruby/ext/ruby_whisper_params.c +++ b/bindings/ruby/ext/ruby_whisper_params.c @@ -33,8 +33,6 @@ extern VALUE mWhisper; extern ID id_call; -extern void ruby_whisper_gvl_locked(void); -extern void ruby_whisper_gvl_unlocked(void); extern VALUE ruby_whisper_normalize_model_path(VALUE model_path); extern VALUE rb_whisper_segment_s_new(VALUE context, int index); extern const rb_data_type_t ruby_whisper_vad_params_type; @@ -139,8 +137,6 @@ typedef struct { static void* call_new_segment_callbacks(void *v_args) { - ruby_whisper_gvl_locked(); - call_new_segment_callbacks_args *args = (call_new_segment_callbacks_args *)v_args; const ruby_whisper_callback_container *container = args->container; struct whisper_state *state = args->state; @@ -183,7 +179,6 @@ static void new_segment_callback(struct whisper_context *ctx, struct whisper_sta n_new }; rb_thread_call_with_gvl(call_new_segment_callbacks, (void *)&args); - ruby_whisper_gvl_unlocked(); } typedef struct { @@ -194,8 +189,6 @@ typedef struct { static void* call_progress_callbacks(void *v_args) { - ruby_whisper_gvl_locked(); - call_progress_callbacks_args *args = (call_progress_callbacks_args *)v_args; const ruby_whisper_callback_container *container = args->container; int progress_cur = args->progress_cur; @@ -232,7 +225,6 @@ static void progress_callback(struct whisper_context *ctx, struct whisper_state progress_cur }; rb_thread_call_with_gvl(call_progress_callbacks, (void *)&args); - ruby_whisper_gvl_unlocked(); } typedef struct { @@ -243,8 +235,6 @@ typedef struct { static void* call_encoder_begin_callbacks(void *v_args) { - ruby_whisper_gvl_locked(); - call_encoder_begin_callbacks_args *args = (call_encoder_begin_callbacks_args *)v_args; const ruby_whisper_callback_container *container = args->container; VALUE result = Qnil; @@ -288,7 +278,6 @@ static bool encoder_begin_callback(struct whisper_context *ctx, struct whisper_s true }; rb_thread_call_with_gvl(call_encoder_begin_callbacks, (void *)&args); - ruby_whisper_gvl_unlocked(); return args.is_continued; } @@ -301,8 +290,6 @@ typedef struct { static void* call_abort_callbacks(void *v_args) { - ruby_whisper_gvl_locked(); - call_abort_callbacks_args *args = (call_abort_callbacks_args *)v_args; const ruby_whisper_abort_callback_container *container = args->container; @@ -354,7 +341,6 @@ static bool abort_callback(void * user_data) { false }; rb_thread_call_with_gvl(call_abort_callbacks, (void *)&args); - ruby_whisper_gvl_unlocked(); return args.is_interrupted; } diff --git a/bindings/ruby/ext/ruby_whisper_transcribe.cpp b/bindings/ruby/ext/ruby_whisper_transcribe.cpp index e9313fc2..37656af1 100644 --- a/bindings/ruby/ext/ruby_whisper_transcribe.cpp +++ b/bindings/ruby/ext/ruby_whisper_transcribe.cpp @@ -16,8 +16,6 @@ extern ID id_to_path; extern ID transcribe_option_names[1]; extern void prepare_transcription(ruby_whisper_params * rwp, VALUE * self, int n_processors); -extern void ruby_whisper_gvl_locked(void); -extern void ruby_whisper_gvl_unlocked(void); typedef struct{ struct whisper_context *context; @@ -31,8 +29,6 @@ typedef struct{ static void* transcribe_without_gvl(void *rb_args) { - ruby_whisper_gvl_unlocked(); - transcribe_without_gvl_args *args = (transcribe_without_gvl_args *)rb_args; args->result = whisper_full_parallel(args->context, *args->params, args->samples, args->n_samples, args->n_processors); @@ -120,7 +116,6 @@ ruby_whisper_transcribe(int argc, VALUE *argv, VALUE self) { rwp->abort_callback_container, }; rb_thread_call_without_gvl(transcribe_without_gvl, (void *)&args, transcribe_ubf, (void *)&ubf_args); - ruby_whisper_gvl_locked(); if (args.result != 0) { fprintf(stderr, "failed to process audio\n"); return self;