Skip to contentSkip to conversation

Clear fd handlers

PATCH In review Juan Quintela 2 participants 21 messages Mar 10, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Clear fd handlers

in this v2 series:
- a pony for malc:

   23 files changed, 133 insertions(+), 215 deletions(-)

- a pony for Anthony
fd_read_poll is no more.

- tap/qemu-char* poll users are gone. I just folded the poll function on top of the read one. Anyone knows of a different reason that legacy for having a poll function? Only difference should be that we are calling select with more descriptors.

- rename qemu_clear_fd_handler() to qemu_remove_fd_handler() It removes it from a list, don't clear anything.

- now that pull function is gone, and remove of handler is a diffrent function, only one NULL is used by function -> no more qemu_set_fd_{read,write,poll}_handler, just old qemu_set_fd_handler.

Monitor works, and tap works. I don't know if there is any performance difference at all. Could anyone that has a setup to meassure it test it?

Later, Juan.

v1 series :
- convert io_handlers to one QLIST
- once there, qemu_set_fd_handlers2() has lots of arguments that are a lot of times NULL. Introduce a set of functions to not have to pass NULL values.
- qemu_clear_fd_handlers(): remove it
- qemu_set_fd_read_handler(): only read
- qemu_set_fd_write_handler(): only write
- qemu_set_fd_rw_handler(): read/write
- qemu_set_fd_poll_handler(): it was only used for reads and in qemu-char.c.

- once there fix bt that don't need poll at all (having a poll function that always return true is the equivalent of not having one).
- remove cast to (void *)(unsigned long) for things that already were pointers
- IOCanRWHandler is only used for reads -> rename it (almost no users).

Juan Quintela (14):
Convert io handlers to QLIST
remove useless cast
rename IOCanRWHandler to IOCanReadHandler
bt: remove bt_host_read_poll()
Handle deleted IOHandlers in a single buffer
tap: insert tap_can_send() into tap_send() qemu-char:stdio insert poll call into read one qemu-char:tcp insert poll call into read one qemu-char:fd insert poll call into read one qemu-char:pty insert poll call into read one qemu-char:udp insert poll call into read one Remove qemu_set_fd_handler2()
Remove now unused fd_read_poll and all its only left user Add qemu_remove_fd_handler()

 aio.c               |    2 +-
 audio/alsaaudio.c   |    4 +-
 audio/ossaudio.c    |    6 +-
 bt-host.c           |    9 +----
 hw/xen_backend.c    |    4 +-
 migration-exec.c    |    7 +--
 migration-fd.c      |    5 +-
 migration-tcp.c     |   10 ++--
 migration-unix.c    |   10 ++--
 migration.c         |    8 ++--
 net/socket.c        |    6 +-
 net/tap.c           |   27 ++++--------
 net/vde.c           |    2 +-
 qemu-aio.h          |    4 +-
 qemu-char.c         |  112 ++++++++++++++++----------------------------------
 qemu-char.h         |   11 +----
 qemu-common.h       |    2 +-
 qemu-tool.c         |    9 ++--
 usb-linux.c         |    2 +-
 vl.c                |   92 +++++++++++++++++-------------------------
 vnc-auth-sasl.c     |    2 +-
 vnc-auth-vencrypt.c |    2 +-
 vnc.c               |   12 +++---
 23 files changed, 133 insertions(+), 215 deletions(-)
#1 lore raw

PATCH v2 1/14 Convert io handlers to QLIST

---
 vl.c |   35 ++++++++++++++---------------------
 1 files changed, 14 insertions(+), 21 deletions(-)
vl.c +14 -21
index 10d8e34..83ff652 100644
@@ -2597,10 +2597,12 @@ typedef struct IOHandlerRecord {
     void *opaque;
     /* temporary data */
     struct pollfd *ufd;
-    struct IOHandlerRecord *next;
+    QTAILQ_ENTRY(IOHandlerRecord) next;
 } IOHandlerRecord;
 
-static IOHandlerRecord *first_io_handler;
+static QTAILQ_HEAD(, IOHandlerRecord) io_handlers =
+    QTAILQ_HEAD_INITIALIZER(io_handlers);
+
 
 /* XXX: fd_read_poll should be suppressed, but an API change is
    necessary in the character devices to suppress fd_can_read(). */
@@ -2610,28 +2612,22 @@ int qemu_set_fd_handler2(int fd,
                          IOHandler *fd_write,
                          void *opaque)
 {
-    IOHandlerRecord **pioh, *ioh;
+    IOHandlerRecord *ioh;
 
     if (!fd_read && !fd_write) {
-        pioh = &first_io_handler;
-        for(;;) {
-            ioh = *pioh;
-            if (ioh == NULL)
-                break;
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd) {
                 ioh->deleted = 1;
                 break;
             }
-            pioh = &ioh->next;
         }
     } else {
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
+        QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);
     found:
         ioh->fd = fd;
         ioh->fd_read_poll = fd_read_poll;
@@ -3822,7 +3818,7 @@ void main_loop_wait(int timeout)
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&xfds);
-    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+    QTAILQ_FOREACH(ioh, &io_handlers, next) {
         if (ioh->deleted)
             continue;
         if (ioh->fd_read &&
@@ -3848,9 +3844,9 @@ void main_loop_wait(int timeout)
     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
     qemu_mutex_lock_iothread();
     if (ret > 0) {
-        IOHandlerRecord **pioh;
+        IOHandlerRecord *pioh;
 
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
                 ioh->fd_read(ioh->opaque);
             }
@@ -3860,14 +3856,11 @@ void main_loop_wait(int timeout)
         }
 
 	/* remove deleted IO handlers */
-	pioh = &first_io_handler;
-	while (*pioh) {
-            ioh = *pioh;
+        QTAILQ_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
             if (ioh->deleted) {
-                *pioh = ioh->next;
+                QTAILQ_REMOVE(&io_handlers, ioh, next);
                 qemu_free(ioh);
-            } else
-                pioh = &ioh->next;
+            }
         }
     }
 
Signature · 1 line
1.6.6.1
#8 lore raw

PATCH v2 2/14 remove useless cast

In reply to Juan Quintela

values are already pointers, no need to cast them to void *

---
 migration-exec.c |    3 +--
 migration-fd.c   |    3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
migration-exec.c +1 -2
index 6ff8449..5435827 100644
@@ -141,8 +141,7 @@ int exec_start_incoming_migration(const char *command)
     }
 
     qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
-			 exec_accept_incoming_migration, NULL,
-			 (void *)(unsigned long)f);
+			 exec_accept_incoming_migration, NULL, f);
 
     return 0;
 }
migration-fd.c +1 -2
index 9cf52ce..0abd372 100644
@@ -136,8 +136,7 @@ int fd_start_incoming_migration(const char *infd)
         return -errno;
     }
 
-    qemu_set_fd_handler2(fd, NULL, fd_accept_incoming_migration, NULL,
-			 (void *)(unsigned long)f);
+    qemu_set_fd_handler2(fd, NULL, fd_accept_incoming_migration, NULL, f);
 
     return 0;
 }
Signature · 1 line
1.6.6.1
#9 lore raw

PATCH v2 3/14 rename IOCanRWHandler to IOCanReadHandler

In reply to Juan Quintela

It was always only used for reads

---
 qemu-char.c   |    4 ++--
 qemu-char.h   |    7 +++----
 qemu-common.h |    2 +-
 qemu-tool.c   |    2 +-
 vl.c          |    4 ++--
 5 files changed, 9 insertions(+), 10 deletions(-)
