In the [QEMU Internals Overall Architecture Blogpost](
https://blog.vmsplice.net/2011/03/qemu-internals-overall-architecture-and.html),
the following is stated:
> Although many I/O operations can be performed in a non-blocking fashion,
there are system calls which have no non-blocking equivalent. Furthermore,
sometimes long-running computations simply hog the CPU and are difficult to
break up into callbacks. In these cases dedicated worker threads can be
used to carefully move these tasks out of core QEMU.

The blog stated example implementations are no longer available in QEMU 11
to gain insight on how worker threads are used.

For my use case, I am trying to trigger external file descriptor writes and
wait for the corresponding read based on vCPU instructions (i.e.
register/MMIO rmw operations). In my current implementation register
read/write operations are redirected to the following function:
``` C
MemTxResult fd_mmio_rmw(hwaddr addr, bool is_write, int bytes,
                        uint64_t wdata, uint64_t *rdata,
                        CharFrontend *fe)
{
    int chardev_status;

    if (!qemu_chr_fe_backend_open(fe)) {
        /* Charbackend is not open */
        return MEMTX_ERROR;
    }

    chardev_status = qemu_chr_fe_write_all(fe, (uint8_t *)wdata, bytes);
    if (chardev_status == -1) {
        /* Unable to write to socket */
        return MEMTX_ERROR;
    }

    chardev_status = qemu_chr_fe_read_all(fe, (uint8_t*)rdata, bytes);
    if (chardev_status < 0) {
        return MEMTX_ERROR;
    }
    return MEMTX_OK;
}
```
But this function blocks the main event loop due to the blocking chardev
frontend APIs. How can:

1. The function be deferred to a worker thread as mentioned in the blog,
while keeping the vCPU triggering this request halted but the main event
loop running?
2. The worker threads be synchronized with the main loop (i.e. how would it
inform the main loop to give control to vCPU executing instructions ONLY
after the FD read is completed)?

Regards,
Irtaza

Reply via email to