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