qemu-char.c +2 -2
index 86c7c5a..6ea4e8c 100644
@@ -182,7 +182,7 @@ void qemu_chr_send_event(CharDriverState *s, int event)
 }
 
 void qemu_chr_add_handlers(CharDriverState *s,
-                           IOCanRWHandler *fd_can_read,
+                           IOCanReadHandler *fd_can_read,
                            IOReadHandler *fd_read,
                            IOEventHandler *fd_event,
                            void *opaque)
@@ -214,7 +214,7 @@ static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
 #define MUX_BUFFER_SIZE 32	/* Must be a power of 2.  */
 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
 typedef struct {
-    IOCanRWHandler *chr_can_read[MAX_MUX];
+    IOCanReadHandler *chr_can_read[MAX_MUX];
     IOReadHandler *chr_read[MAX_MUX];
     IOEventHandler *chr_event[MAX_MUX];
     void *ext_opaque[MAX_MUX];
qemu-char.h +3 -4
index bcc0766..3a9427b 100644
@@ -57,7 +57,7 @@ struct CharDriverState {
     int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
     int (*get_msgfd)(struct CharDriverState *s);
     IOEventHandler *chr_event;
-    IOCanRWHandler *chr_can_read;
+    IOCanReadHandler *chr_can_read;
     IOReadHandler *chr_read;
     void *handler_opaque;
     void (*chr_send_event)(struct CharDriverState *chr, int event);
@@ -79,7 +79,7 @@ void qemu_chr_printf(CharDriverState *s, const char *fmt, ...);
 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len);
 void qemu_chr_send_event(CharDriverState *s, int event);
 void qemu_chr_add_handlers(CharDriverState *s,
-                           IOCanRWHandler *fd_can_read,
+                           IOCanReadHandler *fd_can_read,
                            IOReadHandler *fd_read,
                            IOEventHandler *fd_event,
                            void *opaque);
@@ -98,7 +98,7 @@ extern int term_escape_char;
 /* async I/O support */
 
 int qemu_set_fd_handler2(int fd,
-                         IOCanRWHandler *fd_read_poll,
+                         IOCanReadHandler *fd_read_poll,
                          IOHandler *fd_read,
                          IOHandler *fd_write,
                          void *opaque);
@@ -106,5 +106,4 @@ int qemu_set_fd_handler(int fd,
                         IOHandler *fd_read,
                         IOHandler *fd_write,
                         void *opaque);
-
 #endif
qemu-common.h +1 -1
index 805be1a..42bfbcd 100644
@@ -183,7 +183,7 @@ void QEMU_NORETURN hw_error(const char *fmt, ...)
 
 /* IO callbacks.  */
 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
-typedef int IOCanRWHandler(void *opaque);
+typedef int IOCanReadHandler(void *opaque);
 typedef void IOHandler(void *opaque);
 
 struct ParallelIOArg {
qemu-tool.c +1 -1
index 18b48af..45e6df9 100644
@@ -91,7 +91,7 @@ void qemu_bh_delete(QEMUBH *bh)
 }
 
 int qemu_set_fd_handler2(int fd,
-                         IOCanRWHandler *fd_read_poll,
+                         IOCanReadHandler *fd_read_poll,
                          IOHandler *fd_read,
                          IOHandler *fd_write,
                          void *opaque)
vl.c +2 -2
index 83ff652..354ea31 100644
@@ -2590,7 +2590,7 @@ void pcmcia_info(Monitor *mon)
 
 typedef struct IOHandlerRecord {
     int fd;
-    IOCanRWHandler *fd_read_poll;
+    IOCanReadHandler *fd_read_poll;
     IOHandler *fd_read;
     IOHandler *fd_write;
     int deleted;
@@ -2607,7 +2607,7 @@ static QTAILQ_HEAD(, IOHandlerRecord) io_handlers =
 /* XXX: fd_read_poll should be suppressed, but an API change is
    necessary in the character devices to suppress fd_can_read(). */
 int qemu_set_fd_handler2(int fd,
-                         IOCanRWHandler *fd_read_poll,
+                         IOCanReadHandler *fd_read_poll,
                          IOHandler *fd_read,
                          IOHandler *fd_write,
                          void *opaque)
Signature · 1 line
1.6.6.1
#10 lore raw

PATCH v2 4/14 bt: remove bt_host_read_poll()

In reply to Juan Quintela

It allways returned true, that is the equivalent of not having the callback.

---
 bt-host.c |    9 +--------
 1 files changed, 1 insertions(+), 8 deletions(-)
bt-host.c +1 -8
index 964ac11..33b2761 100644
@@ -80,13 +80,6 @@ static void bt_host_sco(struct HCIInfo *hci, const uint8_t *data, int len)
     bt_host_send(hci, HCI_SCODATA_PKT, data, len);
 }
 
-static int bt_host_read_poll(void *opaque)
-{
-    struct bt_host_hci_s *s = (struct bt_host_hci_s *) opaque;
-
-    return !!s->hci.evt_recv;
-}
-
 static void bt_host_read(void *opaque)
 {
     struct bt_host_hci_s *s = (struct bt_host_hci_s *) opaque;
@@ -192,7 +185,7 @@ struct HCIInfo *bt_host_hci(const char *id)
     s->hci.acl_send = bt_host_acl;
     s->hci.bdaddr_set = bt_host_bdaddr_set;
 
-    qemu_set_fd_handler2(s->fd, bt_host_read_poll, bt_host_read, NULL, s);
+    qemu_set_fd_handler(s->fd, bt_host_read, NULL, s);
 
     return &s->hci;
 }
Signature · 1 line
1.6.6.1
#11 lore raw

PATCH v2 5/14 Handle deleted IOHandlers in a single pass

In reply to Juan Quintela

Previous code 1st call normal functions and then remove deleted handlers in the following pass. Calllapse the two walks.

---
 vl.c |   17 +++++++----------
 1 files changed, 7 insertions(+), 10 deletions(-)
vl.c +7 -10
index 354ea31..c2ae185 100644
@@ -3846,20 +3846,17 @@ void main_loop_wait(int timeout)
     if (ret > 0) {
         IOHandlerRecord *pioh;
 
-        QTAILQ_FOREACH(ioh, &io_handlers, next) {
-            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
-                ioh->fd_read(ioh->opaque);
-            }
-            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
-                ioh->fd_write(ioh->opaque);
-            }
-        }
-
-	/* remove deleted IO handlers */
         QTAILQ_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
             if (ioh->deleted) {
                 QTAILQ_REMOVE(&io_handlers, ioh, next);
                 qemu_free(ioh);
+                continue;
+            }
+            if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
+                ioh->fd_read(ioh->opaque);
+            }
+            if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
+                ioh->fd_write(ioh->opaque);
             }
         }
     }
Signature · 1 line
1.6.6.1
#13 lore raw

PATCH v2 7/14 qemu-char:stdio insert poll call into read one

In reply to Juan Quintela

This way we can remove the poll test

---
 qemu-char.c |   22 +++++++---------------
 1 files changed, 7 insertions(+), 15 deletions(-)
qemu-char.c +7 -15
index 6ea4e8c..93b3ea4 100644
@@ -664,8 +664,10 @@ static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
 static int term_fifo_size;
 
