Statistics
| Branch: | Revision:

root / event_notifier.c @ 19bf7c87

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