[PATCH 3/6] kvm: qemu: add qemu_eventfd_write() and qemu_eventfd_read()

Mark McLoughlin
2008-10-30T17:53:23Z

Split out code for common eventfd operations to avoid
re-implementation.

Signed-off-by: Mark McLoughlin <markmc@redhat.com>
---
qemu/compatfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
qemu/compatfd.h | 2 ++
qemu/qemu-kvm.c | 42 ++++--------------------------------------
3 files changed, 54 insertions(+), 38 deletions(-)

diff --git a/qemu/compatfd.c b/qemu/compatfd.c
index 36e37e5..7e3e7f7 100644
--- a/qemu/compatfd.c
+++ b/qemu/compatfd.c
@@ -129,3 +129,51 @@ int qemu_eventfd(int *fds)

return pipe(fds);
}
+
+int qemu_eventfd_write(int eventfd, uint64_t value)
+{
+ char buffer[8];
+ size_t offset = 0;
+
+ memcpy(buffer, &value, sizeof(value));
+
+ while (offset < 8) {
+ ssize_t len;
+
+ len = write(eventfd, buffer + offset, 8 - offset);
+ if (len == -1 && errno == EINTR)
+ continue;
+
+ if (len <= 0)
+ break;
+
+ offset += len;
+ }
+
+ return offset == 8 ? 0 : -1;
+}
+
+uint64_t qemu_eventfd_read(int eventfd)
+{
+ char buffer[8];
+ size_t offset = 0;
+ uint64_t value = 0;
+
+ while (offset < 8) {
+ ssize_t len;
+
+ len = read(eventfd, buffer + offset, 8 - offset);
+ if (len == -1 && errno == EINTR)
+ continue;
+
+ if (len <= 0)
+ break;
+
+ offset += len;
+ }
+
+ if (offset == 8)
+ memcpy(&value, buffer, sizeof(value));
+
+ return value;
+}
diff --git a/qemu/compatfd.h b/qemu/compatfd.h
index 55a111a..b2792e6 100644
--- a/qemu/compatfd.h
+++ b/qemu/compatfd.h
@@ -24,5 +24,7 @@ struct qemu_signalfd_siginfo {
int qemu_signalfd(const sigset_t *mask);

int qemu_eventfd(int *fds);
+int qemu_eventfd_write(int eventfd, uint64_t value);
+uint64_t qemu_eventfd_read(int eventfd);

#endif
diff --git a/qemu/qemu-kvm.c b/qemu/qemu-kvm.c
index c5f3f29..70c6802 100644
--- a/qemu/qemu-kvm.c
+++ b/qemu/qemu-kvm.c
@@ -485,30 +485,11 @@ int kvm_init_ap(void)

void qemu_kvm_notify_work(void)
{
- uint64_t value = 1;
- char buffer[8];
- size_t offset = 0;
-
if (io_thread_fd == -1)
- return;
-
- memcpy(buffer, &value, sizeof(value));
-
- while (offset < 8) {
- ssize_t len;
-
- len = write(io_thread_fd, buffer + offset, 8 - offset);
- if (len == -1 && errno == EINTR)
- continue;
-
- if (len <= 0)
- break;
-
- offset += len;
- }
+ return;

- if (offset != 8)
- fprintf(stderr, "failed to notify io thread\n");
+ if (qemu_eventfd_write(io_thread_fd, 1) != 0)
+ fprintf(stderr, "failed to notify io thread\n");
}

/* If we have signalfd, we mask out the signals we want to handle and then
@@ -546,22 +527,7 @@ static void sigfd_handler(void *opaque)
/* Used to break IO thread out of select */
static void io_thread_wakeup(void *opaque)
{
- int fd = (unsigned long)opaque;
- char buffer[8];
- size_t offset = 0;
-
- while (offset < 8) {
- ssize_t len;
-
- len = read(fd, buffer + offset, 8 - offset);
- if (len == -1 && errno == EINTR)
- continue;
-
- if (len <= 0)
- break;
-
- offset += len;
- }
+ qemu_eventfd_read((unsigned long)opaque);
}

int kvm_main_loop(void)
--
1.5.4.3