ruby-changes:65854
From: Nobuyoshi <ko1@a...>
Date: Tue, 13 Apr 2021 13:12:54 +0900 (JST)
Subject: [ruby-changes:65854] 12f7ba5ed4 (master): Make String#crypt ractor-safe
https://git.ruby-lang.org/ruby.git/commit/?id=12f7ba5ed4 From 12f7ba5ed4a07855d6a9429aa627211db3655ca7 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 12 Apr 2021 22:56:15 +0900 Subject: Make String#crypt ractor-safe --- common.mk | 2 ++ string.c | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/common.mk b/common.mk index 17116c4..b4c2413 100644 --- a/common.mk +++ b/common.mk @@ -13453,6 +13453,7 @@ string.$(OBJEXT): $(top_srcdir)/internal/transcode.h https://github.com/ruby/ruby/blob/trunk/common.mk#L13453 string.$(OBJEXT): $(top_srcdir)/internal/vm.h string.$(OBJEXT): $(top_srcdir)/internal/warnings.h string.$(OBJEXT): {$(VPATH)}assert.h +string.$(OBJEXT): {$(VPATH)}atomic.h string.$(OBJEXT): {$(VPATH)}backward/2/assume.h string.$(OBJEXT): {$(VPATH)}backward/2/attributes.h string.$(OBJEXT): {$(VPATH)}backward/2/bool.h @@ -13626,6 +13627,7 @@ string.$(OBJEXT): {$(VPATH)}ruby_assert.h https://github.com/ruby/ruby/blob/trunk/common.mk#L13627 string.$(OBJEXT): {$(VPATH)}st.h string.$(OBJEXT): {$(VPATH)}string.c string.$(OBJEXT): {$(VPATH)}subst.h +string.$(OBJEXT): {$(VPATH)}thread_native.h string.$(OBJEXT): {$(VPATH)}util.h string.$(OBJEXT): {$(VPATH)}vm_debug.h string.$(OBJEXT): {$(VPATH)}vm_sync.h diff --git a/string.c b/string.c index 80ea96f..145a153 100644 --- a/string.c +++ b/string.c @@ -9667,6 +9667,42 @@ rb_str_oct(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L9667 return rb_str_to_inum(str, -8, FALSE); } +#ifndef HAVE_CRYPT_R +# include "ruby/thread_native.h" +# include "ruby/atomic.h" + +static struct { + rb_atomic_t initialized; + rb_nativethread_lock_t lock; +} crypt_mutex; + +static void +crypt_mutex_destroy(void) +{ + RUBY_ASSERT_ALWAYS(crypt_mutex.initialized == 1); + rb_nativethread_lock_destroy(&crypt_mutex.lock); + crypt_mutex.initialized = 0; +} + +static void +crypt_mutex_initialize(void) +{ + rb_atomic_t i; + while ((i = RUBY_ATOMIC_CAS(crypt_mutex.initialized, 0, 2)) == 2); + switch (i) { + case 0: + rb_nativethread_lock_initialize(&crypt_mutex.lock); + atexit(crypt_mutex_destroy); + RUBY_ASSERT(crypt_mutex.initialized == 2); + RUBY_ATOMIC_CAS(crypt_mutex.initialized, 2, 1); + break; + case 1: + break; + default: + rb_bug("crypt_mutex.initialized: %d->%d", i, crypt_mutex.initialized); + } +} +#endif /* * call-seq: @@ -9737,7 +9773,7 @@ rb_str_crypt(VALUE str, VALUE salt) https://github.com/ruby/ruby/blob/trunk/string.c#L9773 # define CRYPT_END() ALLOCV_END(databuf) #else extern char *crypt(const char *, const char *); -# define CRYPT_END() (void)0 +# define CRYPT_END() rb_nativethread_lock_unlock(&crypt_mutex.lock) #endif VALUE result; const char *s, *saltp; @@ -9770,6 +9806,8 @@ rb_str_crypt(VALUE str, VALUE salt) https://github.com/ruby/ruby/blob/trunk/string.c#L9806 # endif res = crypt_r(s, saltp, data); #else + crypt_mutex_initialize(); + rb_nativethread_lock_lock(&crypt_mutex.lock); res = crypt(s, saltp); #endif if (!res) { -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/