-static int stdio_read_poll(void *opaque)
+static void stdio_read(void *opaque)
 {
+    int size;
+    uint8_t buf[1];
     CharDriverState *chr = opaque;
 
     /* try to flush the queue if needed */
@@ -673,19 +675,9 @@ static int stdio_read_poll(void *opaque)
         qemu_chr_read(chr, term_fifo, 1);
         term_fifo_size = 0;
     }
-    /* see if we can absorb more chars */
-    if (term_fifo_size == 0)
-        return 1;
-    else
-        return 0;
-}
-
-static void stdio_read(void *opaque)
-{
-    int size;
-    uint8_t buf[1];
-    CharDriverState *chr = opaque;
-
+    if (term_fifo_size != 0) {
+        return;
+    }
     size = read(0, buf, 1);
     if (size == 0) {
         /* stdin has been closed. Remove it from the active list.  */
@@ -757,7 +749,7 @@ static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
         return NULL;
     chr = qemu_chr_open_fd(0, 1);
     chr->chr_close = qemu_chr_close_stdio;
-    qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
+    qemu_set_fd_handler(0, stdio_read, NULL, chr);
     stdio_nb_clients++;
     term_init(opts);
 
Signature · 1 line
1.6.6.1
#14 lore raw

PATCH v2 8/14 qemu-char:tcp insert poll call into read one

In reply to Juan Quintela

This way we can remove the poll test

---
 qemu-char.c |   18 +++++-------------
 1 files changed, 5 insertions(+), 13 deletions(-)
qemu-char.c +5 -13
index 93b3ea4..063da94 100644
@@ -1899,16 +1899,6 @@ static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
     }
 }
 
-static int tcp_chr_read_poll(void *opaque)
-{
-    CharDriverState *chr = opaque;
-    TCPCharDriver *s = chr->opaque;
-    if (!s->connected)
-        return 0;
-    s->max_size = qemu_chr_can_read(chr);
-    return s->max_size;
-}
-
 #define IAC 255
 #define IAC_BREAK 243
 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
@@ -2030,7 +2020,10 @@ static void tcp_chr_read(void *opaque)
     uint8_t buf[READ_BUF_LEN];
     int len, size;
 
-    if (!s->connected || s->max_size <= 0)
+    if (!s->connected)
+        return;
+    s->max_size = qemu_chr_can_read(chr);
+    if (s->max_size <= 0)
         return;
     len = sizeof(buf);
     if (len > s->max_size)
@@ -2064,8 +2057,7 @@ static void tcp_chr_connect(void *opaque)
     TCPCharDriver *s = chr->opaque;
 
     s->connected = 1;
-    qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
-                         tcp_chr_read, NULL, chr);
+    qemu_set_fd_handler(s->fd, tcp_chr_read, NULL, chr);
     qemu_chr_generic_open(chr);
 }
 
Signature · 1 line
1.6.6.1
#12 lore raw

PATCH v2 6/14 tap: insert tap_can_send() into tap_send()

In reply to Juan Quintela

This way we can remove the poll test

---
 net/tap.c |   27 +++++++++------------------
 1 files changed, 9 insertions(+), 18 deletions(-)
net/tap.c +9 -18
index 7a7320c..3ab65a3 100644
@@ -61,17 +61,15 @@ typedef struct TAPState {
 
 static int launch_script(const char *setup_script, const char *ifname, int fd);
 
-static int tap_can_send(void *opaque);
 static void tap_send(void *opaque);
 static void tap_writable(void *opaque);
 
 static void tap_update_fd_handler(TAPState *s)
 {
-    qemu_set_fd_handler2(s->fd,
-                         s->read_poll  ? tap_can_send : NULL,
-                         s->read_poll  ? tap_send     : NULL,
-                         s->write_poll ? tap_writable : NULL,
-                         s);
+    qemu_set_fd_handler(s->fd,
+                        s->read_poll  ? tap_send     : NULL,
+                        s->write_poll ? tap_writable : NULL,
+                        s);
 }
 
 static void tap_read_poll(TAPState *s, int enable)
@@ -165,13 +163,6 @@ static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
     return tap_write_packet(s, iov, 1);
 }
 
-static int tap_can_send(void *opaque)
-{
-    TAPState *s = opaque;
-
-    return qemu_can_send_packet(&s->nc);
-}
-
 #ifndef __sun__
 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
 {
@@ -188,12 +179,10 @@ static void tap_send_completed(VLANClientState *nc, ssize_t len)
 static void tap_send(void *opaque)
 {
     TAPState *s = opaque;
-    int size;
 
-    do {
+    while (qemu_can_send_packet(&s->nc) > 0) {
         uint8_t *buf = s->buf;
-
-        size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
+        int size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
         if (size <= 0) {
             break;
         }
@@ -206,8 +195,10 @@ static void tap_send(void *opaque)
         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
         if (size == 0) {
             tap_read_poll(s, 0);
+        } else if (size < 0) {
+            return;
         }
-    } while (size > 0 && qemu_can_send_packet(&s->nc));
+    }
 }
 
 int tap_has_ufo(VLANClientState *nc)
Signature · 1 line
1.6.6.1
#17 lore raw

PATCH v2 11/14 qemu-char:udp insert poll call into read one

In reply to Juan Quintela

This way we can remove the poll test

---
 qemu-char.c |   13 ++-----------
 1 files changed, 2 insertions(+), 11 deletions(-)
qemu-char.c +2 -11
index bc294af..9098d79 100644
@@ -1767,7 +1767,7 @@ static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
     return send(s->fd, (const void *)buf, len, 0);
 }
 
-static int udp_chr_read_poll(void *opaque)
+static void udp_chr_read(void *opaque)
 {
     CharDriverState *chr = opaque;
     NetCharDriver *s = chr->opaque;
@@ -1782,14 +1782,6 @@ static int udp_chr_read_poll(void *opaque)
         s->bufptr++;
         s->max_size = qemu_chr_can_read(chr);
     }
-    return s->max_size;
-}
-
-static void udp_chr_read(void *opaque)
-{
-    CharDriverState *chr = opaque;
-    NetCharDriver *s = chr->opaque;
-
     if (s->max_size == 0)
         return;
     s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
@@ -1810,8 +1802,7 @@ static void udp_chr_update_read_handler(CharDriverState *chr)
     NetCharDriver *s = chr->opaque;
 
     if (s->fd >= 0) {
-        qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
-                             udp_chr_read, NULL, chr);
+        qemu_set_fd_handler(s->fd, udp_chr_read, NULL, chr);
     }
 }
 
Signature · 1 line
1.6.6.1
#16 lore raw

PATCH v2 10/14 qemu-char:pty insert poll call into read one

In reply to Juan Quintela

This way we can remove the poll test

---
 qemu-char.c |   16 +++++-----------
 1 files changed, 5 insertions(+), 11 deletions(-)
qemu-char.c +5 -11
index 875f254..bc294af 100644
@@ -833,15 +833,6 @@ static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
     return send_all(s->fd, buf, len);
 }
 
