2016-05-02 17:34:40 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "AP_HAL/utility/RingBuffer.h"
|
|
|
|
#include "Semaphores.h"
|
|
|
|
|
|
|
|
namespace Linux {
|
|
|
|
|
2016-07-16 13:29:27 -03:00
|
|
|
class Poller;
|
|
|
|
|
2016-05-02 17:34:40 -03:00
|
|
|
class Pollable {
|
2016-07-16 13:29:27 -03:00
|
|
|
friend class Poller;
|
2016-05-02 17:34:40 -03:00
|
|
|
public:
|
|
|
|
Pollable(int fd) : _fd(fd) { }
|
2016-07-16 13:29:27 -03:00
|
|
|
Pollable() : _fd(-1) { }
|
|
|
|
|
2016-05-02 17:34:40 -03:00
|
|
|
virtual ~Pollable();
|
|
|
|
|
|
|
|
int get_fd() const { return _fd; }
|
|
|
|
|
2016-07-16 13:29:27 -03:00
|
|
|
virtual void on_can_read() { }
|
|
|
|
virtual void on_can_write() { }
|
|
|
|
virtual void on_error() { }
|
|
|
|
virtual void on_hang_up() { }
|
2016-05-02 17:34:40 -03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int _fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Poller {
|
|
|
|
public:
|
|
|
|
Poller() : _epfd(epoll_create1(EPOLL_CLOEXEC)) { }
|
|
|
|
~Poller() { close(_epfd); }
|
|
|
|
|
2016-07-16 13:29:27 -03:00
|
|
|
bool register_pollable(Pollable*, uint32_t events);
|
2016-05-02 17:34:40 -03:00
|
|
|
void unregister_pollable(const Pollable*);
|
|
|
|
|
2016-07-16 13:29:27 -03:00
|
|
|
int poll() const;
|
2016-05-02 17:34:40 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
int _epfd;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|