summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2020-12-21 23:31:11 -0500
committerMarc-André Lafortune <github@marc-andre.ca>2020-12-22 19:46:07 -0500
commitcd63f0358ffb43890d20bb5612c5d82be19fae62 (patch)
treef51341602bea8993177c7ba5cd08daaf220eee86
parent3286380ebcc239b3fe1044884780162f1ee079fe (diff)
[ruby/etc] Make Ractor safe
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3954
-rw-r--r--ext/etc/depend29
-rw-r--r--ext/etc/etc.c8
-rw-r--r--test/etc/test_etc.rb23
3 files changed, 46 insertions, 14 deletions
diff --git a/ext/etc/depend b/ext/etc/depend
index b1c24e0a28..b2e334005e 100644
--- a/ext/etc/depend
+++ b/ext/etc/depend
@@ -6,6 +6,20 @@ constdefs.h : $(srcdir)/mkconstants.rb
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
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
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 4c18b630f7..ab9de97e6b 100644
--- a/ext/etc/etc.c
+++ b/ext/etc/etc.c
@@ -62,6 +62,10 @@ void rb_deprecate_constant(VALUE mod, const char *name);
#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) {
*mutex = 0;
}
#define rb_native_mutex_initialize rb_native_mutex_unlock
+#endif
/* call-seq:
* getlogin -> String
@@ -1087,6 +1092,9 @@ Init_etc(void)
{
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 d2ae9fa33c..dc224d0d32 100644
--- a/test/etc/test_etc.rb
+++ b/test/etc/test_etc.rb
@@ -169,4 +169,27 @@ class TestEtc < Test::Unit::TestCase
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