[前][次][番号順一覧][スレッド一覧]

ruby-changes:38585

From: normal <ko1@a...>
Date: Fri, 29 May 2015 11:24:32 +0900 (JST)
Subject: [ruby-changes:38585] normal:r50666 (trunk): socket: avoid redundant fcntl on Linux

normal	2015-05-29 11:24:18 +0900 (Fri, 29 May 2015)

  New Revision: 50666

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50666

  Log:
    socket: avoid redundant fcntl on Linux
    
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/ancdata.c
    trunk/ext/socket/init.c
    trunk/ext/socket/rubysocket.h
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50665)
+++ ChangeLog	(revision 50666)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri May 29 11:18:58 2015  Eric Wong  <e@8...>
+
+	* 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
+
 Fri May 29 10:30:34 2015  Eric Wong  <e@8...>
 
 	* lib/net/resolv.rb (request): use monotonic clock
Index: ext/socket/rubysocket.h
===================================================================
--- ext/socket/rubysocket.h	(revision 50665)
+++ ext/socket/rubysocket.h	(revision 50666)
@@ -407,9 +407,20 @@ NORETURN(void rsock_sys_fail_raddrinfo_o https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L407
 #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
Index: ext/socket/init.c
===================================================================
--- ext/socket/init.c	(revision 50665)
+++ ext/socket/init.c	(revision 50666)
@@ -211,7 +211,10 @@ rsock_s_recvfrom_nonblock(VALUE sock, in https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L211
     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)
Index: ext/socket/ancdata.c
===================================================================
--- ext/socket/ancdata.c	(revision 50665)
+++ ext/socket/ancdata.c	(revision 50666)
@@ -1277,7 +1277,7 @@ bsock_sendmsg_internal(int argc, VALUE * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1277
 #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 * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1595
         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);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]