From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] socket: avoid redundant fcntl on Linux
Date: Tue, 26 May 2015 20:47:39 +0000 [thread overview]
Message-ID: <1432673259-12360-1-git-send-email-e@80x24.org> (raw)
* ext/socket/ancdata.c (bsock_sendmsg_internal,
bsock_recvmsg_internal):
avoid redundant fcntl on Linux
[ruby-core:69154] [Feature #11145]
* ext/socket/init.c (rsock_s_recvfrom_nonblock): ditto
* ext/socket/rubysocket.h (MSG_DONTWAIT_RELIABLE): new macro
MSG_DONTWAIT is enough to force non-blocking I/O under Linux,
so avoid changing the state of a socket. This will allow certain
threads to do a non-destructive non-blocking "peek" while others
block (without relying on an extra ppoll syscall).
We shall be conservative about enabling this feature since some
OSes may have incomplete support for MSG_DONTWAIT. I shall
defer to a FreeBSD expert to enable that for FreeBSD.
---
ext/socket/ancdata.c | 4 ++--
ext/socket/init.c | 5 ++++-
ext/socket/rubysocket.h | 11 +++++++++++
3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c
index 3eaed1c..0a94149 100644
--- a/ext/socket/ancdata.c
+++ b/ext/socket/ancdata.c
@@ -1277,7 +1277,7 @@ bsock_sendmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
#endif
rb_io_check_closed(fptr);
- if (nonblock)
+ if (nonblock && !MSG_DONTWAIT_RELIABLE)
rb_io_set_nonblock(fptr);
ss = rb_sendmsg(fptr->fd, &mh, flags);
@@ -1595,7 +1595,7 @@ bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
flags |= MSG_PEEK;
rb_io_check_closed(fptr);
- if (nonblock)
+ if (nonblock && !MSG_DONTWAIT_RELIABLE)
rb_io_set_nonblock(fptr);
ss = rb_recvmsg(fptr->fd, &mh, flags);
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 317dd67..455652d 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -211,7 +211,10 @@ rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type
str = rb_tainted_str_new(0, buflen);
rb_io_check_closed(fptr);
- rb_io_set_nonblock(fptr);
+
+ if (!MSG_DONTWAIT_RELIABLE)
+ rb_io_set_nonblock(fptr);
+
len0 = alen;
slen = recvfrom(fd, RSTRING_PTR(str), buflen, flags, &buf.addr, &alen);
if (slen != -1 && len0 < alen)
diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h
index 689a7a8..359d28e 100644
--- a/ext/socket/rubysocket.h
+++ b/ext/socket/rubysocket.h
@@ -407,9 +407,20 @@ NORETURN(void rsock_sys_fail_raddrinfo_or_sockaddr(const char *, VALUE addr, VAL
#if defined(__linux__)
static inline int rsock_maybe_fd_writable(int fd) { return 1; }
static inline void rsock_maybe_wait_fd(int fd) { }
+# ifdef MSG_DONTWAIT
+# define MSG_DONTWAIT_RELIABLE 1
+# endif
#else /* some systems (mswin/mingw) need these. ref: r36946 */
# define rsock_maybe_fd_writable(fd) rb_thread_fd_writable((fd))
# define rsock_maybe_wait_fd(fd) rb_thread_wait_fd((fd))
#endif
+/*
+ * some OSes may support MSG_DONTWAIT inconsistently depending on socket
+ * type, we only expect Linux to support it consistently for all socket types.
+ */
+#ifndef MSG_DONTWAIT_RELIABLE
+# define MSG_DONTWAIT_RELIABLE 0
+#endif
+
#endif
--
EW
reply other threads:[~2015-05-26 20:47 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1432673259-12360-1-git-send-email-e@80x24.org \
--to=e@80x24.org \
--cc=spew@80x24.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).