openMSX
utils
Poller.cc
Go to the documentation of this file.
1
#include "
Poller.hh
"
2
3
#ifndef _WIN32
4
#include <cstdio>
5
#include <poll.h>
6
#include <unistd.h>
7
#endif
8
9
10
namespace
openmsx
{
11
12
Poller::Poller
()
13
{
14
#ifndef _WIN32
15
if
(pipe(wakeupPipe.data())) {
16
wakeupPipe[0] = wakeupPipe[1] = -1;
17
perror(
"Failed to open wakeup pipe"
);
18
}
19
#endif
20
}
21
22
Poller::~Poller
()
23
{
24
#ifndef _WIN32
25
close(wakeupPipe[0]);
26
close(wakeupPipe[1]);
27
#endif
28
}
29
30
void
Poller::abort
()
31
{
32
abortFlag =
true
;
33
#ifndef _WIN32
34
char
dummy =
'X'
;
35
if
(write(wakeupPipe[1], &dummy,
sizeof
(dummy)) == -1) {
36
// Nothing we can do here; we'll have to rely on the poll() timeout.
37
}
38
#endif
39
}
40
41
#ifndef _WIN32
42
bool
Poller::poll
(
int
fd)
43
{
44
while
(
true
) {
45
std::array fds = {
46
pollfd{.fd = fd, .events = POLLIN, .revents = 0},
47
pollfd{.fd = wakeupPipe[0], .events = POLLIN, .revents = 0},
48
};
49
int
pollResult =
::poll
(fds.data(), fds.size(), 1000);
50
if
(abortFlag) {
51
return
true
;
52
}
53
if
(pollResult == -1) {
// error
54
return
true
;
55
}
56
if
(pollResult != 0) {
// no timeout
57
return
false
;
58
}
59
}
60
}
61
#endif
62
63
}
// namespace openmsx
Poller.hh
openmsx::Poller::Poller
Poller()
Definition
Poller.cc:12
openmsx::Poller::poll
bool poll(int fd)
Waits for an event to occur on the given file descriptor.
Definition
Poller.cc:42
openmsx::Poller::~Poller
~Poller()
Definition
Poller.cc:22
openmsx::Poller::abort
void abort()
Aborts a poll in progress and any future poll attempts.
Definition
Poller.cc:30
openmsx
This file implemented 3 utility functions:
Definition
Autofire.cc:11
Generated on Thu Dec 19 2024 23:13:46 for openMSX by
1.9.8