summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common.mk2
-rw-r--r--string.c40
2 files changed, 41 insertions, 1 deletions
diff --git a/common.mk b/common.mk
index 17116c4c47..b4c2413af9 100644
--- a/common.mk
+++ b/common.mk
@@ -13453,6 +13453,7 @@ string.$(OBJEXT): $(top_srcdir)/internal/transcode.h
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
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 80ea96f574..145a153096 100644
--- a/string.c
+++ b/string.c
@@ -9667,6 +9667,42 @@ rb_str_oct(VALUE str)
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)
# 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)
# 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) {