-static int pty_chr_read_poll(void *opaque)
-{
-    CharDriverState *chr = opaque;
-    PtyCharDriver *s = chr->opaque;
-
-    s->read_bytes = qemu_chr_can_read(chr);
-    return s->read_bytes;
-}
-
 static void pty_chr_read(void *opaque)
 {
     CharDriverState *chr = opaque;
@@ -849,6 +840,10 @@ static void pty_chr_read(void *opaque)
     int size, len;
     uint8_t buf[READ_BUF_LEN];
 
+    s->read_bytes = qemu_chr_can_read(chr);
+
+    if (s->read_bytes <= 0)
+        return;
     len = sizeof(buf);
     if (len > s->read_bytes)
         len = s->read_bytes;
@@ -870,8 +865,7 @@ static void pty_chr_update_read_handler(CharDriverState *chr)
 {
     PtyCharDriver *s = chr->opaque;
 
-    qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
-                         pty_chr_read, NULL, chr);
+    qemu_set_fd_handler(s->fd, pty_chr_read, NULL, chr);
     s->polling = 1;
     /*
      * Short timeout here: just need wait long enougth that qemu makes
Signature · 1 line
1.6.6.1
#15 lore raw

PATCH v2 9/14 qemu-char:fd insert poll call into read one

In reply to Juan Quintela

This way we can remove the poll test

---
 qemu-char.c |   15 ++++-----------
 1 files changed, 4 insertions(+), 11 deletions(-)
qemu-char.c +4 -11
index 063da94..875f254 100644
@@ -536,15 +536,6 @@ static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
     return send_all(s->fd_out, buf, len);
 }
 
-static int fd_chr_read_poll(void *opaque)
-{
-    CharDriverState *chr = opaque;
-    FDCharDriver *s = chr->opaque;
-
-    s->max_size = qemu_chr_can_read(chr);
-    return s->max_size;
-}
-
 static void fd_chr_read(void *opaque)
 {
     CharDriverState *chr = opaque;
@@ -552,6 +543,9 @@ static void fd_chr_read(void *opaque)
     int size, len;
     uint8_t buf[READ_BUF_LEN];
 
+    s->max_size = qemu_chr_can_read(chr);
+    if (s->max_size <= 0)
+        return;
     len = sizeof(buf);
     if (len > s->max_size)
         len = s->max_size;
@@ -576,8 +570,7 @@ static void fd_chr_update_read_handler(CharDriverState *chr)
     if (s->fd_in >= 0) {
         if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
         } else {
-            qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
-                                 fd_chr_read, NULL, chr);
+            qemu_set_fd_handler(s->fd_in, fd_chr_read, NULL, chr);
         }
     }
 }
Signature · 1 line
1.6.6.1
#19 lore raw

PATCH v2 13/14 Remove now unused fd_read_poll and all its only left user

In reply to Juan Quintela
---
 vl.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
vl.c +1 -4
index a961710..3bf8b87 100644
@@ -2590,7 +2590,6 @@ void pcmcia_info(Monitor *mon)
 
 typedef struct IOHandlerRecord {
     int fd;
-    IOCanReadHandler *fd_read_poll;
     IOHandler *fd_read;
     IOHandler *fd_write;
     int deleted;
@@ -3809,9 +3808,7 @@ void main_loop_wait(int timeout)
     QTAILQ_FOREACH(ioh, &io_handlers, next) {
         if (ioh->deleted)
             continue;
-        if (ioh->fd_read &&
-            (!ioh->fd_read_poll ||
-             ioh->fd_read_poll(ioh->opaque) != 0)) {
+        if (ioh->fd_read) {
             FD_SET(ioh->fd, &rfds);
             if (ioh->fd > nfds)
                 nfds = ioh->fd;
Signature · 1 line
1.6.6.1
#18 lore raw

PATCH v2 12/14 Remove qemu_set_fd_handler2()

In reply to Juan Quintela

Now that there are no more poll_read users, we can remove it. Convert all users to qemu_set_fd_handler().

---
 aio.c               |    2 +-
 migration-exec.c    |    6 +++---
 migration-fd.c      |    4 ++--
 migration-tcp.c     |   10 +++++-----
 migration-unix.c    |   10 +++++-----
 migration.c         |    8 ++++----
 qemu-aio.h          |    4 ++--
 qemu-char.c         |   12 ++++++------
 qemu-char.h         |    5 -----
 qemu-tool.c         |    9 ++++-----
 vl.c                |   24 ++++++------------------
 vnc-auth-sasl.c     |    2 +-
 vnc-auth-vencrypt.c |    2 +-
 vnc.c               |   12 ++++++------
 14 files changed, 46 insertions(+), 64 deletions(-)
aio.c +1 -1
index f164a47..16105b3 100644
@@ -93,7 +93,7 @@ int qemu_aio_set_fd_handler(int fd,
         node->opaque = opaque;
     }
 
-    qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque);
+    qemu_set_fd_handler(fd, io_read, io_write, opaque);
 
     return 0;
 }
migration-exec.c +3 -3
index 5435827..c971145 100644
@@ -125,7 +125,7 @@ static void exec_accept_incoming_migration(void *opaque)
         vm_start();
 
 err:
-    qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(qemu_stdio_fd(f), NULL, NULL, NULL);
     qemu_fclose(f);
 }
 
@@ -140,8 +140,8 @@ int exec_start_incoming_migration(const char *command)
         return -errno;
     }
 
-    qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
-			 exec_accept_incoming_migration, NULL, f);
+    qemu_set_fd_handler(qemu_stdio_fd(f),
+                        exec_accept_incoming_migration, NULL, f);
 
     return 0;
 }
migration-fd.c +2 -2
index 0abd372..93e1c4e 100644
@@ -118,7 +118,7 @@ static void fd_accept_incoming_migration(void *opaque)
         vm_start();
 
 err:
-    qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(qemu_stdio_fd(f), NULL, NULL, NULL);
     qemu_fclose(f);
 }
 
@@ -136,7 +136,7 @@ int fd_start_incoming_migration(const char *infd)
         return -errno;
     }
 
-    qemu_set_fd_handler2(fd, NULL, fd_accept_incoming_migration, NULL, f);
+    qemu_set_fd_handler(fd, fd_accept_incoming_migration, NULL, f);
 
     return 0;
 }
migration-tcp.c +5 -5
index 95ce722..80ebbc5 100644
@@ -66,7 +66,7 @@ static void tcp_wait_for_connect(void *opaque)
         return;
     }
 
-    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
 
     if (val == 0)
         migrate_fd_connect(s);
@@ -123,7 +123,7 @@ MigrationState *tcp_start_outgoing_migration(Monitor *mon,
             ret = -(s->get_error(s));
 
         if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
-            qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
+            qemu_set_fd_handler(s->fd, NULL, tcp_wait_for_connect, s);
     } while (ret == -EINTR);
 
     if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
@@ -176,7 +176,7 @@ static void tcp_accept_incoming_migration(void *opaque)
 out_fopen:
     qemu_fclose(f);
 out:
-    qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s, NULL, NULL, NULL);
     close(s);
     close(c);
 }
@@ -205,8 +205,8 @@ int tcp_start_incoming_migration(const char *host_port)
     if (listen(s, 1) == -1)
         goto err;
 
-    qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
-                         (void *)(unsigned long)s);
+    qemu_set_fd_handler(s, tcp_accept_incoming_migration, NULL,
+                        (void *)(unsigned long)s);
 
     return 0;
 
migration-unix.c +5 -5
index 49de1b9..891b3a7 100644
@@ -65,7 +65,7 @@ static void unix_wait_for_connect(void *opaque)
         return;
     }
 
-    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
 
     if (val == 0)
         migrate_fd_connect(s);
@@ -118,7 +118,7 @@ MigrationState *unix_start_outgoing_migration(Monitor *mon,
 	    ret = -(s->get_error(s));
 
         if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
-	    qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
+	    qemu_set_fd_handler(s->fd, NULL, unix_wait_for_connect, s);
     } while (ret == -EINTR);
 
     if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
@@ -182,7 +182,7 @@ static void unix_accept_incoming_migration(void *opaque)
 out_fopen:
     qemu_fclose(f);
 out:
-    qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s, NULL, NULL, NULL);
     close(s);
     close(c);
 }
@@ -214,8 +214,8 @@ int unix_start_incoming_migration(const char *path)
         goto err;
     }
 
-    qemu_set_fd_handler2(sock, NULL, unix_accept_incoming_migration, NULL,
-			 (void *)(unsigned long)sock);
+    qemu_set_fd_handler(sock, unix_accept_incoming_migration, NULL,
+                        (void *)(unsigned long)sock);
 
     return 0;
 
migration.c +4 -4
index 05f6cc5..2da715c 100644
@@ -292,7 +292,7 @@ void migrate_fd_error(FdMigrationState *s)
 
 void migrate_fd_cleanup(FdMigrationState *s)
 {
-    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
 
     if (s->file) {
         DPRINTF("closing file\n");
@@ -315,7 +315,7 @@ void migrate_fd_put_notify(void *opaque)
 {
     FdMigrationState *s = opaque;
 
-    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
     qemu_file_put_notify(s->file);
 }
 
@@ -332,7 +332,7 @@ ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
         ret = -(s->get_error(s));
 
     if (ret == -EAGAIN)
-        qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
+        qemu_set_fd_handler(s->fd, NULL, migrate_fd_put_notify, s);
 
     return ret;
 }
@@ -449,6 +449,6 @@ int migrate_fd_close(void *opaque)
 {
     FdMigrationState *s = opaque;
 
-    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
     return s->close(s);
 }
qemu-aio.h +2 -2
index 3bdd749..0630cbe 100644
@@ -43,11 +43,11 @@ void qemu_aio_wait(void);
 int qemu_aio_process_queue(void);
 
 /* Register a file descriptor and associated callbacks.  Behaves very similarly
- * to qemu_set_fd_handler2.  Unlike qemu_set_fd_handler2, these callbacks will
+ * to qemu_set_fd_handler.  Unlike qemu_set_fd_handler, these callbacks will
  * be invoked when using either qemu_aio_wait() or qemu_aio_flush().
  *
  * Code that invokes AIO completion functions should rely on this function
- * instead of qemu_set_fd_handler[2].
+ * instead of qemu_set_fd_handler.
  */
 int qemu_aio_set_fd_handler(int fd,
                             IOHandler *io_read,
qemu-char.c +6 -6
index 9098d79..3635f4e 100644
@@ -554,7 +554,7 @@ static void fd_chr_read(void *opaque)
     size = read(s->fd_in, buf, len);
     if (size == 0) {
         /* FD has been closed. Remove it from the active list.  */
-        qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
+        qemu_set_fd_handler(s->fd_in, NULL, NULL, NULL);
         qemu_chr_event(chr, CHR_EVENT_CLOSED);
         return;
     }
@@ -582,7 +582,7 @@ static void fd_chr_close(struct CharDriverState *chr)
     if (s->fd_in >= 0) {
         if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
         } else {
-            qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
+            qemu_set_fd_handler(s->fd_in, NULL, NULL, NULL);
         }
     }
 
@@ -674,7 +674,7 @@ static void stdio_read(void *opaque)
     size = read(0, buf, 1);
     if (size == 0) {
         /* stdin has been closed. Remove it from the active list.  */
-        qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
+        qemu_set_fd_handler(0, NULL, NULL, NULL);
         qemu_chr_event(chr, CHR_EVENT_CLOSED);
         return;
     }
@@ -730,7 +730,7 @@ static void qemu_chr_close_stdio(struct CharDriverState *chr)
 {
     term_exit();
     stdio_nb_clients--;
-    qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(0, NULL, NULL, NULL);
     fd_chr_close(chr);
 }
 
@@ -883,7 +883,7 @@ static void pty_chr_state(CharDriverState *chr, int connected)
     PtyCharDriver *s = chr->opaque;
 
     if (!connected) {
-        qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
         s->connected = 0;
         s->polling = 0;
         /* (re-)connect poll interval for idle guests: once per second.
@@ -919,7 +919,7 @@ static void pty_chr_close(struct CharDriverState *chr)
 {
     PtyCharDriver *s = chr->opaque;
 
-    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
     close(s->fd);
     qemu_del_timer(s->timer);
     qemu_free_timer(s->timer);
qemu-char.h +0 -5
index 3a9427b..3bad12d 100644
@@ -97,11 +97,6 @@ extern int term_escape_char;
 
 /* async I/O support */
 
-int qemu_set_fd_handler2(int fd,
-                         IOCanReadHandler *fd_read_poll,
-                         IOHandler *fd_read,
-                         IOHandler *fd_write,
-                         void *opaque);
 int qemu_set_fd_handler(int fd,
                         IOHandler *fd_read,
                         IOHandler *fd_write,
qemu-tool.c +4 -5
index 45e6df9..873763e 100644
@@ -90,11 +90,10 @@ void qemu_bh_delete(QEMUBH *bh)
     qemu_free(bh);
 }
 
-int qemu_set_fd_handler2(int fd,
-                         IOCanReadHandler *fd_read_poll,
-                         IOHandler *fd_read,
-                         IOHandler *fd_write,
-                         void *opaque)
+int qemu_set_fd_handler(int fd,
+                        IOHandler *fd_read,
+                        IOHandler *fd_write,
+                        void *opaque)
 {
     return 0;
 }
vl.c +6 -18
index c2ae185..a961710 100644
@@ -2604,13 +2604,10 @@ static QTAILQ_HEAD(, IOHandlerRecord) io_handlers =
     QTAILQ_HEAD_INITIALIZER(io_handlers);
 
 
-/* XXX: fd_read_poll should be suppressed, but an API change is
-   necessary in the character devices to suppress fd_can_read(). */
-int qemu_set_fd_handler2(int fd,
-                         IOCanReadHandler *fd_read_poll,
-                         IOHandler *fd_read,
-                         IOHandler *fd_write,
-                         void *opaque)
+int qemu_set_fd_handler(int fd,
+                        IOHandler *fd_read,
+                        IOHandler *fd_write,
+                        void *opaque)
 {
     IOHandlerRecord *ioh;
 
@@ -2630,7 +2627,6 @@ int qemu_set_fd_handler2(int fd,
         QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);
     found:
         ioh->fd = fd;
-        ioh->fd_read_poll = fd_read_poll;
         ioh->fd_read = fd_read;
         ioh->fd_write = fd_write;
         ioh->opaque = opaque;
@@ -2639,14 +2635,6 @@ int qemu_set_fd_handler2(int fd,
     return 0;
 }
 
-int qemu_set_fd_handler(int fd,
-                        IOHandler *fd_read,
-                        IOHandler *fd_write,
-                        void *opaque)
-{
-    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
-}
-
 #ifdef _WIN32
 /***********************************************************/
 /* Polling handling */
@@ -3252,8 +3240,8 @@ static int qemu_event_init(void)
     if (err < 0)
         goto fail;
 
-    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
-                         (void *)(unsigned long)fds[0]);
+    qemu_set_fd_handler(fds[0], qemu_event_read, NULL,
+                        (void *)(unsigned long)fds[0]);
 
     io_thread_fd = fds[1];
     return 0;
vnc-auth-sasl.c +1 -1
index acaac0c..1e4f77f 100644
@@ -83,7 +83,7 @@ long vnc_client_write_sasl(VncState *vs)
      * SASL encoded output
      */
     if (vs->output.offset == 0) {
-        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
+        qemu_set_fd_handler(vs->csock, vnc_client_read, NULL, vs);
     }
 
     return ret;
vnc-auth-vencrypt.c +1 -1
index 07c1691..a5d2b44 100644
@@ -93,7 +93,7 @@ static int vnc_start_vencrypt_handshake(struct VncState *vs) {
 
     VNC_DEBUG("Handshake done, switching to TLS data mode\n");
     vs->tls.wiremode = VNC_WIREMODE_TLS;
-    qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
+    qemu_set_fd_handler(vs->csock, vnc_client_read, vnc_client_write, vs);
 
     start_auth_vencrypt_subauth(vs);
 
vnc.c +6 -6
index 01353a9..2cfb0eb 100644
@@ -1076,7 +1076,7 @@ static void vnc_disconnect_start(VncState *vs)
 {
     if (vs->csock == -1)
         return;
-    qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
+    qemu_set_fd_handler(vs->csock, NULL, NULL, NULL);
     closesocket(vs->csock);
     vs->csock = -1;
 }
@@ -1218,7 +1218,7 @@ static long vnc_client_write_plain(VncState *vs)
     vs->output.offset -= ret;
 
     if (vs->output.offset == 0) {
-        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
+        qemu_set_fd_handler(vs->csock, vnc_client_read, NULL, vs);
     }
 
     return ret;
@@ -1356,7 +1356,7 @@ void vnc_write(VncState *vs, const void *data, size_t len)
     buffer_reserve(&vs->output, len);
 
     if (vs->csock != -1 && buffer_empty(&vs->output)) {
-        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
+        qemu_set_fd_handler(vs->csock, vnc_client_read, vnc_client_write, vs);
     }
 
     buffer_append(&vs->output, data, len);
@@ -2389,7 +2389,7 @@ static void vnc_connect(VncDisplay *vd, int csock)
     VNC_DEBUG("New client on socket %d\n", csock);
     dcl->idle = 0;
     socket_set_nonblock(vs->csock);
-    qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
+    qemu_set_fd_handler(vs->csock, vnc_client_read, NULL, vs);
 
     vnc_client_cache_addr(vs);
     vnc_qmp_event(vs, QEVENT_VNC_CONNECTED);
@@ -2475,7 +2475,7 @@ void vnc_display_close(DisplayState *ds)
         vs->display = NULL;
     }
     if (vs->lsock != -1) {
-        qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
+        qemu_set_fd_handler(vs->lsock, NULL, NULL, NULL);
         close(vs->lsock);
         vs->lsock = -1;
     }
@@ -2730,5 +2730,5 @@ int vnc_display_open(DisplayState *ds, const char *display)
             vs->display = dpy;
         }
     }
