From patchwork Tue Aug 7 11:17:18 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 175601 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 620172C0084 for ; Tue, 7 Aug 2012 21:18:26 +1000 (EST) Received: from localhost ([::1]:34654 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syhns-0001oz-8Y for incoming@patchwork.ozlabs.org; Tue, 07 Aug 2012 07:18:24 -0400 Received: from eggs.gnu.org ([208.118.235.92]:56342) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyhnS-0001Q6-03 for qemu-devel@nongnu.org; Tue, 07 Aug 2012 07:18:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SyhnP-0006bI-Mp for qemu-devel@nongnu.org; Tue, 07 Aug 2012 07:17:57 -0400 Received: from mail-yw0-f45.google.com ([209.85.213.45]:59352) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyhnP-0006b2-HX for qemu-devel@nongnu.org; Tue, 07 Aug 2012 07:17:55 -0400 Received: by yhpp34 with SMTP id p34so3424824yhp.4 for ; Tue, 07 Aug 2012 04:17:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=w9V5YuFFu6qIiIzQQQIOgekPHo8nTsDc9OOEkp47Wcc=; b=jWGnL/O9MenMnUx1yPR6QOC2bc3IhL1RYeSV2e7g/oJrqwikO4afflBbfDHAuOFJ8H 1j5IJKAmvUhVEyyaXPpsNvFtJMEC3LNI1dKEWHgyzqBZTuabwYdgS1Mj1i39t5PzTMCg jzU33xoWSmcdy0dT2Wd6Z64G32AzoinWMg6wYvLflJu/ejkAz4iTxpYJAUattfWqonpe hoa62yh9CVkQeC93QTSEzamRhbtpEdfCnaVchirGUKRJE3koFxb5WrBW+AzYlaMHJEDl VCJcyu58VZSLsPQmwKtQY8zvp2hS5ZwrzG3Pyh+vLf0AlHWz45d/L7HTIutQVznVwg2M L2zQ== Received: by 10.66.75.97 with SMTP id b1mr25416314paw.15.1344338274440; Tue, 07 Aug 2012 04:17:54 -0700 (PDT) Received: from yakj.usersys.redhat.com (93-34-169-1.ip50.fastwebnet.it. [93.34.169.1]) by mx.google.com with ESMTPS id pq1sm11008602pbb.3.2012.08.07.04.17.50 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 07 Aug 2012 04:17:53 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 7 Aug 2012 13:17:18 +0200 Message-Id: <1344338247-17567-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.11.2 In-Reply-To: <1344338247-17567-1-git-send-email-pbonzini@redhat.com> References: <1344338247-17567-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.213.45 Cc: aliguori@linux.vnet.ibm.com, stefanha@linux.vnet.ibm.com, sw@weilnetz.de Subject: [Qemu-devel] [PATCH v2 03/12] main-loop: use event notifiers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Reviewed-by: Anthony Liguori Signed-off-by: Paolo Bonzini --- Makefile.objs | 4 +-- main-loop.c | 106 +++++++++------------------------------------------------- oslib-posix.c | 31 ----------------- qemu-common.h | 1 - 4 file modificati, 17 inserzioni(+), 125 rimozioni(-) diff --git a/Makefile.objs b/Makefile.objs index 2d9d0ce..e6d40da 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -20,8 +20,8 @@ universal-obj-y += $(qom-obj-y) ####################################################################### # oslib-obj-y is code depending on the OS (win32 vs posix) oslib-obj-y = osdep.o -oslib-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o -oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o +oslib-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o event_notifier-win32.o +oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o event_notifier-posix.o ####################################################################### # coroutines diff --git a/main-loop.c b/main-loop.c index eb3b6e6..81f49b3 100644 --- a/main-loop.c +++ b/main-loop.c @@ -26,75 +26,12 @@ #include "qemu-timer.h" #include "slirp/slirp.h" #include "main-loop.h" +#include "event_notifier.h" #ifndef _WIN32 #include "compatfd.h" -static int io_thread_fd = -1; - -void qemu_notify_event(void) -{ - /* Write 8 bytes to be compatible with eventfd. */ - static const uint64_t val = 1; - ssize_t ret; - - if (io_thread_fd == -1) { - return; - } - do { - ret = write(io_thread_fd, &val, sizeof(val)); - } while (ret < 0 && errno == EINTR); - - /* EAGAIN is fine, a read must be pending. */ - if (ret < 0 && errno != EAGAIN) { - fprintf(stderr, "qemu_notify_event: write() failed: %s\n", - strerror(errno)); - exit(1); - } -} - -static void qemu_event_read(void *opaque) -{ - int fd = (intptr_t)opaque; - ssize_t len; - char buffer[512]; - - /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ - do { - len = read(fd, buffer, sizeof(buffer)); - } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); -} - -static int qemu_event_init(void) -{ - int err; - int fds[2]; - - err = qemu_eventfd(fds); - if (err == -1) { - return -errno; - } - err = fcntl_setfl(fds[0], O_NONBLOCK); - if (err < 0) { - goto fail; - } - err = fcntl_setfl(fds[1], O_NONBLOCK); - if (err < 0) { - goto fail; - } - qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, - (void *)(intptr_t)fds[0]); - - io_thread_fd = fds[1]; - return 0; - -fail: - close(fds[0]); - close(fds[1]); - return err; -} - /* If we have signalfd, we mask out the signals we want to handle and then * use signalfd to listen for them. We rely on whatever the current signal * handler is to dispatch the signals when we receive them. @@ -164,40 +101,22 @@ static int qemu_signal_init(void) #else /* _WIN32 */ -static HANDLE qemu_event_handle = NULL; - -static void dummy_event_handler(void *opaque) -{ -} - -static int qemu_event_init(void) +static int qemu_signal_init(void) { - qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); - if (!qemu_event_handle) { - fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); - return -1; - } - qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); return 0; } +#endif + +static EventNotifier io_thread_notifier; +static int io_thread_initialized; void qemu_notify_event(void) { - if (!qemu_event_handle) { + if (!io_thread_initialized) { return; } - if (!SetEvent(qemu_event_handle)) { - fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n", - GetLastError()); - exit(1); - } -} - -static int qemu_signal_init(void) -{ - return 0; + event_notifier_set(&io_thread_notifier); } -#endif int main_loop_init(void) { @@ -210,11 +129,15 @@ int main_loop_init(void) } /* Note eventfd must be drained before signalfd handlers run */ - ret = qemu_event_init(); + ret = event_notifier_init(&io_thread_notifier, 0); if (ret) { return ret; } + io_thread_initialized = true; + event_notifier_set_handler(&io_thread_notifier, + (EventNotifierHandler *) + event_notifier_test_and_clear); return 0; } @@ -400,7 +323,8 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) void qemu_fd_register(int fd) { - WSAEventSelect(fd, qemu_event_handle, FD_READ | FD_ACCEPT | FD_CLOSE | + WSAEventSelect(fd, event_notifier_get_handle(&io_thread_notifier), + FD_READ | FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_WRITE | FD_OOB); } diff --git a/oslib-posix.c b/oslib-posix.c index dbeb627..9db9c3d 100644 --- a/oslib-posix.c +++ b/oslib-posix.c @@ -61,9 +61,6 @@ static int running_on_valgrind = -1; #ifdef CONFIG_LINUX #include #endif -#ifdef CONFIG_EVENTFD -#include -#endif int qemu_get_thread_id(void) { @@ -183,34 +180,6 @@ int qemu_pipe(int pipefd[2]) return ret; } -/* - * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set. - */ -int qemu_eventfd(int fds[2]) -{ -#ifdef CONFIG_EVENTFD - int ret; - - ret = eventfd(0, 0); - if (ret >= 0) { - fds[0] = ret; - fds[1] = dup(ret); - if (fds[1] == -1) { - close(ret); - return -1; - } - qemu_set_cloexec(ret); - qemu_set_cloexec(fds[1]); - return 0; - } - if (errno != ENOSYS) { - return -1; - } -#endif - - return qemu_pipe(fds); -} - int qemu_utimens(const char *path, const struct timespec *times) { struct timeval tv[2], tv_now; diff --git a/qemu-common.h b/qemu-common.h index f16079f..162591b 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -215,7 +215,6 @@ ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags) QEMU_WARN_UNUSED_RESULT; #ifndef _WIN32 -int qemu_eventfd(int pipefd[2]); int qemu_pipe(int pipefd[2]); #endif