Statistics
| Branch: | Revision:

root / hw / event_notifier.c @ 8ca209ad

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 2292b339 Michael S. Tsirkin
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 2292b339 Michael S. Tsirkin
 * the COPYING file in the top-level directory.
11 2292b339 Michael S. Tsirkin
 */
12 2292b339 Michael S. Tsirkin
13 2292b339 Michael S. Tsirkin
#include "hw.h"
14 2292b339 Michael S. Tsirkin
#include "event_notifier.h"
15 2292b339 Michael S. Tsirkin
#ifdef CONFIG_EVENTFD
16 2292b339 Michael S. Tsirkin
#include <sys/eventfd.h>
17 2292b339 Michael S. Tsirkin
#endif
18 2292b339 Michael S. Tsirkin
19 2292b339 Michael S. Tsirkin
int event_notifier_init(EventNotifier *e, int active)
20 2292b339 Michael S. Tsirkin
{
21 2292b339 Michael S. Tsirkin
#ifdef CONFIG_EVENTFD
22 2292b339 Michael S. Tsirkin
    int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
23 2292b339 Michael S. Tsirkin
    if (fd < 0)
24 2292b339 Michael S. Tsirkin
        return -errno;
25 2292b339 Michael S. Tsirkin
    e->fd = fd;
26 2292b339 Michael S. Tsirkin
    return 0;
27 2292b339 Michael S. Tsirkin
#else
28 2292b339 Michael S. Tsirkin
    return -ENOSYS;
29 2292b339 Michael S. Tsirkin
#endif
30 2292b339 Michael S. Tsirkin
}
31 2292b339 Michael S. Tsirkin
32 2292b339 Michael S. Tsirkin
void event_notifier_cleanup(EventNotifier *e)
33 2292b339 Michael S. Tsirkin
{
34 2292b339 Michael S. Tsirkin
    close(e->fd);
35 2292b339 Michael S. Tsirkin
}
36 2292b339 Michael S. Tsirkin
37 2292b339 Michael S. Tsirkin
int event_notifier_get_fd(EventNotifier *e)
38 2292b339 Michael S. Tsirkin
{
39 2292b339 Michael S. Tsirkin
    return e->fd;
40 2292b339 Michael S. Tsirkin
}
41 2292b339 Michael S. Tsirkin
42 2292b339 Michael S. Tsirkin
int event_notifier_test_and_clear(EventNotifier *e)
43 2292b339 Michael S. Tsirkin
{
44 2292b339 Michael S. Tsirkin
    uint64_t value;
45 2292b339 Michael S. Tsirkin
    int r = read(e->fd, &value, sizeof(value));
46 2292b339 Michael S. Tsirkin
    return r == sizeof(value);
47 2292b339 Michael S. Tsirkin
}
48 2292b339 Michael S. Tsirkin
49 2292b339 Michael S. Tsirkin
int event_notifier_test(EventNotifier *e)
50 2292b339 Michael S. Tsirkin
{
51 2292b339 Michael S. Tsirkin
    uint64_t value;
52 2292b339 Michael S. Tsirkin
    int r = read(e->fd, &value, sizeof(value));
53 2292b339 Michael S. Tsirkin
    if (r == sizeof(value)) {
54 2292b339 Michael S. Tsirkin
        /* restore previous value. */
55 2292b339 Michael S. Tsirkin
        int s = write(e->fd, &value, sizeof(value));
56 2292b339 Michael S. Tsirkin
        /* never blocks because we use EFD_SEMAPHORE.
57 2292b339 Michael S. Tsirkin
         * If we didn't we'd get EAGAIN on overflow
58 2292b339 Michael S. Tsirkin
         * and we'd have to write code to ignore it. */
59 2292b339 Michael S. Tsirkin
        assert(s == sizeof(value));
60 2292b339 Michael S. Tsirkin
    }
61 2292b339 Michael S. Tsirkin
    return r == sizeof(value);
62 2292b339 Michael S. Tsirkin
}