From d5af4d2738bc47f9ecc30b0900400833077f022b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 10 Mar 2011 02:12:46 +0000 Subject: move set_nonblock() into util.c No need to repeat it and bloat ourselves. --- ext/sleepy_penguin/eventfd.c | 9 ++++----- ext/sleepy_penguin/inotify.c | 3 +-- ext/sleepy_penguin/nonblock.h | 19 ------------------- ext/sleepy_penguin/signalfd.c | 3 +-- ext/sleepy_penguin/sleepy_penguin.h | 1 + ext/sleepy_penguin/timerfd.c | 3 +-- ext/sleepy_penguin/util.c | 13 +++++++++++++ 7 files changed, 21 insertions(+), 30 deletions(-) delete mode 100644 ext/sleepy_penguin/nonblock.h diff --git a/ext/sleepy_penguin/eventfd.c b/ext/sleepy_penguin/eventfd.c index e7d7daf..20f74aa 100644 --- a/ext/sleepy_penguin/eventfd.c +++ b/ext/sleepy_penguin/eventfd.c @@ -1,7 +1,6 @@ #ifdef HAVE_SYS_EVENTFD_H #include "sleepy_penguin.h" #include -#include "nonblock.h" static ID id_for_fd; /* @@ -131,7 +130,7 @@ static VALUE incr(VALUE self, VALUE value) uint64_t val = (uint64_t)NUM2ULL(value); ssize_t w; - set_nonblock(fd); + rb_sp_set_nonblock(fd); retry: w = write(fd, &val, sizeof(uint64_t)); if (w == -1) { @@ -149,7 +148,7 @@ static VALUE getvalue(VALUE self) uint64_t val; ssize_t r; - set_nonblock(fd); + rb_sp_set_nonblock(fd); retry: r = read(fd, &val, sizeof(uint64_t)); if (r == -1) { @@ -176,7 +175,7 @@ static VALUE value_nonblock(VALUE self) uint64_t val; ssize_t r; - set_nonblock(fd); + rb_sp_set_nonblock(fd); r = read(fd, &val, sizeof(uint64_t)); if (r == -1) rb_sys_fail("read(eventfd)"); @@ -198,7 +197,7 @@ static VALUE incr_nonblock(VALUE self, VALUE value) uint64_t val = (uint64_t)NUM2ULL(value); ssize_t w; - set_nonblock(fd); + rb_sp_set_nonblock(fd); w = write(fd, &val, sizeof(uint64_t)); if (w == -1) rb_sys_fail("write(eventfd)"); diff --git a/ext/sleepy_penguin/inotify.c b/ext/sleepy_penguin/inotify.c index b1602a3..ec8b551 100644 --- a/ext/sleepy_penguin/inotify.c +++ b/ext/sleepy_penguin/inotify.c @@ -1,6 +1,5 @@ #ifdef HAVE_SYS_INOTIFY_H #include "sleepy_penguin.h" -#include "nonblock.h" #include #include static ID id_for_fd, id_inotify_buf, id_inotify_tmp, id_mask; @@ -219,7 +218,7 @@ static VALUE take(int argc, VALUE *argv, VALUE self) len = RSTRING_LEN(buf); ptr = (struct inotify_event *)RSTRING_PTR(buf); do { - set_nonblock(fd); + rb_sp_set_nonblock(fd); r = read(fd, ptr, len); if (r == 0 || (r < 0 && errno == EINVAL)) { /* resize internal buffer */ diff --git a/ext/sleepy_penguin/nonblock.h b/ext/sleepy_penguin/nonblock.h deleted file mode 100644 index 7198114..0000000 --- a/ext/sleepy_penguin/nonblock.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef SLEEPY_PENGUIN_NONBLOCK_H -#define SLEEPY_PENGUIN_NONBLOCK_H -#include -#include -#include -static void set_nonblock(int fd) -{ - int flags = fcntl(fd, F_GETFL); - - if (flags == -1) - rb_sys_fail("fcntl(F_GETFL)"); - if ((flags & O_NONBLOCK) == O_NONBLOCK) - return; - flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK); - if (flags == -1) - rb_sys_fail("fcntl(F_SETFL)"); -} - -#endif /* SLEEPY_PENGUIN_NONBLOCK_H */ diff --git a/ext/sleepy_penguin/signalfd.c b/ext/sleepy_penguin/signalfd.c index 8bb88f6..67a35f0 100644 --- a/ext/sleepy_penguin/signalfd.c +++ b/ext/sleepy_penguin/signalfd.c @@ -1,6 +1,5 @@ #ifdef HAVE_SYS_SIGNALFD_H #include "sleepy_penguin.h" -#include "nonblock.h" #include static ID id_for_fd, id_list; static VALUE ssi_members; @@ -181,7 +180,7 @@ static ssize_t do_sfd_read(struct signalfd_siginfo *ssi) int fd = ssi->ssi_fd; ssize_t r; - set_nonblock(fd); + rb_sp_set_nonblock(fd); do r = read(fd, ssi, sizeof(struct signalfd_siginfo)); diff --git a/ext/sleepy_penguin/sleepy_penguin.h b/ext/sleepy_penguin/sleepy_penguin.h index dff414b..ef4f87b 100644 --- a/ext/sleepy_penguin/sleepy_penguin.h +++ b/ext/sleepy_penguin/sleepy_penguin.h @@ -16,6 +16,7 @@ unsigned rb_sp_get_uflags(VALUE klass, VALUE flags); int rb_sp_get_flags(VALUE klass, VALUE flags); int rb_sp_io_closed(VALUE io); int rb_sp_fileno(VALUE io); +void rb_sp_set_nonblock(int fd); #define get_uflags rb_sp_get_uflags #define get_flags rb_sp_get_flags diff --git a/ext/sleepy_penguin/timerfd.c b/ext/sleepy_penguin/timerfd.c index 3305c24..47c353f 100644 --- a/ext/sleepy_penguin/timerfd.c +++ b/ext/sleepy_penguin/timerfd.c @@ -124,14 +124,13 @@ static VALUE expirations(VALUE self) return ULL2NUM(buf); } #else /* ! HAVE_RB_THREAD_BLOCKING_REGION */ -#include "nonblock.h" static VALUE expirations(VALUE self) { int fd = rb_sp_fileno(self); uint64_t buf; ssize_t r; - set_nonblock(fd); + rb_sp_set_nonblock(fd); retry: r = read(fd, &buf, sizeof(uint64_t)); if (r == -1) { diff --git a/ext/sleepy_penguin/util.c b/ext/sleepy_penguin/util.c index d8b27d4..44ab08a 100644 --- a/ext/sleepy_penguin/util.c +++ b/ext/sleepy_penguin/util.c @@ -120,3 +120,16 @@ int rb_sp_fileno(VALUE io) GetOpenFile(io, fptr); return FPTR_TO_FD(fptr); } + +void rb_sp_set_nonblock(int fd) +{ + int flags = fcntl(fd, F_GETFL); + + if (flags == -1) + rb_sys_fail("fcntl(F_GETFL)"); + if ((flags & O_NONBLOCK) == O_NONBLOCK) + return; + flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK); + if (flags == -1) + rb_sys_fail("fcntl(F_SETFL)"); +} -- cgit v1.2.3-24-ge0c7