2e83913
event notifiers are slightly generalized eventfd descriptors. Current
2e83913
implementation depends on eventfd because vhost is the only user, and
2e83913
vhost depends on eventfd anyway, but a stub is provided for non-eventfd
2e83913
case.
2e83913
2e83913
We'll be able to further generalize this when another user comes along
2e83913
and we see how to best do this.
2e83913
2e83913
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
2e83913
---
2e83913
 Makefile.target |    1 +
2e83913
 hw/notifier.c   |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
2e83913
 hw/notifier.h   |   16 ++++++++++++++++
2e83913
 qemu-common.h   |    1 +
2e83913
 4 files changed, 68 insertions(+), 0 deletions(-)
2e83913
 create mode 100644 hw/notifier.c
2e83913
 create mode 100644 hw/notifier.h
2e83913
2e83913
diff --git a/Makefile.target b/Makefile.target
2e83913
index 6037fed..0c844a9 100644
2e83913
--- a/Makefile.target
2e83913
+++ b/Makefile.target
2e83913
@@ -167,6 +167,7 @@ obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o
2e83913
 # virtio has to be here due to weird dependency between PCI and virtio-net.
2e83913
 # need to fix this properly
2e83913
 obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o virtio-serial-bus.o
2e83913
+obj-y += notifier.o
2e83913
 obj-$(CONFIG_KVM) += kvm.o kvm-all.o
2e83913
 # MSI-X depends on kvm for interrupt injection,
2e83913
 # so moved it from Makefile.hw to Makefile.target for now
2e83913
diff --git a/hw/notifier.c b/hw/notifier.c
2e83913
new file mode 100644
2e83913
index 0000000..dff38de
2e83913
--- /dev/null
2e83913
+++ b/hw/notifier.c
2e83913
@@ -0,0 +1,50 @@
2e83913
+#include "hw.h"
2e83913
+#include "notifier.h"
2e83913
+#ifdef CONFIG_EVENTFD
2e83913
+#include <sys/eventfd.h>
2e83913
+#endif
2e83913
+
2e83913
+int event_notifier_init(EventNotifier *e, int active)
2e83913
+{
2e83913
+#ifdef CONFIG_EVENTFD
2e83913
+	int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
2e83913
+	if (fd < 0)
2e83913
+		return -errno;
2e83913
+	e->fd = fd;
2e83913
+	return 0;
2e83913
+#else
2e83913
+	return -ENOSYS;
2e83913
+#endif
2e83913
+}
2e83913
+
2e83913
+void event_notifier_cleanup(EventNotifier *e)
2e83913
+{
2e83913
+	close(e->fd);
2e83913
+}
2e83913
+
2e83913
+int event_notifier_get_fd(EventNotifier *e)
2e83913
+{
2e83913
+	return e->fd;
2e83913
+}
2e83913
+
2e83913
+int event_notifier_test_and_clear(EventNotifier *e)
2e83913
+{
2e83913
+	uint64_t value;
2e83913
+	int r = read(e->fd, &value, sizeof value);
2e83913
+	return r == sizeof value;
2e83913
+}
2e83913
+
2e83913
+int event_notifier_test(EventNotifier *e)
2e83913
+{
2e83913
+	uint64_t value;
2e83913
+	int r = read(e->fd, &value, sizeof value);
2e83913
+	if (r == sizeof value) {
2e83913
+		/* restore previous value. */
2e83913
+		int s = write(e->fd, &value, sizeof value);
2e83913
+		/* never blocks because we use EFD_SEMAPHORE.
2e83913
+		 * If we didn't we'd get EAGAIN on overflow
2e83913
+		 * and we'd have to write code to ignore it. */
2e83913
+		assert(s == sizeof value);
2e83913
+	}
2e83913
+	return r == sizeof value;
2e83913
+}
2e83913
diff --git a/hw/notifier.h b/hw/notifier.h
2e83913
new file mode 100644
2e83913
index 0000000..24117ea
2e83913
--- /dev/null
2e83913
+++ b/hw/notifier.h
2e83913
@@ -0,0 +1,16 @@
2e83913
+#ifndef QEMU_EVENT_NOTIFIER_H
2e83913
+#define QEMU_EVENT_NOTIFIER_H
2e83913
+
2e83913
+#include "qemu-common.h"
2e83913
+
2e83913
+struct EventNotifier {
2e83913
+	int fd;
2e83913
+};
2e83913
+
2e83913
+int event_notifier_init(EventNotifier *, int active);
2e83913
+void event_notifier_cleanup(EventNotifier *);
2e83913
+int event_notifier_get_fd(EventNotifier *);
2e83913
+int event_notifier_test_and_clear(EventNotifier *);
2e83913
+int event_notifier_test(EventNotifier *);
2e83913
+
2e83913
+#endif
2e83913
diff --git a/qemu-common.h b/qemu-common.h
2e83913
index 5fbe0f9..cdead98 100644
2e83913
--- a/qemu-common.h
2e83913
+++ b/qemu-common.h
2e83913
@@ -217,6 +217,7 @@ typedef struct uWireSlave uWireSlave;
2e83913
 typedef struct I2SCodec I2SCodec;
2e83913
 typedef struct DeviceState DeviceState;
2e83913
 typedef struct SSIBus SSIBus;
2e83913
+typedef struct EventNotifier EventNotifier;
2e83913
 
2e83913
 /* CPU save/load.  */
2e83913
 void cpu_save(QEMUFile *f, void *opaque);
2e83913
-- 
2e83913
1.6.6.144.g5c3af