Scheduler.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include <pthread.h>
  3. #include "AP_HAL_Linux.h"
  4. #include "Semaphores.h"
  5. #include "Thread.h"
  6. #define LINUX_SCHEDULER_MAX_TIMER_PROCS 10
  7. #define LINUX_SCHEDULER_MAX_TIMESLICED_PROCS 10
  8. #define LINUX_SCHEDULER_MAX_IO_PROCS 10
  9. #define AP_LINUX_SENSORS_STACK_SIZE 256 * 1024
  10. #define AP_LINUX_SENSORS_SCHED_POLICY SCHED_FIFO
  11. #define AP_LINUX_SENSORS_SCHED_PRIO 12
  12. namespace Linux {
  13. class Scheduler : public AP_HAL::Scheduler {
  14. public:
  15. Scheduler();
  16. static Scheduler *from(AP_HAL::Scheduler *scheduler) {
  17. return static_cast<Scheduler*>(scheduler);
  18. }
  19. void init() override;
  20. void delay(uint16_t ms) override;
  21. void delay_microseconds(uint16_t us) override;
  22. void register_timer_process(AP_HAL::MemberProc) override;
  23. void register_io_process(AP_HAL::MemberProc) override;
  24. bool in_main_thread() const override;
  25. void register_timer_failsafe(AP_HAL::Proc, uint32_t period_us) override;
  26. void system_initialized() override;
  27. void reboot(bool hold_in_bootloader) override;
  28. void stop_clock(uint64_t time_usec) override;
  29. uint64_t stopped_clock_usec() const { return _stopped_clock_usec; }
  30. void microsleep(uint32_t usec);
  31. void teardown();
  32. /*
  33. create a new thread
  34. */
  35. bool thread_create(AP_HAL::MemberProc, const char *name, uint32_t stack_size, priority_base base, int8_t priority) override;
  36. private:
  37. class SchedulerThread : public PeriodicThread {
  38. public:
  39. SchedulerThread(Thread::task_t t, Scheduler &sched)
  40. : PeriodicThread(t)
  41. , _sched(sched)
  42. { }
  43. protected:
  44. bool _run() override;
  45. Scheduler &_sched;
  46. };
  47. void init_realtime();
  48. void _wait_all_threads();
  49. void _debug_stack();
  50. AP_HAL::Proc _failsafe;
  51. bool _initialized;
  52. pthread_barrier_t _initialized_barrier;
  53. AP_HAL::MemberProc _timer_proc[LINUX_SCHEDULER_MAX_TIMER_PROCS];
  54. uint8_t _num_timer_procs;
  55. volatile bool _in_timer_proc;
  56. AP_HAL::MemberProc _io_proc[LINUX_SCHEDULER_MAX_IO_PROCS];
  57. uint8_t _num_io_procs;
  58. SchedulerThread _timer_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_timer_task, void), *this};
  59. SchedulerThread _io_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_io_task, void), *this};
  60. SchedulerThread _rcin_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_rcin_task, void), *this};
  61. SchedulerThread _uart_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_uart_task, void), *this};
  62. void _timer_task();
  63. void _io_task();
  64. void _rcin_task();
  65. void _uart_task();
  66. void _run_io();
  67. void _run_uarts();
  68. uint64_t _stopped_clock_usec;
  69. uint64_t _last_stack_debug_msec;
  70. pthread_t _main_ctx;
  71. Semaphore _io_semaphore;
  72. };
  73. }