diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/ffi_c/Function.c | 12 | ||||
-rw-r--r-- | ext/ffi_c/Variadic.c | 6 | ||||
-rw-r--r-- | ext/ffi_c/compat.h | 3 |
3 files changed, 13 insertions, 8 deletions
diff --git a/ext/ffi_c/Function.c b/ext/ffi_c/Function.c index 40d613e..9da6b37 100644 --- a/ext/ffi_c/Function.c +++ b/ext/ffi_c/Function.c @@ -492,6 +492,7 @@ static VALUE function_attach(VALUE self, VALUE module, VALUE name) { Function* fn; + char var[1024]; StringValue(name); TypedData_Get_Struct(self, Function, &function_data_type, fn); @@ -511,14 +512,11 @@ function_attach(VALUE self, VALUE module, VALUE name) } /* - * Stash the Function in a module variable so it does not get garbage collected + * Stash the Function in a module variable so it does not get garbage collected and can be inspected by attached_functions */ - VALUE funcs = rb_iv_get(module, "@ffi_functions"); - if (RB_NIL_P(funcs)) { - funcs = rb_hash_new(); - rb_iv_set(module, "@ffi_functions", funcs); - } - rb_hash_aset(funcs, name, self); + snprintf(var, sizeof(var), "@ffi_function_%s", StringValueCStr(name)); + rb_ractor_make_shareable(self); + rb_iv_set(module, var, self); rb_define_singleton_method(module, StringValueCStr(name), rbffi_MethodHandle_CodeAddress(fn->methodHandle), -1); diff --git a/ext/ffi_c/Variadic.c b/ext/ffi_c/Variadic.c index fb37194..cb5081b 100644 --- a/ext/ffi_c/Variadic.c +++ b/ext/ffi_c/Variadic.c @@ -36,6 +36,9 @@ #include <stdint.h> #include <stdbool.h> #include <ruby.h> +#if HAVE_RB_EXT_RACTOR_SAFE +#include <ruby/ractor.h> +#endif #include <ffi.h> #include "rbffi.h" @@ -181,7 +184,8 @@ variadic_initialize(VALUE self, VALUE rbFunction, VALUE rbParameterTypes, VALUE * @fixed and @type_map are used by the parameter mangling ruby code */ rb_iv_set(self, "@fixed", fixed); - rb_iv_set(self, "@type_map", rb_hash_aref(options, ID2SYM(rb_intern("type_map")))); + rb_iv_set(self, "@type_map", rb_obj_dup(rb_hash_aref(options, ID2SYM(rb_intern("type_map"))))); + rb_ractor_make_shareable(self); return retval; } diff --git a/ext/ffi_c/compat.h b/ext/ffi_c/compat.h index a8fdcbe..27a99be 100644 --- a/ext/ffi_c/compat.h +++ b/ext/ffi_c/compat.h @@ -98,5 +98,8 @@ #define FFI_RUBY_TYPED_FROZEN_SHAREABLE RUBY_TYPED_FROZEN_SHAREABLE #endif +#ifndef HAVE_RB_EXT_RACTOR_SAFE +#define rb_ractor_make_shareable(self) rb_obj_freeze(self); +#endif #endif /* RBFFI_COMPAT_H */ |