summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-14 06:24:08 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-14 06:24:08 +0000
commit64fe84aea97489ef2060b553734ead50e71ac489 (patch)
tree8f6148a56f9872342d0edfd6339b183b9623f605 /ext
parent505d2d7ff6599ae77b66643594892096619b7c2f (diff)
merge revision(s) 44643: [Backport #9039]
* ext/socket: Avoid unnecessary ppoll/select on Linux. Patch by Eric Wong. [ruby-core:57950] [Bug #9039] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@44943 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/socket/basicsocket.c2
-rw-r--r--ext/socket/init.c4
-rw-r--r--ext/socket/rubysocket.h16
-rw-r--r--ext/socket/udpsocket.c2
4 files changed, 20 insertions, 4 deletions
diff --git a/ext/socket/basicsocket.c b/ext/socket/basicsocket.c
index 6f942e3a55..1530d677a3 100644
--- a/ext/socket/basicsocket.c
+++ b/ext/socket/basicsocket.c
@@ -566,7 +566,7 @@ rsock_bsock_send(int argc, VALUE *argv, VALUE sock)
GetOpenFile(sock, fptr);
arg.fd = fptr->fd;
arg.flags = NUM2INT(flags);
- while (rb_thread_fd_writable(arg.fd),
+ while (rsock_maybe_fd_writable(arg.fd),
(n = (int)BLOCKING_REGION_FD(func, &arg)) < 0) {
if (rb_io_wait_writable(arg.fd)) {
continue;
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 54452e4b99..8e418230be 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -129,7 +129,7 @@ rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
RBASIC(str)->klass = 0;
while (rb_io_check_closed(fptr),
- rb_thread_wait_fd(arg.fd),
+ rsock_maybe_wait_fd(arg.fd),
(slen = BLOCKING_REGION_FD(recvfrom_blocking, &arg)) < 0) {
if (!rb_io_wait_readable(fptr->fd)) {
rb_sys_fail("recvfrom(2)");
@@ -503,7 +503,7 @@ rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
arg.sockaddr = sockaddr;
arg.len = len;
retry:
- rb_thread_wait_fd(fd);
+ rsock_maybe_wait_fd(fd);
fd2 = (int)BLOCKING_REGION_FD(accept_blocking, &arg);
if (fd2 < 0) {
switch (errno) {
diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h
index ab05270be6..9a5f15c542 100644
--- a/ext/socket/rubysocket.h
+++ b/ext/socket/rubysocket.h
@@ -306,4 +306,20 @@ void rsock_init_addrinfo(void);
void rsock_init_sockopt(void);
void rsock_init_socket_init(void);
+/*
+ * It is safe on Linux to attempt using a socket without waiting on it in
+ * all cases. For some syscalls (e.g. accept/accept4), blocking on the
+ * syscall instead of relying on select/poll allows the kernel to use
+ * "wake-one" behavior and avoid the thundering herd problem.
+ * This is likely safe on all other *nix-like systems, so this whitelist
+ * can be expanded by interested parties.
+ */
+#if defined(__linux__)
+static inline int rsock_maybe_fd_writable(int fd) { return 1; }
+static inline void rsock_maybe_wait_fd(int fd) { }
+#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
+
#endif
diff --git a/ext/socket/udpsocket.c b/ext/socket/udpsocket.c
index 0ba4371f1a..b66c45dee4 100644
--- a/ext/socket/udpsocket.c
+++ b/ext/socket/udpsocket.c
@@ -176,7 +176,7 @@ udp_send(int argc, VALUE *argv, VALUE sock)
retry:
arg.to = res->ai_addr;
arg.tolen = res->ai_addrlen;
- rb_thread_fd_writable(arg.fd);
+ rsock_maybe_fd_writable(arg.fd);
n = (int)BLOCKING_REGION_FD(rsock_sendto_blocking, &arg);
if (n >= 0) {
freeaddrinfo(res0);