qemu-char: eliminate busy waiting on can_read returning zero
commitd185c094b404b4ff392b77d1244c0233da7d53bd
authorPaolo Bonzini <pbonzini@redhat.com>
Fri, 5 Apr 2013 15:59:33 +0000 (5 17:59 +0200)
committerAnthony Liguori <aliguori@us.ibm.com>
Fri, 5 Apr 2013 17:53:07 +0000 (5 12:53 -0500)
tree593c1e12e5963c6f64f5ba1e2c85009cc90868bc
parent91b53e4407ed8379d2d40f88a585e0b767681927
qemu-char: eliminate busy waiting on can_read returning zero

The character backend refactoring introduced an undesirable busy wait.
The busy wait happens if can_read returns zero and there is data available
on the character device's file descriptor.  Then, the I/O watch will
fire continuously and, with TCG, the CPU thread will never run.

    1) Char backend asks front end if it can write
    2) Front end says no
    3) poll() finds the char backend's descriptor is available
    4) Goto (1)

What we really want is this (note that step 3 avoids the busy wait):

    1) Char backend asks front end if it can write
    2) Front end says no
    3) poll() goes on without char backend's descriptor
    4) Goto (1) until qemu_chr_accept_input() called

    5) Char backend asks front end if it can write
    6) Front end says yes
    7) poll() finds the char backend's descriptor is available
    8) Backend handler called

After this patch, the IOWatchPoll source and the watch source are
separated.  The IOWatchPoll is simply a hook that runs during the prepare
phase on each main loop iteration.  The hook adds/removes the actual
source depending on the return value from can_read.

A simple reproducer is

    qemu-system-i386 -serial mon:stdio

... followed by banging on the terminal as much as you can. :)  Without
this patch, emulation will hang.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-id: 1365177573-11817-1-git-send-email-pbonzini@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
qemu-char.c