openMSX
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
10namespace openmsx {
11
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
23{
24#ifndef _WIN32
25 close(wakeupPipe[0]);
26 close(wakeupPipe[1]);
27#endif
28}
29
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
42bool 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
bool poll(int fd)
Waits for an event to occur on the given file descriptor.
Definition Poller.cc:42
void abort()
Aborts a poll in progress and any future poll attempts.
Definition Poller.cc:30
This file implemented 3 utility functions:
Definition Autofire.cc:11