ruby-changes:64489
From: Marc-Andre <ko1@a...>
Date: Wed, 23 Dec 2020 09:46:26 +0900 (JST)
Subject: [ruby-changes:64489] cd63f0358f (master): [ruby/etc] Make Ractor safe
https://git.ruby-lang.org/ruby.git/commit/?id=cd63f0358f From cd63f0358ffb43890d20bb5612c5d82be19fae62 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune <github@m...> Date: Mon, 21 Dec 2020 23:31:11 -0500 Subject: [ruby/etc] Make Ractor safe diff --git a/ext/etc/depend b/ext/etc/depend index b1c24e0..b2e3340 100644 --- a/ext/etc/depend +++ b/ext/etc/depend @@ -6,6 +6,20 @@ constdefs.h : $(srcdir)/mkconstants.rb https://github.com/ruby/ruby/blob/trunk/ext/etc/depend#L6 etc.o: $(RUBY_EXTCONF_H) etc.o: $(arch_hdrdir)/ruby/config.h etc.o: $(hdrdir)/ruby.h +etc.o: $(hdrdir)/ruby/assert.h +etc.o: $(hdrdir)/ruby/backward.h +etc.o: $(hdrdir)/ruby/backward/2/assume.h +etc.o: $(hdrdir)/ruby/backward/2/attributes.h +etc.o: $(hdrdir)/ruby/backward/2/bool.h +etc.o: $(hdrdir)/ruby/backward/2/gcc_version_since.h +etc.o: $(hdrdir)/ruby/backward/2/inttypes.h +etc.o: $(hdrdir)/ruby/backward/2/limits.h +etc.o: $(hdrdir)/ruby/backward/2/long_long.h +etc.o: $(hdrdir)/ruby/backward/2/stdalign.h +etc.o: $(hdrdir)/ruby/backward/2/stdarg.h +etc.o: $(hdrdir)/ruby/defines.h +etc.o: $(hdrdir)/ruby/encoding.h +etc.o: $(hdrdir)/ruby/intern.h etc.o: $(hdrdir)/ruby/internal/anyargs.h etc.o: $(hdrdir)/ruby/internal/arithmetic.h etc.o: $(hdrdir)/ruby/internal/arithmetic/char.h @@ -146,20 +160,6 @@ etc.o: $(hdrdir)/ruby/internal/value_type.h https://github.com/ruby/ruby/blob/trunk/ext/etc/depend#L160 etc.o: $(hdrdir)/ruby/internal/variable.h etc.o: $(hdrdir)/ruby/internal/warning_push.h etc.o: $(hdrdir)/ruby/internal/xmalloc.h -etc.o: $(hdrdir)/ruby/assert.h -etc.o: $(hdrdir)/ruby/backward.h -etc.o: $(hdrdir)/ruby/backward/2/assume.h -etc.o: $(hdrdir)/ruby/backward/2/attributes.h -etc.o: $(hdrdir)/ruby/backward/2/bool.h -etc.o: $(hdrdir)/ruby/backward/2/gcc_version_since.h -etc.o: $(hdrdir)/ruby/backward/2/inttypes.h -etc.o: $(hdrdir)/ruby/backward/2/limits.h -etc.o: $(hdrdir)/ruby/backward/2/long_long.h -etc.o: $(hdrdir)/ruby/backward/2/stdalign.h -etc.o: $(hdrdir)/ruby/backward/2/stdarg.h -etc.o: $(hdrdir)/ruby/defines.h -etc.o: $(hdrdir)/ruby/encoding.h -etc.o: $(hdrdir)/ruby/intern.h etc.o: $(hdrdir)/ruby/io.h etc.o: $(hdrdir)/ruby/missing.h etc.o: $(hdrdir)/ruby/onigmo.h @@ -167,6 +167,7 @@ etc.o: $(hdrdir)/ruby/oniguruma.h https://github.com/ruby/ruby/blob/trunk/ext/etc/depend#L167 etc.o: $(hdrdir)/ruby/ruby.h etc.o: $(hdrdir)/ruby/st.h etc.o: $(hdrdir)/ruby/subst.h +etc.o: $(hdrdir)/ruby/thread_native.h etc.o: constdefs.h etc.o: etc.c # AUTOGENERATED DEPENDENCIES END diff --git a/ext/etc/etc.c b/ext/etc/etc.c index 4c18b63..ab9de97 100644 --- a/ext/etc/etc.c +++ b/ext/etc/etc.c @@ -62,6 +62,10 @@ void rb_deprecate_constant(VALUE mod, const char *name); https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L62 #include "constdefs.h" +#ifdef HAVE_RB_EXT_RACTOR_SAFE +#include "ruby/thread_native.h" +#else +/* Implement rb_native_mutex_x using an int */ typedef int rb_nativethread_lock_t; static int rb_native_mutex_trylock(int *mutex) { if (*mutex) { @@ -74,6 +78,7 @@ static void rb_native_mutex_unlock(int *mutex) { https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L78 *mutex = 0; } #define rb_native_mutex_initialize rb_native_mutex_unlock +#endif /* call-seq: * getlogin -> String @@ -1087,6 +1092,9 @@ Init_etc(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L1092 { VALUE mEtc; + #ifdef HAVE_RB_EXT_RACTOR_SAFE + RB_EXT_RACTOR_SAFE(true); + #endif mEtc = rb_define_module("Etc"); rb_define_const(mEtc, "VERSION", rb_str_new_cstr(RUBY_ETC_VERSION)); init_constants(mEtc); diff --git a/test/etc/test_etc.rb b/test/etc/test_etc.rb index d2ae9fa..dc224d0 100644 --- a/test/etc/test_etc.rb +++ b/test/etc/test_etc.rb @@ -169,4 +169,27 @@ class TestEtc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/etc/test_etc.rb#L169 assert_operator(1, :<=, n) end + def test_ractor + return unless Etc.passwd # => skip test if no platform support + + assert_ractor(<<~RUBY, require: 'etc') + ractor = Ractor.new do + Etc.passwd do |s| + Ractor.yield :sync + Ractor.yield s.name + break :done + end + end + ractor.take # => :sync + assert_raise RuntimeError, /parallel/ do + Etc.passwd {} + end + name = ractor.take # => first name + ractor.take # => :done + name2 = Etc.passwd do |s| + break s.name + end + assert_equal(name2, name) + RUBY + end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/