-    return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
+    return qemu_set_fd_handler(vs->lsock, vnc_listen_read, NULL, vs);
 }
Signature · 1 line
1.6.6.1
#20 lore raw

PATCH v2 14/14 Add qemu_remove_fd_handler()

In reply to Juan Quintela

Switch all users of qemu_set_fd_handler(fd, NULL, NULL, NULL) to this new function.

---
 audio/alsaaudio.c |    4 ++--
 audio/ossaudio.c  |    6 +++---
 hw/xen_backend.c  |    4 ++--
 migration-exec.c  |    2 +-
 migration-fd.c    |    2 +-
 migration-tcp.c   |    4 ++--
 migration-unix.c  |    4 ++--
 migration.c       |    6 +++---
 net/socket.c      |    6 +++---
 net/vde.c         |    2 +-
 qemu-char.c       |   24 +++++++++++-------------
 qemu-char.h       |    1 +
 usb-linux.c       |    2 +-
 vl.c              |   19 +++++++++++++------
 vnc.c             |    4 ++--
 15 files changed, 48 insertions(+), 42 deletions(-)
audio/alsaaudio.c +2 -2
index 88344ff..b4437e6 100644
@@ -134,7 +134,7 @@ static void alsa_fini_poll (struct pollhlp *hlp)
 
     if (pfds) {
         for (i = 0; i < hlp->count; ++i) {
-            qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
+            qemu_remove_fd_handler (pfds[i].fd);
         }
         qemu_free (pfds);
     }
