sleepy_penguin RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH] unify rb_gc() handling for out-of-FD conditions
@ 2017-01-02  3:43 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2017-01-02  3:43 UTC (permalink / raw)
  To: sleepy-penguin; +Cc: Eric Wong

This should hopefully simplify logic a bit and avoid expensive
access to errno.
---
 ext/sleepy_penguin/epoll.c          | 4 +---
 ext/sleepy_penguin/eventfd.c        | 4 +---
 ext/sleepy_penguin/inotify.c        | 4 +---
 ext/sleepy_penguin/sleepy_penguin.h | 2 ++
 ext/sleepy_penguin/timerfd.c        | 4 +---
 ext/sleepy_penguin/util.c           | 9 +++++++++
 6 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/ext/sleepy_penguin/epoll.c b/ext/sleepy_penguin/epoll.c
index 120af0c..e655bf9 100644
--- a/ext/sleepy_penguin/epoll.c
+++ b/ext/sleepy_penguin/epoll.c
@@ -87,10 +87,8 @@ static VALUE s_new(VALUE klass, VALUE _flags)
 	VALUE rv;
 
 	if (fd < 0) {
-		if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
-			rb_gc();
+		if (rb_sp_gc_for_fd(errno))
 			fd = epoll_create1(flags);
-		}
 		if (fd < 0)
 			rb_sys_fail("epoll_create1");
 	}
diff --git a/ext/sleepy_penguin/eventfd.c b/ext/sleepy_penguin/eventfd.c
index 4f95b0d..4804150 100644
--- a/ext/sleepy_penguin/eventfd.c
+++ b/ext/sleepy_penguin/eventfd.c
@@ -31,10 +31,8 @@ static VALUE s_new(int argc, VALUE *argv, VALUE klass)
 
 	fd = eventfd(initval, flags);
 	if (fd < 0) {
-		if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
-			rb_gc();
+		if (rb_sp_gc_for_fd(errno))
 			fd = eventfd(initval, flags);
-		}
 		if (fd < 0)
 			rb_sys_fail("eventfd");
 	}
diff --git a/ext/sleepy_penguin/inotify.c b/ext/sleepy_penguin/inotify.c
index b324227..b5cd67b 100644
--- a/ext/sleepy_penguin/inotify.c
+++ b/ext/sleepy_penguin/inotify.c
@@ -26,10 +26,8 @@ static VALUE s_new(int argc, VALUE *argv, VALUE klass)
 
 	fd = inotify_init1(flags);
 	if (fd < 0) {
-		if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
-			rb_gc();
+		if (rb_sp_gc_for_fd(errno))
 			fd = inotify_init1(flags);
-		}
 		if (fd < 0)
 			rb_sys_fail("inotify_init1");
 	}
diff --git a/ext/sleepy_penguin/sleepy_penguin.h b/ext/sleepy_penguin/sleepy_penguin.h
index 522ac0a..8aa514a 100644
--- a/ext/sleepy_penguin/sleepy_penguin.h
+++ b/ext/sleepy_penguin/sleepy_penguin.h
@@ -90,4 +90,6 @@ void *rb_sp_gettlsbuf(size_t *size);
 # endif
 #endif
 
+int rb_sp_gc_for_fd(int err);
+
 #endif /* SLEEPY_PENGUIN_H */
diff --git a/ext/sleepy_penguin/timerfd.c b/ext/sleepy_penguin/timerfd.c
index a4f5c7d..e3af46c 100644
--- a/ext/sleepy_penguin/timerfd.c
+++ b/ext/sleepy_penguin/timerfd.c
@@ -31,10 +31,8 @@ static VALUE s_new(int argc, VALUE *argv, VALUE klass)
 
 	fd = timerfd_create(clockid, flags);
 	if (fd < 0) {
-		if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
-			rb_gc();
+		if (rb_sp_gc_for_fd(errno))
 			fd = timerfd_create(clockid, flags);
-		}
 		if (fd < 0)
 			rb_sys_fail("timerfd_create");
 	}
diff --git a/ext/sleepy_penguin/util.c b/ext/sleepy_penguin/util.c
index 4086b14..8dd6842 100644
--- a/ext/sleepy_penguin/util.c
+++ b/ext/sleepy_penguin/util.c
@@ -156,3 +156,12 @@ int rb_sp_wait(rb_sp_waitfn waiter, VALUE obj, int *fd)
 	*fd = rb_sp_fileno(obj);
 	return rc;
 }
+
+int rb_sp_gc_for_fd(int err)
+{
+	if (err == EMFILE || err == ENFILE || err == ENOMEM) {
+		rb_gc();
+		return 1;
+	}
+	return 0;
+}
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2017-01-02  3:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-02  3:43 [PATCH] unify rb_gc() handling for out-of-FD conditions Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/sleepy_penguin.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).