123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * 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/>.
- */
- #include "PollerThread.h"
- #include <algorithm>
- #include <poll.h>
- #include <sys/epoll.h>
- #include <sys/timerfd.h>
- #include <AP_Math/AP_Math.h>
- namespace Linux {
- void TimerPollable::on_can_read()
- {
- if (_removeme) {
- return;
- }
- uint64_t nevents = 0;
- int r = read(_fd, &nevents, sizeof(nevents));
- if (r < 0) {
- return;
- }
- if (_wrapper) {
- _wrapper->start_cb();
- }
- _cb();
- if (_wrapper) {
- _wrapper->end_cb();
- }
- }
- bool TimerPollable::setup_timer(uint32_t timeout_usec)
- {
- if (_fd >= 0) {
- return false;
- }
- _fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC|TFD_NONBLOCK);
- if (_fd < 0) {
- return false;
- }
- if (!adjust_timer(timeout_usec)) {
- ::close(_fd);
- _fd = -1;
- return false;
- }
- return true;
- }
- bool TimerPollable::adjust_timer(uint32_t timeout_usec)
- {
- if (_fd < 0) {
- return false;
- }
- struct itimerspec spec = { };
- spec.it_interval.tv_nsec = timeout_usec * AP_NSEC_PER_USEC;
- spec.it_value.tv_nsec = timeout_usec * AP_NSEC_PER_USEC;
- if (timerfd_settime(_fd, 0, &spec, nullptr) < 0) {
- return false;
- }
- return true;
- }
- TimerPollable *PollerThread::add_timer(TimerPollable::PeriodicCb cb,
- TimerPollable::WrapperCb *wrapper,
- uint32_t timeout_usec)
- {
- if (!_poller) {
- return nullptr;
- }
- TimerPollable *p = new TimerPollable(cb, wrapper);
- if (!p || !p->setup_timer(timeout_usec) ||
- !_poller.register_pollable(p, POLLIN)) {
- delete p;
- return nullptr;
- }
- _timers.push_back(p);
- return p;
- }
- bool PollerThread::adjust_timer(TimerPollable *p, uint32_t timeout_usec)
- {
- /* Make sure the handle points to a valid timer */
- auto it = std::find(_timers.begin(), _timers.end(), p);
- if (it == _timers.end()) {
- return false;
- }
- return (*it)->adjust_timer(timeout_usec);
- }
- void PollerThread::_cleanup_timers()
- {
- if (!_poller) {
- return;
- }
- for (auto it = _timers.begin(); it != _timers.end(); it++) {
- TimerPollable *p = *it;
- if (p->_removeme) {
- _timers.erase(it);
- _poller.unregister_pollable(p);
- delete p;
- }
- }
- }
- void PollerThread::mainloop()
- {
- if (!_poller) {
- return;
- }
- while (!_should_exit) {
- _poller.poll();
- _cleanup_timers();
- }
- _started = false;
- _should_exit = false;
- }
- bool PollerThread::stop()
- {
- if (!is_started()) {
- return false;
- }
- _should_exit = true;
- _poller.wakeup();
- return true;
- }
- }
|