@@ -286,7 +286,7 @@ static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
                    pfds[i].events, i, pfds[i].fd, err);
 
             while (i--) {
-                qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
+                qemu_remove_fd_handler (pfds[i].fd);
             }
             qemu_free (pfds);
             return -1;
audio/ossaudio.c +3 -3
index 42bffae..ccabf05 100644
@@ -122,7 +122,7 @@ static void oss_anal_close (int *fdp)
 {
     int err;
 
-    qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
+    qemu_set_fd_handler (*fdp);
     err = close (*fdp);
     if (err) {
         oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
@@ -651,7 +651,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
 
     case VOICE_DISABLE:
         if (hw->poll_mode) {
-            qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
+            qemu_remove_fd_handler (oss->fd);
             hw->poll_mode = 0;
         }
 
@@ -835,7 +835,7 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
     case VOICE_DISABLE:
         if (hw->poll_mode) {
             hw->poll_mode = 0;
-            qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
+            qemu_set_fd_handler (oss->fd);
         }
         break;
     }
hw/xen_backend.c +2 -2
index a2e408f..37d24c5 100644
@@ -635,7 +635,7 @@ int xen_be_init(void)
     return 0;
 
 err:
-    qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
+    qemu_clear_fd_handler(xs_fileno(xenstore));
     xs_daemon_close(xenstore);
     xenstore = NULL;
 
@@ -667,7 +667,7 @@ void xen_be_unbind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port == -1)
 	return;
-    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
+    qemu_remove_fd_handler(xc_evtchn_fd(xendev->evtchndev));
     xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
     xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
     xendev->local_port = -1;
migration-exec.c +1 -1
index c971145..45667d7 100644
@@ -125,7 +125,7 @@ static void exec_accept_incoming_migration(void *opaque)
         vm_start();
 
 err:
-    qemu_set_fd_handler(qemu_stdio_fd(f), NULL, NULL, NULL);
+    qemu_remove_fd_handler(qemu_stdio_fd(f));
     qemu_fclose(f);
 }
 
migration-fd.c +1 -1
index 93e1c4e..9aa4191 100644
@@ -118,7 +118,7 @@ static void fd_accept_incoming_migration(void *opaque)
         vm_start();
 
 err:
-    qemu_set_fd_handler(qemu_stdio_fd(f), NULL, NULL, NULL);
+    qemu_remove_fd_handler(qemu_stdio_fd(f));
     qemu_fclose(f);
 }
 
migration-tcp.c +2 -2
index 80ebbc5..228f279 100644
@@ -66,7 +66,7 @@ static void tcp_wait_for_connect(void *opaque)
         return;
     }
 
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->fd);
 
     if (val == 0)
         migrate_fd_connect(s);
@@ -176,7 +176,7 @@ static void tcp_accept_incoming_migration(void *opaque)
 out_fopen:
     qemu_fclose(f);
 out:
-    qemu_set_fd_handler(s, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s);
     close(s);
     close(c);
 }
migration-unix.c +2 -2
index 891b3a7..c998e95 100644
@@ -65,7 +65,7 @@ static void unix_wait_for_connect(void *opaque)
         return;
     }
 
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->fd);
 
     if (val == 0)
         migrate_fd_connect(s);
