OXYGENHUB
◆ main openmsx / src / utils / Poller.cc
1.0 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
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