Statistics
| Branch: | Revision:

root / hw / event_notifier.c @ df0db221

History | View | Annotate | Download (1.3 kB)

1
/*
2
 * event notifier support
3
 *
4
 * Copyright Red Hat, Inc. 2010
5
 *
6
 * Authors:
7
 *  Michael S. Tsirkin <mst@redhat.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 */
12

    
13
#include "hw.h"
14
#include "event_notifier.h"
15
#ifdef CONFIG_EVENTFD
16
#include <sys/eventfd.h>
17
#endif
18

    
19
int event_notifier_init(EventNotifier *e, int active)
20
{
21
#ifdef CONFIG_EVENTFD
22
    int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
23
    if (fd < 0)
24
        return -errno;
25
    e->fd = fd;
26
    return 0;
27
#else
28
    return -ENOSYS;
29
#endif
30
}
31

    
32
void event_notifier_cleanup(EventNotifier *e)
33
{
34
    close(e->fd);
35
}
36

    
37
int event_notifier_get_fd(EventNotifier *e)
38
{
39
    return e->fd;
40
}
41

    
42
int event_notifier_test_and_clear(EventNotifier *e)
43
{
44
    uint64_t value;
45
    int r = read(e->fd, &value, sizeof(value));
46
    return r == sizeof(value);
47
}
48

    
49
int event_notifier_test(EventNotifier *e)
50
{
51
    uint64_t value;
52
    int r = read(e->fd, &value, sizeof(value));
53
    if (r == sizeof(value)) {
54
        /* restore previous value. */
55
        int s = write(e->fd, &value, sizeof(value));
56
        /* never blocks because we use EFD_SEMAPHORE.
57
         * If we didn't we'd get EAGAIN on overflow
58
         * and we'd have to write code to ignore it. */
59
        assert(s == sizeof(value));
60
    }
61
    return r == sizeof(value);
62
}