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