PollerThread.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. #include <inttypes.h>
  19. #include <vector>
  20. #include <AP_HAL/Device.h>
  21. #include "Poller.h"
  22. #include "Thread.h"
  23. namespace Linux {
  24. class TimerPollable : public Pollable {
  25. friend class PollerThread;
  26. public:
  27. class WrapperCb {
  28. public:
  29. virtual ~WrapperCb() { }
  30. virtual void start_cb() { }
  31. virtual void end_cb() { }
  32. };
  33. using PeriodicCb = AP_HAL::Device::PeriodicCb;
  34. virtual ~TimerPollable() { }
  35. void on_can_read() override;
  36. bool setup_timer(uint32_t timeout_usec);
  37. bool adjust_timer(uint32_t timeout_usec);
  38. protected:
  39. TimerPollable(PeriodicCb cb, WrapperCb *wrapper)
  40. : _cb(cb)
  41. , _wrapper(wrapper)
  42. {
  43. }
  44. PeriodicCb _cb;
  45. WrapperCb *_wrapper;
  46. bool _removeme = false;
  47. };
  48. class PollerThread : public Thread {
  49. public:
  50. PollerThread() : Thread{FUNCTOR_BIND_MEMBER(&PollerThread::mainloop, void)} { }
  51. virtual ~PollerThread() { }
  52. TimerPollable *add_timer(TimerPollable::PeriodicCb cb,
  53. TimerPollable::WrapperCb *wrapper,
  54. uint32_t timeout_usec);
  55. bool adjust_timer(TimerPollable *p, uint32_t timeout_usec);
  56. void mainloop();
  57. bool stop() override;
  58. protected:
  59. void _cleanup_timers();
  60. Poller _poller{};
  61. std::vector<TimerPollable*> _timers{};
  62. };
  63. }