Use rb_thread_call_with_gvl instead of global variable

This commit is contained in:
Kitaiti Makoto 2026-04-27 08:52:56 +09:00
parent a841074284
commit a62c44d722
4 changed files with 3 additions and 43 deletions

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;