123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #pragma once
- #include <unistd.h>
- #include "AP_HAL/utility/RingBuffer.h"
- #include "Semaphores.h"
- namespace Linux {
- class Poller;
- class Pollable {
- friend class Poller;
- public:
- Pollable(int fd) : _fd(fd) { }
- Pollable() { }
- virtual ~Pollable();
- int get_fd() const { return _fd; }
-
- virtual void on_can_read() { }
-
- virtual void on_can_write() { }
-
- virtual void on_error() { }
-
- virtual void on_hang_up() { }
- protected:
- int _fd = -1;
- };
- class WakeupPollable : public Pollable {
- friend class Poller;
- public:
- void on_can_read() override;
- };
- class Poller {
- public:
- Poller();
- ~Poller() {
- unregister_pollable(&_wakeup);
- if (_epfd >= 0) {
- close(_epfd);
- }
- }
-
- bool operator!() const { return _epfd == -1; }
-
- explicit operator bool() const { return _epfd != -1; }
-
- bool register_pollable(Pollable *p, uint32_t events);
-
- void unregister_pollable(const Pollable *p);
-
- int poll() const;
-
- void wakeup() const;
- private:
- int _epfd = -1;
- WakeupPollable _wakeup{};
- };
- }
|