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 
10 namespace openmsx {
11 
13  : abortFlag(false)
14 {
15 #ifndef _WIN32
16  if (pipe(wakeupPipe)) {
17  wakeupPipe[0] = wakeupPipe[1] = -1;
18  perror("Failed to open wakeup pipe");
19  }
20 #endif
21 }
22 
24 {
25 #ifndef _WIN32
26  close(wakeupPipe[0]);
27  close(wakeupPipe[1]);
28 #endif
29 }
30 
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  struct pollfd fds[2] = {
47  { .fd = fd, .events = POLLIN, .revents = 0 },
48  { .fd = wakeupPipe[0], .events = POLLIN, .revents = 0 },
49  };
50  int pollResult = ::poll(fds, 2, 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
void abort()
Aborts a poll in progress and any future poll attempts.
Definition: Poller.cc:31
bool poll(int fd)
Waits for an event to occur on the given file descriptor.
Definition: Poller.cc:43
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5