AP_Scheduler: add singleton

This commit is contained in:
Peter Barker 2018-05-04 10:44:50 +10:00 committed by Andrew Tridgell
parent 676f60333a
commit 01e0c03a12
2 changed files with 33 additions and 0 deletions

View File

@ -63,6 +63,14 @@ const AP_Param::GroupInfo AP_Scheduler::var_info[] = {
AP_Scheduler::AP_Scheduler(scheduler_fastloop_fn_t fastloop_fn) :
_fastloop_fn(fastloop_fn)
{
if (_s_instance) {
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
AP_HAL::panic("Too many schedulers");
#endif
return;
}
_s_instance = this;
AP_Param::setup_object_defaults(this, var_info);
// only allow 50 to 2000 Hz
@ -74,6 +82,15 @@ AP_Scheduler::AP_Scheduler(scheduler_fastloop_fn_t fastloop_fn) :
_last_loop_time_s = 1.0 / _loop_rate_hz;
}
/*
* Get the AP_Scheduler singleton
*/
AP_Scheduler *AP_Scheduler::_s_instance = nullptr;
AP_Scheduler *AP_Scheduler::get_instance()
{
return _s_instance;
}
// initialise the scheduler
void AP_Scheduler::init(const AP_Scheduler::Task *tasks, uint8_t num_tasks, uint32_t log_performance_bit)
{
@ -283,3 +300,12 @@ void AP_Scheduler::Log_Write_Performance()
};
DataFlash_Class::instance()->WriteCriticalBlock(&pkt, sizeof(pkt));
}
namespace AP {
AP_Scheduler &scheduler()
{
return *AP_Scheduler::get_instance();
}
};

View File

@ -62,6 +62,9 @@ public:
AP_Scheduler(const AP_Scheduler &other) = delete;
AP_Scheduler &operator=(const AP_Scheduler&) = delete;
static AP_Scheduler *get_instance();
static AP_Scheduler *_s_instance;
FUNCTOR_TYPEDEF(task_fn_t, void);
struct Task {
@ -201,3 +204,7 @@ private:
// bitmask bit which indicates if we should log PERF message to dataflash
uint32_t _log_performance_bit;
};
namespace AP {
AP_Scheduler &scheduler();
};