2016-01-10 02:23:32 -04:00
|
|
|
#pragma once
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
#include <sys/time.h>
|
2018-07-18 20:34:01 -03:00
|
|
|
#include <pthread.h>
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2018-10-19 02:07:15 -03:00
|
|
|
#define SITL_SCHEDULER_MAX_TIMER_PROCS 8
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
/* Scheduler implementation: */
|
2016-01-10 02:23:32 -04:00
|
|
|
class HALSITL::Scheduler : public AP_HAL::Scheduler {
|
2012-12-12 18:04:27 -04:00
|
|
|
public:
|
2017-01-09 08:33:52 -04:00
|
|
|
explicit Scheduler(SITL_State *sitlState);
|
2016-01-10 02:23:32 -04:00
|
|
|
static Scheduler *from(AP_HAL::Scheduler *scheduler) {
|
|
|
|
return static_cast<HALSITL::Scheduler*>(scheduler);
|
2015-09-13 15:28:02 -03:00
|
|
|
}
|
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
/* AP_HAL::Scheduler methods */
|
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
void init() override;
|
|
|
|
void delay(uint16_t ms) override;
|
|
|
|
void delay_microseconds(uint16_t us) override;
|
2013-01-03 21:32:52 -04:00
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
void register_timer_process(AP_HAL::MemberProc) override;
|
|
|
|
void register_io_process(AP_HAL::MemberProc) override;
|
2013-01-03 21:32:52 -04:00
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
void register_timer_failsafe(AP_HAL::Proc, uint32_t period_us) override;
|
2013-01-10 17:50:32 -04:00
|
|
|
|
2018-07-06 23:42:00 -03:00
|
|
|
bool in_main_thread() const override;
|
2020-12-23 07:28:14 -04:00
|
|
|
bool is_system_initialized() override { return _initialized; };
|
|
|
|
void set_system_initialized() override;
|
2013-01-10 17:50:32 -04:00
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
void reboot(bool hold_in_bootloader) override;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2021-02-01 12:26:28 -04:00
|
|
|
bool interrupts_are_blocked(void) const {
|
2015-05-04 21:59:07 -03:00
|
|
|
return _nested_atomic_ctr != 0;
|
|
|
|
}
|
2012-12-13 18:57:01 -04:00
|
|
|
|
2017-01-09 08:33:52 -04:00
|
|
|
void sitl_begin_atomic() {
|
2015-05-04 21:59:07 -03:00
|
|
|
_nested_atomic_ctr++;
|
|
|
|
}
|
2017-01-09 08:33:52 -04:00
|
|
|
void sitl_end_atomic();
|
2012-12-13 18:57:01 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
static void timer_event() {
|
2018-05-13 19:47:47 -03:00
|
|
|
_run_timer_procs();
|
|
|
|
_run_io_procs();
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2012-12-13 18:57:01 -04:00
|
|
|
|
2015-11-11 09:43:18 -04:00
|
|
|
uint64_t stopped_clock_usec() const { return _stopped_clock_usec; }
|
|
|
|
|
2018-05-13 19:47:47 -03:00
|
|
|
static void _run_io_procs();
|
2018-05-28 13:20:30 -03:00
|
|
|
static bool _should_reboot;
|
2019-09-22 21:39:42 -03:00
|
|
|
static bool _should_exit;
|
2018-07-06 20:30:27 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
create a new thread
|
|
|
|
*/
|
|
|
|
bool thread_create(AP_HAL::MemberProc, const char *name,
|
|
|
|
uint32_t stack_size, priority_base base, int8_t priority) override;
|
2019-09-02 03:00:16 -03:00
|
|
|
|
|
|
|
void set_in_semaphore_take_wait(bool value) { _in_semaphore_take_wait = value; }
|
|
|
|
/*
|
|
|
|
* semaphore_wait_hack_required - possibly move time input step
|
|
|
|
* forward even if we are currently pretending to be the IO or timer
|
|
|
|
* threads.
|
|
|
|
*/
|
|
|
|
// a couple of helper functions to cope with SITL's time stepping
|
2021-02-01 12:26:28 -04:00
|
|
|
bool semaphore_wait_hack_required() const;
|
2019-09-02 03:00:16 -03:00
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
private:
|
2014-11-15 20:04:55 -04:00
|
|
|
SITL_State *_sitlState;
|
2012-12-12 18:04:27 -04:00
|
|
|
uint8_t _nested_atomic_ctr;
|
2013-09-30 07:35:42 -03:00
|
|
|
static AP_HAL::Proc _failsafe;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2018-05-13 19:47:47 -03:00
|
|
|
static void _run_timer_procs();
|
2013-01-03 21:32:52 -04:00
|
|
|
|
|
|
|
static volatile bool _timer_event_missed;
|
2013-09-30 07:35:42 -03:00
|
|
|
static AP_HAL::MemberProc _timer_proc[SITL_SCHEDULER_MAX_TIMER_PROCS];
|
|
|
|
static AP_HAL::MemberProc _io_proc[SITL_SCHEDULER_MAX_TIMER_PROCS];
|
2012-12-12 18:04:27 -04:00
|
|
|
static uint8_t _num_timer_procs;
|
2013-04-17 08:33:50 -03:00
|
|
|
static uint8_t _num_io_procs;
|
2017-01-09 08:33:52 -04:00
|
|
|
static bool _in_timer_proc;
|
|
|
|
static bool _in_io_proc;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2019-09-02 03:00:16 -03:00
|
|
|
// boolean set by the Semaphore code to indicate it's currently
|
|
|
|
// waiting for a take-timeout to occur.
|
|
|
|
static bool _in_semaphore_take_wait;
|
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
void stop_clock(uint64_t time_usec) override;
|
2013-01-10 17:50:32 -04:00
|
|
|
|
2018-07-06 20:30:27 -03:00
|
|
|
static void *thread_create_trampoline(void *ctx);
|
2018-08-19 23:39:05 -03:00
|
|
|
static void check_thread_stacks(void);
|
2018-07-06 20:30:27 -03:00
|
|
|
|
2015-03-21 11:27:25 -03:00
|
|
|
bool _initialized;
|
2015-11-11 09:43:18 -04:00
|
|
|
uint64_t _stopped_clock_usec;
|
2017-10-20 21:57:05 -03:00
|
|
|
uint64_t _last_io_run;
|
2018-07-06 23:42:00 -03:00
|
|
|
pthread_t _main_ctx;
|
2018-08-19 23:39:05 -03:00
|
|
|
|
|
|
|
static HAL_Semaphore _thread_sem;
|
|
|
|
struct thread_attr {
|
|
|
|
struct thread_attr *next;
|
|
|
|
AP_HAL::MemberProc *f;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
uint32_t stack_size;
|
|
|
|
void *stack;
|
|
|
|
const uint8_t *stack_min;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
static struct thread_attr *threads;
|
|
|
|
static const uint8_t stackfill = 0xEB;
|
2012-12-12 18:04:27 -04:00
|
|
|
};
|
2017-01-09 08:33:52 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|