[前][次][番号順一覧][スレッド一覧]

ruby-changes:64490

From: Marc-Andre <ko1@a...>
Date: Wed, 23 Dec 2020 09:46:27 +0900 (JST)
Subject: [ruby-changes:64490] 3286380ebc (master): [ruby/etc] Refactor locks using mutex API

https://git.ruby-lang.org/ruby.git/commit/?id=3286380ebc

From 3286380ebcc239b3fe1044884780162f1ee079fe Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@m...>
Date: Tue, 22 Dec 2020 00:02:45 -0500
Subject: [ruby/etc] Refactor locks using mutex API


diff --git a/ext/etc/etc.c b/ext/etc/etc.c
index cbff665..4c18b63 100644
--- a/ext/etc/etc.c
+++ b/ext/etc/etc.c
@@ -62,6 +62,19 @@ void rb_deprecate_constant(VALUE mod, const char *name); https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L62
 
 #include "constdefs.h"
 
+typedef int rb_nativethread_lock_t;
+static int rb_native_mutex_trylock(int *mutex) {
+    if (*mutex) {
+    	return 1;
+    }
+    *mutex = 1;
+    return 0;
+}
+static void rb_native_mutex_unlock(int *mutex) {
+    *mutex = 0;
+}
+#define rb_native_mutex_initialize rb_native_mutex_unlock
+
 /* call-seq:
  *	getlogin	->  String
  *
@@ -240,12 +253,12 @@ etc_getpwnam(VALUE obj, VALUE nam) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L253
 }
 
 #ifdef HAVE_GETPWENT
-static int passwd_blocking = 0;
+static rb_nativethread_lock_t passwd_blocking;
 static VALUE
 passwd_ensure(VALUE _)
 {
     endpwent();
-    passwd_blocking = (int)Qfalse;
+    rb_native_mutex_unlock(&passwd_blocking);
     return Qnil;
 }
 
@@ -264,10 +277,9 @@ passwd_iterate(VALUE _) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L277
 static void
 each_passwd(void)
 {
-    if (passwd_blocking) {
+    if (rb_native_mutex_trylock(&passwd_blocking)) {
 	rb_raise(rb_eRuntimeError, "parallel passwd iteration");
     }
-    passwd_blocking = (int)Qtrue;
     rb_ensure(passwd_iterate, 0, passwd_ensure, 0);
 }
 #endif
@@ -483,12 +495,12 @@ etc_getgrnam(VALUE obj, VALUE nam) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L495
 }
 
 #ifdef HAVE_GETGRENT
-static int group_blocking = 0;
+static rb_nativethread_lock_t group_blocking;
 static VALUE
 group_ensure(VALUE _)
 {
     endgrent();
-    group_blocking = (int)Qfalse;
+    rb_native_mutex_unlock(&group_blocking);
     return Qnil;
 }
 
@@ -508,10 +520,9 @@ group_iterate(VALUE _) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L520
 static void
 each_group(void)
 {
-    if (group_blocking) {
+    if (rb_native_mutex_trylock(&group_blocking)) {
 	rb_raise(rb_eRuntimeError, "parallel group iteration");
     }
-    group_blocking = (int)Qtrue;
     rb_ensure(group_iterate, 0, group_ensure, 0);
 }
 #endif
@@ -1180,8 +1191,11 @@ Init_etc(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L1191
     rb_deprecate_constant(rb_cStruct, "Passwd");
     rb_extend_object(sPasswd, rb_mEnumerable);
     rb_define_singleton_method(sPasswd, "each", etc_each_passwd, 0);
-
+#ifdef HAVE_GETPWENT
+    rb_native_mutex_initialize(&passwd_blocking);
+#endif
 #ifdef HAVE_GETGRENT
+    rb_native_mutex_initialize(&group_blocking);
     sGroup = rb_struct_define_under(mEtc, "Group", "name",
 #ifdef HAVE_STRUCT_GROUP_GR_PASSWD
 				    "passwd",
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]