forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan-pollable.c
93 lines (78 loc) · 1.41 KB
/
airscan-pollable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Pollable events
*/
#include "airscan.h"
#include <sys/eventfd.h>
#include <poll.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
/* The pollable event
*/
struct pollable {
int efd; /* Underlying eventfd handle */
};
/* Create new pollable event
*/
pollable*
pollable_new (void)
{
int efd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
if (efd< 0) {
return NULL;
}
pollable *p = g_new0(pollable, 1);
p->efd = efd;
return p;
}
/* Free pollable event
*/
void
pollable_free (pollable *p)
{
close(p->efd);
g_free(p);
}
/* Get file descriptor for poll()/select().
*/
int
pollable_get_fd (pollable *p)
{
return p->efd;
}
/* Make pollable event "ready"
*/
void
pollable_signal (pollable *p)
{
static uint64_t c = 1;
write(p->efd, &c, sizeof(c));
}
/* Make pollable event "not ready"
*/
void
pollable_reset (pollable *p)
{
uint64_t unused;
read(p->efd, &unused, sizeof(unused));
}
/* Wait until pollable event is ready
*/
void
pollable_wait (pollable *p)
{
int rc;
do {
struct pollfd pfd = {
.fd = p->efd,
.events = POLLIN,
.revents = 0
};
rc = poll(&pfd, 1, -1);
} while (rc < 1);
}
/* vim:ts=8:sw=4:et
*/