MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/runtime/scheduler.hpp
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file scheduler.hpp
00034  *
00035  * \author MAV'RIC Team
00036  * \author Felix Schill
00037  * \author Julien Lecoeur
00038  *
00039  * \brief Scheduler
00040  *
00041  ******************************************************************************/
00042 
00043 
00044 #ifndef SCHEDULER_HPP_
00045 #define SCHEDULER_HPP_
00046 
00047 #include <cstdint>
00048 #include "runtime/scheduler_task.hpp"
00049 
00050 #include "util/print_util.hpp"
00051 
00058 class Scheduler
00059 {
00060 
00061 public:
00063     static const uint32_t TIMEBASE = 1000000;
00064 
00068     enum strategy_t
00069     {
00070         ROUND_ROBIN,                
00071         FIXED_PRIORITY              
00072     };
00073 
00074 
00078     struct conf_t
00079     {
00080         Scheduler::strategy_t schedule_strategy;    
00081         bool debug;                                 
00082     };
00083 
00084 
00090     Scheduler(const Scheduler::conf_t config = default_config());
00091 
00092 
00106     template<typename T>
00107     bool add_task(uint32_t                                      repeat_period,
00108                   typename Scheduler_task::function<T>::type_t  task_function,
00109                   T*                                            task_argument,
00110                   Scheduler_task::priority_t                    priority       = Scheduler_task::PRIORITY_NORMAL,
00111                   Scheduler_task::timing_mode_t                 timing_mode    = Scheduler_task::PERIODIC_ABSOLUTE,
00112                   Scheduler_task::run_mode_t                    run_mode       = Scheduler_task::RUN_REGULAR,
00113                   int32_t                                       task_id        = -1);
00114 
00115 
00121     bool sort_tasks(void);
00122 
00123 
00129     int32_t update(void);
00130 
00131 
00139     const Scheduler_task* get_task_by_id(uint16_t task_id) const;
00140     Scheduler_task* get_task_by_id(uint16_t task_id);
00141 
00142 
00150     const Scheduler_task* get_task_by_index(uint16_t task_index) const;
00151     Scheduler_task* get_task_by_index(uint16_t task_index);
00152 
00153 
00159     void suspend_all_tasks(uint32_t delay);
00160 
00161 
00166     void run_all_tasks_now(void);
00167 
00168 
00172     uint32_t task_count(void) const;
00173 
00174 
00180     static Scheduler::conf_t default_config(void);
00181 
00182 protected:
00189     virtual uint32_t max_task_count(void) = 0;
00190 
00191 
00199     virtual Scheduler_task* tasks(void) = 0;
00200     virtual const Scheduler_task* tasks(void) const = 0;
00201 
00202 private:
00203     strategy_t schedule_strategy_;               
00204     bool debug_;                                 
00205     uint32_t task_count_;                        
00206     uint32_t current_schedule_slot_;             
00207 };
00208 
00209 
00215 template<uint32_t N = 10>
00216 class Scheduler_T: public Scheduler
00217 {
00218 public:
00222     Scheduler_T(const Scheduler::conf_t config = default_config()):
00223         Scheduler(config)
00224     {;}
00225 
00226 protected:
00227 
00234     uint32_t max_task_count(void)
00235     {
00236         return N;
00237     };
00238 
00244     Scheduler_task* tasks(void)
00245     {
00246         return tasks_;
00247     }
00248 
00249     const Scheduler_task* tasks(void) const
00250     {
00251         return tasks_;
00252     }
00253 
00254 private:
00255     Scheduler_task  tasks_[N];           
00256 };
00257 
00258 #include "scheduler.hxx"
00259 
00260 #endif /* SCHEDULER_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines