Loading...
Searching...
No Matches
scheduler.h
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_SCHEDULER_INCLUDED
#define ETL_SCHEDULER_INCLUDED
#include "platform.h"
#include "error_handler.h"
#include "exception.h"
#include "function.h"
#include "nullptr.h"
#include "task.h"
#include "type_traits.h"
#include "vector.h"
#include <stdint.h>
namespace etl
{
//***************************************************************************
//***************************************************************************
class scheduler_exception : public etl::exception
{
public:
scheduler_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: etl::exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class scheduler_no_tasks_exception : public etl::scheduler_exception
{
public:
scheduler_no_tasks_exception(string_type file_name_, numeric_type line_number_)
: etl::scheduler_exception(ETL_ERROR_TEXT("scheduler:no tasks", ETL_SCHEDULER_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class scheduler_null_task_exception : public etl::scheduler_exception
{
public:
scheduler_null_task_exception(string_type file_name_, numeric_type line_number_)
: etl::scheduler_exception(ETL_ERROR_TEXT("scheduler:null task", ETL_SCHEDULER_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class scheduler_too_many_tasks_exception : public etl::scheduler_exception
{
public:
scheduler_too_many_tasks_exception(string_type file_name_, numeric_type line_number_)
: etl::scheduler_exception(ETL_ERROR_TEXT("scheduler:too many tasks", ETL_SCHEDULER_FILE_ID"C"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
struct scheduler_policy_sequential_single
{
bool schedule_tasks(etl::ivector<etl::task*>& task_list)
{
bool idle = true;
for (size_t index = 0UL; index < task_list.size(); ++index)
{
etl::task& task = *(task_list[index]);
if (task.task_request_work() > 0)
{
task.task_process_work();
idle = false;
}
}
return idle;
}
};
ETL_DEPRECATED_REASON("Misspelt class name") typedef scheduler_policy_sequential_single scheduler_policy_sequencial_single;
//***************************************************************************
//***************************************************************************
struct scheduler_policy_sequential_multiple
{
bool schedule_tasks(etl::ivector<etl::task*>& task_list)
{
bool idle = true;
for (size_t index = 0UL; index < task_list.size(); ++index)
{
etl::task& task = *(task_list[index]);
while (task.task_request_work() > 0)
{
task.task_process_work();
idle = false;
}
}
return idle;
}
};
ETL_DEPRECATED
typedef scheduler_policy_sequential_multiple scheduler_policy_sequencial_multiple;
//***************************************************************************
//***************************************************************************
struct scheduler_policy_highest_priority
{
bool schedule_tasks(etl::ivector<etl::task*>& task_list)
{
bool idle = true;
size_t index = 0UL;
while (index < task_list.size())
{
etl::task& task = *(task_list[index]);
if (task.task_request_work() > 0)
{
task.task_process_work();
idle = false;
break;
}
else
{
++index;
}
}
return idle;
}
};
//***************************************************************************
//***************************************************************************
struct scheduler_policy_most_work
{
bool schedule_tasks(etl::ivector<etl::task*>& task_list)
{
bool idle = true;
size_t most_index = 0UL;
uint32_t most_work = 0UL;
for (size_t index = 0UL; index < task_list.size(); ++index)
{
etl::task& task = *(task_list[index]);
uint32_t n_work = task.task_request_work();
if (n_work > most_work)
{
most_index = index;
most_work = n_work;
idle = false;
}
}
if (!idle)
{
task_list[most_index]->task_process_work();
}
return idle;
}
};
//***************************************************************************
//***************************************************************************
class ischeduler
{
public:
//*******************************************
// Virtuals.
//*******************************************
virtual void start() = 0;
virtual ~ischeduler() {}
//*******************************************
//*******************************************
void set_idle_callback(etl::ifunction<void>& callback)
{
p_idle_callback = &callback;
}
//*******************************************
//*******************************************
void set_watchdog_callback(etl::ifunction<void>& callback)
{
p_watchdog_callback = &callback;
}
//*******************************************
//*******************************************
void set_scheduler_running(bool scheduler_running_)
{
scheduler_running = scheduler_running_;
}
//*******************************************
//*******************************************
bool scheduler_is_running() const
{
return scheduler_running;
}
//*******************************************
//*******************************************
void exit_scheduler()
{
scheduler_exit = true;
}
//*******************************************
//*******************************************
void add_task(etl::task& task)
{
ETL_ASSERT(!task_list.full(), ETL_ERROR(etl::scheduler_too_many_tasks_exception));
if (!task_list.full())
{
task_list_t::iterator itask = etl::upper_bound(task_list.begin(), task_list.end(), task.get_task_priority(), compare_priority());
task_list.insert(itask, &task);
task.on_task_added();
}
}
//*******************************************
//*******************************************
template <typename TSize>
void add_task_list(etl::task** p_tasks, TSize size)
{
for (TSize i = 0; i < size; ++i)
{
ETL_ASSERT((p_tasks[i] != ETL_NULLPTR), ETL_ERROR(etl::scheduler_null_task_exception));
add_task(*(p_tasks[i]));
}
}
protected:
//*******************************************
//*******************************************
ischeduler(etl::ivector<etl::task*>& task_list_)
: scheduler_running(false)
, scheduler_exit(false)
, p_idle_callback(ETL_NULLPTR)
, p_watchdog_callback(ETL_NULLPTR)
, task_list(task_list_)
{
}
bool scheduler_running;
bool scheduler_exit;
etl::ifunction<void>* p_idle_callback;
etl::ifunction<void>* p_watchdog_callback;
private:
//*******************************************
// Used to order tasks in descending priority.
//*******************************************
struct compare_priority
{
bool operator()(etl::task_priority_t priority, etl::task* ptask) const
{
return priority > ptask->get_task_priority();
}
};
typedef etl::ivector<etl::task*> task_list_t;
task_list_t& task_list;
};
//***************************************************************************
//***************************************************************************
template <typename TSchedulerPolicy, size_t MAX_TASKS_>
class scheduler
: public etl::ischeduler
, protected TSchedulerPolicy
{
public:
enum
{
MAX_TASKS = MAX_TASKS_,
};
scheduler()
: ischeduler(task_list)
{
}
//*******************************************
//*******************************************
void start()
{
ETL_ASSERT(task_list.size() > 0, ETL_ERROR(etl::scheduler_no_tasks_exception));
scheduler_running = true;
while (!scheduler_exit)
{
if (scheduler_running)
{
bool idle = TSchedulerPolicy::schedule_tasks(task_list);
if (p_watchdog_callback)
{
(*p_watchdog_callback)();
}
if (idle && p_idle_callback)
{
(*p_idle_callback)();
}
}
}
}
private:
typedef etl::vector<etl::task*, MAX_TASKS> task_list_t;
task_list_t task_list;
};
} // namespace etl
#endif
etl::callback
Definition callback.h:46
etl::ischeduler
Scheduler base.
Definition scheduler.h:230
etl::ischeduler::add_task_list
void add_task_list(etl::task **p_tasks, TSize size)
Definition scheduler.h:304
etl::ischeduler::ischeduler
ischeduler(etl::ivector< etl::task * > &task_list_)
Constructor.
Definition scheduler.h:318
etl::ischeduler::set_watchdog_callback
void set_watchdog_callback(etl::ifunction< void > &callback)
Set the watchdog callback.
Definition scheduler.h:251
etl::ischeduler::exit_scheduler
void exit_scheduler()
Force the scheduler to exit.
Definition scheduler.h:275
etl::ischeduler::set_scheduler_running
void set_scheduler_running(bool scheduler_running_)
Set the running state for the scheduler.
Definition scheduler.h:259
etl::ischeduler::add_task
void add_task(etl::task &task)
Definition scheduler.h:284
etl::ischeduler::set_idle_callback
void set_idle_callback(etl::ifunction< void > &callback)
Set the idle callback.
Definition scheduler.h:243
etl::ischeduler::scheduler_is_running
bool scheduler_is_running() const
Get the running state for the scheduler.
Definition scheduler.h:267
etl::ivector
Definition vector.h:65
etl::scheduler_exception
Base exception class for scheduler.
Definition scheduler.h:49
etl::scheduler_no_tasks_exception
'No tasks' exception.
Definition scheduler.h:62
etl::scheduler_null_task_exception
'Null tasks' exception.
Definition scheduler.h:75
etl::scheduler_too_many_tasks_exception
'Too many tasks' exception.
Definition scheduler.h:88
etl::scheduler::start
void start()
Start the scheduler.
Definition scheduler.h:372
etl::task
Task.
Definition task.h:59
etl::task::on_task_added
virtual void on_task_added()
Called when the task has been added to the scheduler.
Definition task.h:90
etl::task::task_request_work
virtual uint32_t task_request_work() const =0
etl::task::get_task_priority
etl::task_priority_t get_task_priority() const
Definition task.h:115
etl::task::task_process_work
virtual void task_process_work()=0
Called to get the task to do work.
error_handler.h
exception.h
function.h
ETL_ASSERT
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
etl::exception
Definition exception.h:59
etl::ifunction
Definition function.h:54
etl::vector
Definition vector.h:1830
etl
Definition absolute.h:40
etl::scheduler_policy_sequencial_multiple
ETL_DEPRECATED typedef scheduler_policy_sequential_multiple scheduler_policy_sequencial_multiple
Typedef for backwards compatibility with miss-spelt struct name.
Definition scheduler.h:154
etl::size
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
etl::ETL_DEPRECATED_REASON
ETL_DEPRECATED_REASON("Misspelt class name") typedef scheduler_policy_sequential_single scheduler_policy_sequencial_single
Typedef for backwards compatibility with miss-spelt struct name.
nullptr.h
platform.h
etl::scheduler_policy_highest_priority
Definition scheduler.h:162
etl::scheduler_policy_most_work
Definition scheduler.h:195
etl::scheduler_policy_sequential_multiple
Definition scheduler.h:132
etl::scheduler_policy_sequential_single
Definition scheduler.h:103
type_traits.h
vector.h