OXYGENHUB
◆ main openmsx / src / utils / Poller.hh
1.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef POLLER_HH
2
#define POLLER_HH
3
4
#include <array>
5
#include <atomic>
6
7
namespace openmsx {
8
9
/** Polls for events on a given file descriptor.
10
  * It is possible to abort this poll from another thread.
11
  * This class exists because in POSIX there is no straightforward way to
12
  * abort a blocking I/O operation.
13
  */
14
class Poller
15
{
16
public:
17
	Poller();
18
	Poller(const Poller&) = delete;
19
	Poller(Poller&&) = delete;
20
	Poller& operator=(const Poller&) = delete;
21
	Poller& operator=(Poller&&) = delete;
22
	~Poller();
23
24
#ifndef _WIN32
25
	/** Waits for an event to occur on the given file descriptor.
26
	  * Returns true iff abort() was called or an error occurred.
27
	  */
28
	[[nodiscard]] bool poll(int fd);
29
#endif
30
31
	/** Returns true iff abort() was called.
32
	  */
33
	[[nodiscard]] bool aborted() const {
34
		return abortFlag;
35
	}
36
37
	/** Aborts a poll in progress and any future poll attempts.
38
	  */
39
	void abort();
40
41
	/** Reset aborted() to false. (Functionally the same, but more efficient
42
	  * than destroying and recreating this object). */
43
	void reset() {
44
		abortFlag = false;
45
	}
46
47
private:
48
#ifndef _WIN32
49
	std::array<int, 2> wakeupPipe;
50
#endif
51
	std::atomic_bool abortFlag = false;
52
};
53
54
} // namespace openmsx
55
56
#endif