From 62e6973e9ca00899665dcfceb9f50d5dacc7e598 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 3 Feb 2018 01:21:40 +1300 Subject: [PATCH 1/6] Handle Monitor#interests = nil --- ext/nio4r/monitor.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/nio4r/monitor.c b/ext/nio4r/monitor.c index b7c3e5d..988807b 100644 --- a/ext/nio4r/monitor.c +++ b/ext/nio4r/monitor.c @@ -163,7 +163,11 @@ static VALUE NIO_Monitor_interests(VALUE self) static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests) { - NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests)); + if(interests == Qnil) { + NIO_Monitor_update_interests(self, 0); + } else { + NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests)); + } return rb_ivar_get(self, rb_intern("interests")); } From f9a856e7d88fd0efd60e6def15ba2939db82658a Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 3 Feb 2018 01:21:58 +1300 Subject: [PATCH 2/6] Only update interests if they have changed. --- ext/nio4r/monitor.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ext/nio4r/monitor.c b/ext/nio4r/monitor.c index 988807b..9ddbc92 100644 --- a/ext/nio4r/monitor.c +++ b/ext/nio4r/monitor.c @@ -296,9 +296,11 @@ static void NIO_Monitor_update_interests(VALUE self, int interests) rb_ivar_set(self, rb_intern("interests"), Qnil); } - monitor->interests = interests; + if(monitor->interests != interests) { + monitor->interests = interests; - ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io); - ev_io_set(&monitor->ev_io, monitor->ev_io.fd, monitor->interests); - ev_io_start(monitor->selector->ev_loop, &monitor->ev_io); + ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io); + ev_io_set(&monitor->ev_io, monitor->ev_io.fd, monitor->interests); + ev_io_start(monitor->selector->ev_loop, &monitor->ev_io); + } } From db2dfd5d4d9b93cc01865ea64fcf2e942784cbb1 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 3 Feb 2018 01:24:17 +1300 Subject: [PATCH 3/6] Allow Monitor#interests=nil in pure Ruby implementation. --- lib/nio/monitor.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/nio/monitor.rb b/lib/nio/monitor.rb index b480841..7b55e0f 100644 --- a/lib/nio/monitor.rb +++ b/lib/nio/monitor.rb @@ -26,12 +26,12 @@ def initialize(io, interests, selector) # Replace the existing interest set with a new one # - # @param interests [:r, :w, :rw] I/O readiness we're interested in (read/write/readwrite) + # @param interests [:r, :w, :rw, nil] I/O readiness we're interested in (read/write/readwrite) # # @return [Symbol] new interests def interests=(interests) raise EOFError, "monitor is closed" if closed? - raise ArgumentError, "bad interests: #{interests}" unless %i[r w rw].include?(interests) + raise ArgumentError, "bad interests: #{interests}" unless [:r, :w, :rw, nil].include?(interests) @interests = interests end From 17ac9935d13125672d0254f407c581a0e9498c63 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 3 Feb 2018 01:31:53 +1300 Subject: [PATCH 4/6] Also handle nil interests in JRuby implementation. --- ext/nio4r/org/nio4r/Monitor.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/nio4r/org/nio4r/Monitor.java b/ext/nio4r/org/nio4r/Monitor.java index 9565702..9e98f79 100644 --- a/ext/nio4r/org/nio4r/Monitor.java +++ b/ext/nio4r/org/nio4r/Monitor.java @@ -62,7 +62,12 @@ public IRubyObject setInterests(ThreadContext context, IRubyObject interests) { Ruby ruby = context.getRuntime(); SelectableChannel channel = (SelectableChannel)io.getChannel(); - key.interestOps(Nio4r.symbolToInterestOps(ruby, channel, interests)); + if(interests != context.nil) { + key.interestOps(Nio4r.symbolToInterestOps(ruby, channel, interests)); + } else { + key.interestOps(0); + } + this.interests = interests; return this.interests; From 04bd4f0bd6f0d219ff8df71aa2e67f4e71680d82 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 3 Feb 2018 01:42:10 +1300 Subject: [PATCH 5/6] Fix C implementation. --- ext/nio4r/monitor.c | 2 +- spec/nio/monitor_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ext/nio4r/monitor.c b/ext/nio4r/monitor.c index 9ddbc92..af04f5b 100644 --- a/ext/nio4r/monitor.c +++ b/ext/nio4r/monitor.c @@ -163,7 +163,7 @@ static VALUE NIO_Monitor_interests(VALUE self) static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests) { - if(interests == Qnil) { + if(NIL_P(interests)) { NIO_Monitor_update_interests(self, 0); } else { NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests)); diff --git a/spec/nio/monitor_spec.rb b/spec/nio/monitor_spec.rb index e0737af..55a4268 100644 --- a/spec/nio/monitor_spec.rb +++ b/spec/nio/monitor_spec.rb @@ -29,6 +29,12 @@ end describe "#interests=" do + it "can set interests to nil" do + expect(monitor.interests).not_to eq(nil) + monitor.interests = nil + expect(monitor.interests).to eq(nil) + end + it "changes the interest set" do expect(monitor.interests).not_to eq(:w) monitor.interests = :w From ff91aa06b79432f88974547ccaf2c147b2d90cc7 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 3 Feb 2018 01:44:11 +1300 Subject: [PATCH 6/6] Fix trailing whitespace. --- spec/nio/monitor_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/nio/monitor_spec.rb b/spec/nio/monitor_spec.rb index 55a4268..b1cf563 100644 --- a/spec/nio/monitor_spec.rb +++ b/spec/nio/monitor_spec.rb @@ -34,7 +34,7 @@ monitor.interests = nil expect(monitor.interests).to eq(nil) end - + it "changes the interest set" do expect(monitor.interests).not_to eq(:w) monitor.interests = :w