qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 2/5] char: unix write: Add some sleep to ease off sp


From: Amit Shah
Subject: [Qemu-devel] [PATCH 2/5] char: unix write: Add some sleep to ease off spinning in a tight loop
Date: Mon, 5 Apr 2010 18:15:35 +0530

When the other end of a chardev connection isn't picking up data as
fast as we're sending, we just used to keep spinning in a tight loop
till all the data was sent out.

Polling for POLLOUT indefinitely gives the other end a chance to catch
up and also saves us CPU cycles.

Signed-off-by: Amit Shah <address@hidden>
---
 qemu-char.c |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/qemu-char.c b/qemu-char.c
index d5b1662..48e1e57 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -36,6 +36,7 @@
 
 #include <unistd.h>
 #include <fcntl.h>
+#include <poll.h>
 #include <signal.h>
 #include <time.h>
 #include <errno.h>
@@ -513,15 +514,27 @@ int send_all(int fd, const void *buf, size_t *len)
 
 static int unix_write(int fd, const uint8_t *buf, size_t *len)
 {
+    struct pollfd pollfds[1];
     ssize_t tmplen, ret;
 
+    pollfds[0].fd = fd;
+    pollfds[0].events = POLLOUT;
+
     tmplen = *len;
     *len = 0;
     while (tmplen > 0) {
+        ret = poll(pollfds, 1, -1);
+        if (ret == -1) {
+            if (errno == EINTR) {
+                continue;
+            }
+            return -1;
+        }
         ret = write(fd, buf, tmplen);
         if (ret < 0) {
-            if (errno != EINTR && errno != EAGAIN)
-                return -1;
+            if (errno != EINTR && errno != EAGAIN) {
+                 return -1;
+            }
         } else if (ret == 0) {
             break;
         } else {
-- 
1.6.2.5





reply via email to

[Prev in Thread] Current Thread [Next in Thread]