@@ -182,7 +182,7 @@ static void unix_accept_incoming_migration(void *opaque)
 out_fopen:
     qemu_fclose(f);
 out:
-    qemu_set_fd_handler(s, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s);
     close(s);
     close(c);
 }
migration.c +3 -3
index 2da715c..282ba99 100644
@@ -292,7 +292,7 @@ void migrate_fd_error(FdMigrationState *s)
 
 void migrate_fd_cleanup(FdMigrationState *s)
 {
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->fd);
 
     if (s->file) {
         DPRINTF("closing file\n");
@@ -315,7 +315,7 @@ void migrate_fd_put_notify(void *opaque)
 {
     FdMigrationState *s = opaque;
 
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->fd);
     qemu_file_put_notify(s->file);
 }
 
@@ -449,6 +449,6 @@ int migrate_fd_close(void *opaque)
 {
     FdMigrationState *s = opaque;
 
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->fd);
     return s->close(s);
 }
net/socket.c +3 -3
index 442a9c7..e47374e 100644
@@ -84,7 +84,7 @@ static void net_socket_send(void *opaque)
     } else if (size == 0) {
         /* end of connection */
     eoc:
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->fd);
         closesocket(s->fd);
         return;
     }
@@ -143,7 +143,7 @@ static void net_socket_send_dgram(void *opaque)
         return;
     if (size == 0) {
         /* end of connection */
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->fd);
         return;
     }
     qemu_send_packet(&s->nc, s->buf, size);
@@ -212,7 +212,7 @@ fail:
 static void net_socket_cleanup(VLANClientState *nc)
 {
     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->fd);
     close(s->fd);
 }
 
net/vde.c +1 -1
index 0b46fa6..598604f 100644
@@ -65,7 +65,7 @@ static ssize_t vde_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
 static void vde_cleanup(VLANClientState *nc)
 {
     VDEState *s = DO_UPCAST(VDEState, nc, nc);
-    qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
+    qemu_remove_fd_handler(vde_datafd(s->vde));
     vde_close(s->vde);
 }
 
qemu-char.c +11 -13
index 3635f4e..8574a0f 100644
@@ -553,8 +553,7 @@ static void fd_chr_read(void *opaque)
         return;
     size = read(s->fd_in, buf, len);
     if (size == 0) {
-        /* FD has been closed. Remove it from the active list.  */
-        qemu_set_fd_handler(s->fd_in, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->fd_in);
         qemu_chr_event(chr, CHR_EVENT_CLOSED);
         return;
     }
@@ -582,7 +581,7 @@ static void fd_chr_close(struct CharDriverState *chr)
     if (s->fd_in >= 0) {
         if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
         } else {
-            qemu_set_fd_handler(s->fd_in, NULL, NULL, NULL);
+            qemu_remove_fd_handler(s->fd_in);
         }
     }
 
@@ -673,8 +672,7 @@ static void stdio_read(void *opaque)
     }
     size = read(0, buf, 1);
     if (size == 0) {
-        /* stdin has been closed. Remove it from the active list.  */
-        qemu_set_fd_handler(0, NULL, NULL, NULL);
+        qemu_remove_fd_handler(0);
         qemu_chr_event(chr, CHR_EVENT_CLOSED);
         return;
     }
@@ -730,7 +728,7 @@ static void qemu_chr_close_stdio(struct CharDriverState *chr)
 {
     term_exit();
     stdio_nb_clients--;
-    qemu_set_fd_handler(0, NULL, NULL, NULL);
+    qemu_remove_fd_handler(0);
     fd_chr_close(chr);
 }
 
@@ -883,7 +881,7 @@ static void pty_chr_state(CharDriverState *chr, int connected)
     PtyCharDriver *s = chr->opaque;
 
     if (!connected) {
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->fd);
         s->connected = 0;
         s->polling = 0;
         /* (re-)connect poll interval for idle guests: once per second.
@@ -919,7 +917,7 @@ static void pty_chr_close(struct CharDriverState *chr)
 {
     PtyCharDriver *s = chr->opaque;
 
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->fd);
     close(s->fd);
     qemu_del_timer(s->timer);
     qemu_free_timer(s->timer);
@@ -1810,7 +1808,7 @@ static void udp_chr_close(CharDriverState *chr)
 {
     NetCharDriver *s = chr->opaque;
     if (s->fd >= 0) {
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->fd);
         closesocket(s->fd);
     }
     qemu_free(s);
@@ -2013,7 +2011,7 @@ static void tcp_chr_read(void *opaque)
         if (s->listen_fd >= 0) {
             qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
         }
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->fd);
         closesocket(s->fd);
         s->fd = -1;
         qemu_chr_event(chr, CHR_EVENT_CLOSED);
@@ -2096,7 +2094,7 @@ static void tcp_chr_accept(void *opaque)
     if (s->do_nodelay)
         socket_set_nodelay(fd);
     s->fd = fd;
-    qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(s->listen_fd);
     tcp_chr_connect(chr);
 }
 
@@ -2104,11 +2102,11 @@ static void tcp_chr_close(CharDriverState *chr)
 {
     TCPCharDriver *s = chr->opaque;
     if (s->fd >= 0) {
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->fd);
         closesocket(s->fd);
     }
     if (s->listen_fd >= 0) {
-        qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
+        qemu_remove_fd_handler(s->listen_fd);
         closesocket(s->listen_fd);
     }
     qemu_free(s);
qemu-char.h +1 -0
index 3bad12d..fcb770b 100644
@@ -101,4 +101,5 @@ int qemu_set_fd_handler(int fd,
                         IOHandler *fd_read,
                         IOHandler *fd_write,
                         void *opaque);
+int qemu_remove_fd_handler(int fd);
 #endif
usb-linux.c +1 -1
index a9c15c6..86d3595 100644
@@ -957,7 +957,7 @@ static int usb_host_close(USBHostDevice *dev)
     if (dev->fd == -1)
         return -1;
 
-    qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
+    qemu_remove_fd_handler(dev->fd);
     dev->closing = 1;
     async_complete(dev);
     dev->closing = 0;
vl.c +13 -6
index 3bf8b87..6cbdf14 100644
@@ -2602,6 +2602,18 @@ typedef struct IOHandlerRecord {
 static QTAILQ_HEAD(, IOHandlerRecord) io_handlers =
     QTAILQ_HEAD_INITIALIZER(io_handlers);
 
+int qemu_remove_fd_handler(int fd)
+{
+    IOHandlerRecord *ioh;
+
+    QTAILQ_FOREACH(ioh, &io_handlers, next) {
+        if (ioh->fd == fd) {
+            ioh->deleted = 1;
+            break;
+        }
+    }
+    return 0;
+}
 
 int qemu_set_fd_handler(int fd,
                         IOHandler *fd_read,
@@ -2611,12 +2623,7 @@ int qemu_set_fd_handler(int fd,
     IOHandlerRecord *ioh;
 
     if (!fd_read && !fd_write) {
-        QTAILQ_FOREACH(ioh, &io_handlers, next) {
-            if (ioh->fd == fd) {
-                ioh->deleted = 1;
-                break;
-            }
-        }
+        qemu_remove_fd_handler(fd);
     } else {
         QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
vnc.c +2 -2
index 2cfb0eb..2a9fcb9 100644
@@ -1076,7 +1076,7 @@ static void vnc_disconnect_start(VncState *vs)
 {
     if (vs->csock == -1)
         return;
-    qemu_set_fd_handler(vs->csock, NULL, NULL, NULL);
+    qemu_remove_fd_handler(vs->csock);
     closesocket(vs->csock);
     vs->csock = -1;
 }
@@ -2475,7 +2475,7 @@ void vnc_display_close(DisplayState *ds)
         vs->display = NULL;
     }
     if (vs->lsock != -1) {
-        qemu_set_fd_handler(vs->lsock, NULL, NULL, NULL);
+        qemu_remove_fd_handler(vs->lsock);
         close(vs->lsock);
         vs->lsock = -1;
     }
Signature · 1 line
1.6.6.1
#2 lore raw

Re: [Qemu-devel] [PATCH 01/14] Convert io handlers to QLIST

In reply to Juan Quintela
Quoted 45 lines · Juan Quintela
---
 vl.c |   35 ++++++++++++++---------------------
 1 files changed, 14 insertions(+), 21 deletions(-)
vl.c +6 -9
index 10d8e34..83ff652 100644
@@ -2597,10 +2597,12 @@ typedef struct IOHandlerRecord {
     void *opaque;
     /* temporary data */
     struct pollfd *ufd;
