Statistics
| Branch: | Revision:

root / event_notifier.c @ 13ef70f6

History | View | Annotate | Download (1.3 kB)

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