event_notifier: add event_notifier_set

EventNotifier right now cannot be used as an inter-thread communication
primitive.  It only works if something else (the kernel) sets the eventfd.
Add a primitive to signal an EventNotifier that another thread is waiting
on.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
event_notifier: remove event_notifier_test

This is broken; since the eventfd is used in nonblocking mode there
is a race between reading and writing.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
event_notifier: add event_notifier_init_fd

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ivshmem: use EventNotifier and memory API

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ivshmem: wrap ivshmem_del_eventfd loops with transaction

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
memory: pass EventNotifier, not eventfd

Under Win32, EventNotifiers do not have event_notifier_get_fd, so we cannot
call it in common code such as hw/virtio-pci.c.  Pass a pointer to the
notifier, and only retrieve the file descriptor in kvm-specific code.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
event_notifier: add event_notifier_set_handler

Win32 event notifiers are not file descriptors, so they cannot use
qemu_set_fd_handler.  Add a specific function that wraps the call,
and make it pass the EventNotifier as the opaque value so that it
will be used with container_of.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virtio: move common ioeventfd handling out of virtio-pci

All transports can use the same event handler for the ioeventfd, though
the exact addresses will be specific.

This lets virtio use event_notifier_set_handler.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virtio: move common irqfd handling out of virtio-pci

All transports can use the same event handler for the ioeventfd, though
the exact mechanics of the assignment will be specific.  Note that there
are three states: handled by the kernel, handled in userspace, disabled.

This also lets virtio use event_notifier_set_handler.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
event_notifier: enable it to use pipes

This takes the eventfd emulation code from the main loop.
When the EventNotifier is used for the main loop too, we
need this compatibility code.

event_notifier_init_fd and event_notifier_get_fd are used
for low-level code talking to KVM, so make them dependent
on the "real" eventfd.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
event_notifier: add Win32 implementation

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
main-loop: use event notifiers

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: provide platform-independent API

This adds to aio.c a platform-independent API based on EventNotifiers, that
can be used by the portable thread pool.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: add Win32 implementation

The Win32 implementation only accepts EventNotifiers, thus a few
drivers are disabled under Windows.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
linux-aio: use event notifiers

Since linux-aio already uses an eventfd, converting it to use the
EventNotifier-based API simplifies the code even though it is not
meant to be portable.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-thread: add QemuSemaphore

The new thread pool will use semaphores instead of condition
variables, because QemuCond does not have qemu_cond_timedwait.
(I also like it more this way).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: add generic thread-pool facility

Add a generic thread-pool.  The code is roughly based on posix-aio-compat.c,
with some changes, especially the following:

- use QemuSemaphore instead of QemuCond;

- separate the state of the thread from the return code of the worker
function.  The return code is totally opaque for the thread pool;

- do not busy wait when doing cancellation.

A more generic threadpool (but still specific to I/O so that in the future
it can use special scheduling classes or PI mutexes) can have many uses:
it allows more flexibility in raw-posix.c and can more easily be extended
to Win32, and it will also be used to do an msync of the persistent bitmap.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: switch posix-aio-compat to threadpool

This is not meant for portability, but to remove code duplication.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw: merge posix-aio-compat.c into block/raw-posix.c

Making the qemu_paiocb specific to raw devices will let us access members
of the BDRVRawState arbitrarily.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw-posix: rename raw-posix-aio.h, hide unavailable prototypes

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>