Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * event notifier support |
| 3 | * |
| 4 | * Copyright Red Hat, Inc. 2010 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Michael S. Tsirkin <mst@redhat.com> |
| 8 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 13 | #include "qemu-common.h" |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 14 | #include "event_notifier.h" |
Paolo Bonzini | 6bf819f | 2012-07-05 17:16:28 +0200 | [diff] [blame] | 15 | #include "qemu-char.h" |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 16 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 17 | #ifdef CONFIG_EVENTFD |
| 18 | #include <sys/eventfd.h> |
| 19 | #endif |
| 20 | |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 21 | void event_notifier_init_fd(EventNotifier *e, int fd) |
| 22 | { |
| 23 | e->fd = fd; |
| 24 | } |
| 25 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 26 | int event_notifier_init(EventNotifier *e, int active) |
| 27 | { |
| 28 | #ifdef CONFIG_EVENTFD |
| 29 | int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC); |
| 30 | if (fd < 0) |
| 31 | return -errno; |
| 32 | e->fd = fd; |
| 33 | return 0; |
| 34 | #else |
| 35 | return -ENOSYS; |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | void event_notifier_cleanup(EventNotifier *e) |
| 40 | { |
| 41 | close(e->fd); |
| 42 | } |
| 43 | |
| 44 | int event_notifier_get_fd(EventNotifier *e) |
| 45 | { |
| 46 | return e->fd; |
| 47 | } |
| 48 | |
Paolo Bonzini | 6bf819f | 2012-07-05 17:16:28 +0200 | [diff] [blame] | 49 | int event_notifier_set_handler(EventNotifier *e, |
| 50 | EventNotifierHandler *handler) |
| 51 | { |
| 52 | return qemu_set_fd_handler(e->fd, (IOHandler *)handler, NULL, e); |
| 53 | } |
| 54 | |
Paolo Bonzini | 2ec10b9 | 2012-07-05 17:16:22 +0200 | [diff] [blame] | 55 | int event_notifier_set(EventNotifier *e) |
| 56 | { |
| 57 | uint64_t value = 1; |
| 58 | int r = write(e->fd, &value, sizeof(value)); |
| 59 | return r == sizeof(value); |
| 60 | } |
| 61 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 62 | int event_notifier_test_and_clear(EventNotifier *e) |
| 63 | { |
| 64 | uint64_t value; |
| 65 | int r = read(e->fd, &value, sizeof(value)); |
| 66 | return r == sizeof(value); |
| 67 | } |