diff options
-rw-r--r-- | ext/sleepy_penguin/epoll.c | 2 | ||||
-rw-r--r-- | ext/sleepy_penguin/eventfd.c | 45 | ||||
-rw-r--r-- | ext/sleepy_penguin/signalfd.c | 35 | ||||
-rw-r--r-- | ext/sleepy_penguin/sleepy_penguin.h | 12 | ||||
-rw-r--r-- | ext/sleepy_penguin/timerfd.c | 20 | ||||
-rw-r--r-- | ext/sleepy_penguin/util.c | 14 |
6 files changed, 44 insertions, 84 deletions
diff --git a/ext/sleepy_penguin/epoll.c b/ext/sleepy_penguin/epoll.c index 7af159d..ea915e2 100644 --- a/ext/sleepy_penguin/epoll.c +++ b/ext/sleepy_penguin/epoll.c @@ -344,7 +344,7 @@ static VALUE nogvl_wait(void *args) static VALUE real_epwait(struct rb_epoll *ep) { - int n = (int)rb_thread_blocking_region(nogvl_wait, ep, RUBY_UBF_IO, 0); + int n = (int)rb_sp_io_region(nogvl_wait, ep); return epwait_result(ep, n); } diff --git a/ext/sleepy_penguin/eventfd.c b/ext/sleepy_penguin/eventfd.c index 20f74aa..fd169f5 100644 --- a/ext/sleepy_penguin/eventfd.c +++ b/ext/sleepy_penguin/eventfd.c @@ -43,7 +43,6 @@ static VALUE s_new(int argc, VALUE *argv, VALUE klass) return rb_funcall(klass, id_for_fd, 1, INT2NUM(fd)); } -#ifdef HAVE_RB_THREAD_BLOCKING_REGION struct efd_args { int fd; uint64_t val; @@ -79,10 +78,11 @@ static VALUE incr(VALUE self, VALUE value) ssize_t w; x.fd = rb_sp_fileno(self); + blocking_io_prepare(x.fd); x.val = (uint64_t)NUM2ULL(value); retry: - w = (ssize_t)rb_thread_blocking_region(efd_write, &x, RUBY_UBF_IO, 0); + w = (ssize_t)rb_sp_io_region(efd_write, &x); if (w == -1) { if (rb_io_wait_writable(x.fd)) goto retry; @@ -111,9 +111,10 @@ static VALUE getvalue(VALUE self) ssize_t w; x.fd = rb_sp_fileno(self); + blocking_io_prepare(x.fd); retry: - w = (ssize_t)rb_thread_blocking_region(efd_read, &x, RUBY_UBF_IO, 0); + w = (ssize_t)rb_sp_io_region(efd_read, &x); if (w == -1) { if (rb_io_wait_readable(x.fd)) goto retry; @@ -122,44 +123,6 @@ retry: return ULL2NUM(x.val); } -#else /* !HAVE_RB_THREAD_BLOCKING_REGION */ - -static VALUE incr(VALUE self, VALUE value) -{ - int fd = rb_sp_fileno(self); - uint64_t val = (uint64_t)NUM2ULL(value); - ssize_t w; - - rb_sp_set_nonblock(fd); -retry: - w = write(fd, &val, sizeof(uint64_t)); - if (w == -1) { - if (rb_io_wait_writable(fd)) - goto retry; - rb_sys_fail("write(eventfd)"); - } - - return Qnil; -} - -static VALUE getvalue(VALUE self) -{ - int fd = rb_sp_fileno(self); - uint64_t val; - ssize_t r; - - rb_sp_set_nonblock(fd); -retry: - r = read(fd, &val, sizeof(uint64_t)); - if (r == -1) { - if (rb_io_wait_readable(fd)) - goto retry; - rb_sys_fail("read(eventfd)"); - } - - return ULL2NUM(val); -} -#endif /* !HAVE_RB_THREAD_BLOCKING_REGION */ /* * call-seq: diff --git a/ext/sleepy_penguin/signalfd.c b/ext/sleepy_penguin/signalfd.c index 67a35f0..d261bcf 100644 --- a/ext/sleepy_penguin/signalfd.c +++ b/ext/sleepy_penguin/signalfd.c @@ -160,8 +160,7 @@ static VALUE ssi_init(VALUE self) return self; } -#ifdef HAVE_RB_THREAD_BLOCKING_REGION -static VALUE xread(void *args) +static VALUE sfd_read(void *args) { struct signalfd_siginfo *ssi = args; int fd = ssi->ssi_fd; @@ -170,26 +169,6 @@ static VALUE xread(void *args) return (VALUE)r; } -static ssize_t do_sfd_read(struct signalfd_siginfo *ssi) -{ - return (ssize_t)rb_thread_blocking_region(xread, ssi, RUBY_UBF_IO, 0); -} -#else /* ! HAVE_RB_THREAD_BLOCKING_REGION */ -static ssize_t do_sfd_read(struct signalfd_siginfo *ssi) -{ - int fd = ssi->ssi_fd; - ssize_t r; - - rb_sp_set_nonblock(fd); - - do - r = read(fd, ssi, sizeof(struct signalfd_siginfo)); - while (r == -1 && rb_io_wait_readable(fd)); - - return r; -} -#endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */ - /* * call-seq: * sfd.take -> SignalFD::SigInfo object @@ -201,11 +180,17 @@ static VALUE sfd_take(VALUE self) VALUE rv = ssi_alloc(cSigInfo); struct signalfd_siginfo *ssi = DATA_PTR(rv); ssize_t r; + int fd; - ssi->ssi_fd = rb_sp_fileno(self); - r = do_sfd_read(ssi); - if (r < 0) + fd = ssi->ssi_fd = rb_sp_fileno(self); + blocking_io_prepare(fd); +retry: + r = (ssize_t)rb_sp_io_region(sfd_read, ssi); + if (r == -1) { + if (rb_io_wait_readable(fd)) + goto retry; rb_sys_fail("read(signalfd)"); + } if (r == 0) rb_eof_error(); return rv; diff --git a/ext/sleepy_penguin/sleepy_penguin.h b/ext/sleepy_penguin/sleepy_penguin.h index 0578ba1..9c2f701 100644 --- a/ext/sleepy_penguin/sleepy_penguin.h +++ b/ext/sleepy_penguin/sleepy_penguin.h @@ -18,6 +18,18 @@ int rb_sp_io_closed(VALUE io); int rb_sp_fileno(VALUE io); void rb_sp_set_nonblock(int fd); +#ifdef HAVE_RB_THREAD_BLOCKING_REGION +static inline VALUE rb_sp_io_region(rb_blocking_function_t *func, void *data) +{ + return rb_thread_blocking_region(func, data, RUBY_UBF_IO, 0); +} +# define blocking_io_prepare(fd) for(;0;) +#else +typedef VALUE rb_blocking_function_t(void *); +VALUE rb_sp_io_region(rb_blocking_function_t *func, void *data); +# define blocking_io_prepare(fd) rb_sp_set_nonblock((fd)) +#endif + #define NODOC_CONST(klass,name,value) \ rb_define_const((klass),(name),(value)) #endif /* SLEEPY_PENGUIN_H */ diff --git a/ext/sleepy_penguin/timerfd.c b/ext/sleepy_penguin/timerfd.c index 47c353f..1089f0b 100644 --- a/ext/sleepy_penguin/timerfd.c +++ b/ext/sleepy_penguin/timerfd.c @@ -95,7 +95,6 @@ static VALUE gettime(VALUE self) return itimerspec2ary(&curr); } -#ifdef HAVE_RB_THREAD_BLOCKING_REGION static VALUE tfd_read(void *args) { uint64_t *buf = args; @@ -115,24 +114,12 @@ static VALUE tfd_read(void *args) static VALUE expirations(VALUE self) { ssize_t r; - uint64_t buf = (int)rb_sp_fileno(self); - - r = (VALUE)rb_thread_blocking_region(tfd_read, &buf, RUBY_UBF_IO, 0); - if (r == -1) - rb_sys_fail("read(timerfd)"); - - return ULL2NUM(buf); -} -#else /* ! HAVE_RB_THREAD_BLOCKING_REGION */ -static VALUE expirations(VALUE self) -{ int fd = rb_sp_fileno(self); - uint64_t buf; - ssize_t r; + uint64_t buf = (uint64_t)fd; - rb_sp_set_nonblock(fd); + blocking_io_prepare(fd); retry: - r = read(fd, &buf, sizeof(uint64_t)); + r = (ssize_t)rb_sp_io_region(tfd_read, &buf); if (r == -1) { if (rb_io_wait_readable(fd)) goto retry; @@ -141,7 +128,6 @@ retry: return ULL2NUM(buf); } -#endif void sleepy_penguin_init_timerfd(void) { diff --git a/ext/sleepy_penguin/util.c b/ext/sleepy_penguin/util.c index 44ab08a..ca430c5 100644 --- a/ext/sleepy_penguin/util.c +++ b/ext/sleepy_penguin/util.c @@ -133,3 +133,17 @@ void rb_sp_set_nonblock(int fd) if (flags == -1) rb_sys_fail("fcntl(F_SETFL)"); } + +#ifndef HAVE_RB_THREAD_BLOCKING_REGION +#include <rubysig.h> +VALUE rb_sp_io_region(rb_blocking_function_t *func, void *data) +{ + VALUE rv; + + TRAP_BEG; + rv = func(data); + TRAP_END; + + return rv; +} +#endif |