-    struct IOHandlerRecord *next;
+    QTAILQ_ENTRY(IOHandlerRecord) next;
 } IOHandlerRecord;
 
-static IOHandlerRecord *first_io_handler;
+static QTAILQ_HEAD(, IOHandlerRecord) io_handlers =
+    QTAILQ_HEAD_INITIALIZER(io_handlers);
+
 
 /* XXX: fd_read_poll should be suppressed, but an API change is
    necessary in the character devices to suppress fd_can_read(). */
@@ -2610,28 +2612,22 @@ int qemu_set_fd_handler2(int fd,
                          IOHandler *fd_write,
                          void *opaque)
 {
-    IOHandlerRecord **pioh, *ioh;
+    IOHandlerRecord *ioh;
 
     if (!fd_read && !fd_write) {
-        pioh = &first_io_handler;
-        for(;;) {
-            ioh = *pioh;
-            if (ioh == NULL)
-                break;
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd) {
                 ioh->deleted = 1;
                 break;
             }
-            pioh = &ioh->next;
         }

vl.c int qemu_set_fd_handler2(int fd, · line 2630

Quoted 10 lines
vl.c +2 -3
     } else {
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
+        QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);

The old code inserted at the head, didn't it?

Quoted 43 lines
vl.c +6 -9
     found:
         ioh->fd = fd;
         ioh->fd_read_poll = fd_read_poll;
@@ -3822,7 +3818,7 @@ void main_loop_wait(int timeout)
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&xfds);
-    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+    QTAILQ_FOREACH(ioh, &io_handlers, next) {
         if (ioh->deleted)
             continue;
         if (ioh->fd_read &&
@@ -3848,9 +3844,9 @@ void main_loop_wait(int timeout)
     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
     qemu_mutex_lock_iothread();
     if (ret > 0) {
-        IOHandlerRecord **pioh;
+        IOHandlerRecord *pioh;
 
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
                 ioh->fd_read(ioh->opaque);
             }
@@ -3860,14 +3856,11 @@ void main_loop_wait(int timeout)
         }
 
 	/* remove deleted IO handlers */
-	pioh = &first_io_handler;
-	while (*pioh) {
-            ioh = *pioh;
+        QTAILQ_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
             if (ioh->deleted) {
-                *pioh = ioh->next;
+                QTAILQ_REMOVE(&io_handlers, ioh, next);
                 qemu_free(ioh);
-            } else
-                pioh = &ioh->next;
+            }
         }
     }
 
Signature · 1 line
mailto:[email protected]
#3 lore raw

[Qemu-devel] Re: [PATCH 01/14] Convert io handlers to QLIST

Quoted 12 lines · malc

vl.c int qemu_set_fd_handler2(int fd, · line 2630

Quoted 9 lines · Juan Quintela
vl.c +2 -3
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
+        QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);
Quoted 1 line

The old code inserted at the head, didn't it?

Sorry, you are right, it shouldn't matter too much, but it is a change.

Later, Juan.

#4 lore raw

[Qemu-devel] Re: [PATCH 01/14] Convert io handlers to QLIST

Quoted 13 lines · Juan Quintela
Quoted 12 lines · malc
Quoted 9 lines · Juan Quintela
vl.c +2 -3
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
+        QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);
Quoted 3 lines
Quoted 1 line

The old code inserted at the head, didn't it?

Sorry, you are right, it shouldn't matter too much, but it is a change.

If it did, why queue instead of list?

Quoted 3 lines

Later, Juan.

Signature · 1 line
mailto:[email protected]
#5 lore raw

[Qemu-devel] Re: [PATCH 01/14] Convert io handlers to QLIST

Quoted 15 lines · malc
Quoted 13 lines · Juan Quintela
Quoted 12 lines · malc
Quoted 9 lines · Juan Quintela
vl.c +2 -3
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
+        QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

The old code inserted at the head, didn't it?

Sorry, you are right, it shouldn't matter too much, but it is a change.

If it did, why queue instead of list?

Arbitrary. Example conversion nearer was QTAIL.

Use is:
- insert at the beggining
- search for removal
- loop all list

insert/searchs should be less than loops. I am more interested/intrigued if setting more fd's in the select call could change behaviour.

Changing to QLIST is search/replace, no big deal.

Quoted 3 lines
Quoted 3 lines

Later, Juan.

#6 lore raw

[Qemu-devel] Re: [PATCH 01/14] Convert io handlers to QLIST

Quoted 16 lines · Juan Quintela
Quoted 15 lines · malc
Quoted 13 lines · Juan Quintela
Quoted 12 lines · malc
Quoted 9 lines · Juan Quintela
vl.c +2 -3
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
+        QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

The old code inserted at the head, didn't it?

Sorry, you are right, it shouldn't matter too much, but it is a change.

If it did, why queue instead of list?

Arbitrary. Example conversion nearer was QTAIL.

Please do `man 3 queue'. Specifically the comparison between the tail queues and lists.

Quoted 16 lines

Use is:
- insert at the beggining
- search for removal
- loop all list

insert/searchs should be less than loops. I am more interested/intrigued if setting more fd's in the select call could change behaviour.

Changing to QLIST is search/replace, no big deal.

Quoted 3 lines
Quoted 3 lines

Later, Juan.

Signature · 1 line
mailto:[email protected]
#7 lore raw

[Qemu-devel] Re: [PATCH 01/14] Convert io handlers to QLIST

Quoted 20 lines · malc
Quoted 18 lines · Juan Quintela
Quoted 17 lines · malc
Quoted 15 lines · Juan Quintela
Quoted 13 lines · malc

vl.c int qemu_set_fd_handler2(int fd, · line 2630

Quoted 9 lines · Juan Quintela
vl.c +2 -3
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+        QTAILQ_FOREACH(ioh, &io_handlers, next) {
             if (ioh->fd == fd)
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
+        QTAILQ_INSERT_TAIL(&io_handlers, ioh, next);

The old code inserted at the head, didn't it?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Sorry, you are right, it shouldn't matter too much, but it is a change.

If it did, why queue instead of list?

Arbitrary. Example conversion nearer was QTAIL.

Please do `man 3 queue'. Specifically the comparison between the tail queues and lists.

Thanks very much for the info. Didn't knew that page.

You win. Will change to QLISTS. Waiting for more comments.

Later, Juan.