kgio RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [RFC] use MSG_NOSIGNAL under Linux 2.2+
@ 2015-02-13 20:12 Eric Wong
  2015-02-13 20:33 ` Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2015-02-13 20:12 UTC (permalink / raw)
  To: kgio-public

I don't consider this a serious patch because it only affects
rare error handling paths and is not going to make a difference
in real-world use.  But oh well, I'm low on caffeine right now :x

MSG_NOSIGNAL exists on any machine that supports MSG_DONTWAIT.
While Ruby itself ignore SIGPIPE, this can save a few cycles
inside the kernel by avoiding the no-op signal delivery
in the first place.
---
 ext/kgio/read.c  | 10 ++++++++--
 ext/kgio/write.c |  2 +-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/ext/kgio/read.c b/ext/kgio/read.c
index 472a592..76b1638 100644
--- a/ext/kgio/read.c
+++ b/ext/kgio/read.c
@@ -5,8 +5,14 @@
 #include "nonblock.h"
 static VALUE sym_wait_readable;
 
+/*
+ * MSG_NOSIGNAL exists on any machine that supports MSG_DONTWAIT.
+ * While Ruby itself ignore SIGPIPE, this can save a few cycles
+ * inside the kernel by avoiding the no-op signal delivery
+ * in the first place.
+ */
 #ifdef USE_MSG_DONTWAIT
-static const int peek_flags = MSG_DONTWAIT|MSG_PEEK;
+static const int peek_flags = MSG_DONTWAIT|MSG_PEEK|MSG_NOSIGNAL;
 
 /* we don't need these variants, we call kgio_autopush_recv directly */
 static inline void kgio_autopush_read(VALUE io) { }
@@ -162,7 +168,7 @@ static VALUE my_recv(int io_wait, int argc, VALUE *argv, VALUE io)
 
 	if (a.len > 0) {
 retry:
-		n = (long)recv(a.fd, a.ptr, a.len, MSG_DONTWAIT);
+		n = (long)recv(a.fd, a.ptr, a.len, MSG_DONTWAIT|MSG_NOSIGNAL);
 		if (read_check(&a, n, "recv", io_wait) != 0)
 			goto retry;
 	}
diff --git a/ext/kgio/write.c b/ext/kgio/write.c
index ce4aa75..c97b049 100644
--- a/ext/kgio/write.c
+++ b/ext/kgio/write.c
@@ -123,7 +123,7 @@ static VALUE my_send(VALUE io, VALUE str, int io_wait)
 
 	prepare_write(&a, io, str);
 retry:
-	n = (long)send(a.fd, a.ptr, a.len, MSG_DONTWAIT);
+	n = (long)send(a.fd, a.ptr, a.len, MSG_DONTWAIT|MSG_NOSIGNAL);
 	if (write_check(&a, n, "send", io_wait) != 0)
 		goto retry;
 	if (TYPE(a.buf) != T_SYMBOL)
-- 
EW

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [RFC] use MSG_NOSIGNAL under Linux 2.2+
  2015-02-13 20:12 [RFC] use MSG_NOSIGNAL under Linux 2.2+ Eric Wong
@ 2015-02-13 20:33 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-02-13 20:33 UTC (permalink / raw)
  To: kgio-public

Eric Wong <e@80x24.org> wrote:
>  ext/kgio/read.c  | 10 ++++++++--
>  ext/kgio/write.c |  2 +-

And further inspection of the Linux kernel reveals MSG_NOSIGNAL only
affects writers, not readers, so the read.c changes are no-ops.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-02-13 20:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-13 20:12 [RFC] use MSG_NOSIGNAL under Linux 2.2+ Eric Wong
2015-02-13 20:33 ` Eric Wong

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

	https://yhbt.net/kgio.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).