Line data Source code
1 : #pragma once 2 : 3 : #include <AP_HAL/AP_HAL.h> 4 : #if CONFIG_HAL_BOARD == HAL_BOARD_SITL 5 : #include "AP_HAL_SITL_Namespace.h" 6 : #include <sys/time.h> 7 : #include <pthread.h> 8 : 9 : #define SITL_SCHEDULER_MAX_TIMER_PROCS 8 10 : 11 : /* Scheduler implementation: */ 12 : class HALSITL::Scheduler : public AP_HAL::Scheduler { 13 : public: 14 : explicit Scheduler(SITL_State *sitlState); 15 20698094246 : static Scheduler *from(AP_HAL::Scheduler *scheduler) { 16 20698094246 : return static_cast<HALSITL::Scheduler*>(scheduler); 17 : } 18 : 19 : /* AP_HAL::Scheduler methods */ 20 : 21 : void init() override; 22 : void delay(uint16_t ms) override; 23 : void delay_microseconds(uint16_t us) override; 24 : 25 : void register_timer_process(AP_HAL::MemberProc) override; 26 : void register_io_process(AP_HAL::MemberProc) override; 27 : 28 : void register_timer_failsafe(AP_HAL::Proc, uint32_t period_us) override; 29 : 30 : bool in_main_thread() const override; 31 246017465 : bool is_system_initialized() override { return _initialized; }; 32 : void set_system_initialized() override; 33 : 34 : void reboot(bool hold_in_bootloader) override; 35 : 36 187275482 : bool interrupts_are_blocked(void) const { 37 187275482 : return _nested_atomic_ctr != 0; 38 : } 39 : 40 187268325 : void sitl_begin_atomic() { 41 187268325 : _nested_atomic_ctr++; 42 187268325 : } 43 : void sitl_end_atomic(); 44 : 45 187268325 : static void timer_event() { 46 187268325 : _run_timer_procs(); 47 187268325 : _run_io_procs(); 48 187268324 : } 49 : 50 21994710651 : uint64_t stopped_clock_usec() const { return _stopped_clock_usec; } 51 : 52 : static void _run_io_procs(); 53 : static bool _should_exit; 54 : 55 : /* 56 : create a new thread 57 : */ 58 : bool thread_create(AP_HAL::MemberProc, const char *name, 59 : uint32_t stack_size, priority_base base, int8_t priority) override; 60 : 61 16 : void set_in_semaphore_take_wait(bool value) { _in_semaphore_take_wait = value; } 62 : /* 63 : * semaphore_wait_hack_required - possibly move time input step 64 : * forward even if we are currently pretending to be the IO or timer 65 : * threads. 66 : */ 67 : // a couple of helper functions to cope with SITL's time stepping 68 : bool semaphore_wait_hack_required() const; 69 : 70 : // get the name of the current thread, or nullptr if not known 71 : const char *get_current_thread_name(void) const; 72 : 73 : private: 74 : SITL_State *_sitlState; 75 : uint8_t _nested_atomic_ctr; 76 : static AP_HAL::Proc _failsafe; 77 : 78 : static void _run_timer_procs(); 79 : 80 : static volatile bool _timer_event_missed; 81 : static AP_HAL::MemberProc _timer_proc[SITL_SCHEDULER_MAX_TIMER_PROCS]; 82 : static AP_HAL::MemberProc _io_proc[SITL_SCHEDULER_MAX_TIMER_PROCS]; 83 : static uint8_t _num_timer_procs; 84 : static uint8_t _num_io_procs; 85 : static bool _in_timer_proc; 86 : static bool _in_io_proc; 87 : 88 : // boolean set by the Semaphore code to indicate it's currently 89 : // waiting for a take-timeout to occur. 90 : static bool _in_semaphore_take_wait; 91 : 92 : void stop_clock(uint64_t time_usec) override; 93 : 94 : static void *thread_create_trampoline(void *ctx); 95 : static void check_thread_stacks(void); 96 : 97 : bool _initialized; 98 : uint64_t _stopped_clock_usec; 99 : uint64_t _last_io_run; 100 : pthread_t _main_ctx; 101 : 102 : static HAL_Semaphore _thread_sem; 103 : struct thread_attr { 104 : struct thread_attr *next; 105 : AP_HAL::MemberProc *f; 106 : pthread_attr_t attr; 107 : uint32_t stack_size; 108 : void *stack; 109 : const uint8_t *stack_min; 110 : const char *name; 111 : pthread_t thread; 112 : }; 113 : static struct thread_attr *threads; 114 : static const uint8_t stackfill = 0xEB; 115 : }; 116 : #endif // CONFIG_HAL_BOARD