MAV'RIC
scheduler.h
1 /*******************************************************************************
2  * Copyright (c) 2009-2014, MAV'RIC Development Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  ******************************************************************************/
31 
32 /*******************************************************************************
33  * \file scheduler.h
34  *
35  * \author MAV'RIC Team
36  * \author Felix Schill
37  * \author Julien Lecoeur
38  *
39  * \brief Scheduler
40  *
41  ******************************************************************************/
42 
43 
44 #ifndef SCHEDULER_H_
45 #define SCHEDULER_H_
46 
47 
48 #ifdef __cplusplus
49 extern "C"
50 {
51 #endif
52 
53 #include "stdint.h"
54 #include <stdbool.h>
55 
56 #define SCHEDULER_TIMEBASE 1000000
57 
58 
59 typedef uint8_t task_handle_t;
60 
61 
65 typedef enum
66 {
67  TASK_RUN_ERROR = -1,
68  TASK_RUN_BLOCKED = 0,
69  TASK_RUN_SUCCESS = 1
70 } task_return_t;
71 
72 
76 typedef void* task_argument_t;
77 
78 
82 typedef task_return_t (*task_function_t)(task_argument_t);
83 
84 
88 typedef enum
89 {
90  RUN_NEVER,
91  RUN_ONCE,
92  RUN_REGULAR
93 } task_run_mode_t;
94 
95 
99 typedef enum
100 {
101  PERIODIC_ABSOLUTE,
102  PERIODIC_RELATIVE
103 } task_timing_mode_t;
104 
105 
109 typedef enum
110 {
111  PRIORITY_LOWEST = 0,
112  PRIORITY_LOW = 1,
113  PRIORITY_NORMAL = 2,
114  PRIORITY_HIGH = 3,
115  PRIORITY_HIGHEST = 4
116 } task_priority_t;
117 
118 
122 typedef enum
123 {
124  ROUND_ROBIN,
125  FIXED_PRIORITY
126 } schedule_strategy_t;
127 
128 
132 typedef struct
133 {
134  task_function_t call_function;
135  task_argument_t function_argument;
136  uint32_t task_id;
137  task_run_mode_t run_mode;
138  task_timing_mode_t timing_mode;
139  task_priority_t priority;
140  uint32_t repeat_period;
141  uint32_t next_run;
142  uint32_t execution_time;
143  uint32_t delay_max;
144  uint32_t delay_avg;
145  uint32_t delay_var_squared;
146  uint32_t rt_violations;
147 } task_entry_t;
148 
149 
156 typedef struct task_set_t
157 {
158  uint32_t task_count;
159  uint32_t max_task_count;
162 } task_set_t;
163 
164 
171 typedef struct
172 {
173  bool debug;
174  schedule_strategy_t schedule_strategy;
176 } scheduler_t;
177 
178 
182 typedef struct
183 {
184  uint32_t max_task_count;
185  schedule_strategy_t schedule_strategy;
186  bool debug;
188 
189 
198 bool scheduler_init( scheduler_t* scheduler, const scheduler_conf_t* config);
199 
200 
215 bool scheduler_add_task(scheduler_t* scheduler, uint32_t repeat_period, task_run_mode_t run_mode, task_timing_mode_t timing_mode, task_priority_t priority, task_function_t call_function, task_argument_t function_argument, uint32_t task_id);
216 
217 
223 void scheduler_sort_tasks(scheduler_t* scheduler);
224 
225 
233 int32_t scheduler_update(scheduler_t* scheduler);
234 
235 
244 task_entry_t* scheduler_get_task_by_id(const scheduler_t* scheduler, uint16_t task_id);
245 
246 
255 task_entry_t* scheduler_get_task_by_index(const scheduler_t* scheduler, uint16_t task_index);
256 
257 
264 void scheduler_change_run_mode(task_entry_t *te, task_run_mode_t new_run_mode);
265 
266 
273 void scheduler_change_task_period(task_entry_t *te, uint32_t repeat_period);
274 
275 
282 void scheduler_suspend_task(task_entry_t *te, uint32_t delay);
283 
284 
290 void scheduler_run_task_now(task_entry_t *te);
291 
292 #ifdef __cplusplus
293 }
294 #endif
295 
296 #endif /* SCHEDULER_H_ */
uint32_t delay_avg
Average delay between expected execution and actual execution.
Definition: scheduler.h:144
task_run_mode_t run_mode
Run mode.
Definition: scheduler.h:137
task_argument_t function_argument
Argument to be passed to the function.
Definition: scheduler.h:135
uint32_t max_task_count
Maximum number of tasks.
Definition: scheduler.h:184
uint32_t next_run
Next execution time.
Definition: scheduler.h:141
uint32_t task_id
Unique task identifier.
Definition: scheduler.h:136
task_priority_t priority
Priority.
Definition: scheduler.h:139
Scheduler.
Definition: scheduler.h:171
task_set_t * task_set
Pointer to task set, needs memory allocation.
Definition: scheduler.h:175
bool debug
Indicates whether the scheduler should print debug messages.
Definition: scheduler.h:186
schedule_strategy_t schedule_strategy
Scheduling strategy.
Definition: scheduler.h:174
uint32_t max_task_count
Maximum number of tasks.
Definition: scheduler.h:159
task_timing_mode_t timing_mode
Timing mode.
Definition: scheduler.h:138
uint32_t task_count
Number_of_tasks.
Definition: scheduler.h:158
Scheduler configuration.
Definition: scheduler.h:182
Task entry.
Definition: scheduler.h:132
task_function_t call_function
Function to be called.
Definition: scheduler.h:134
uint32_t rt_violations
Number of Real-time violations, this is incremented each time an execution is skipped.
Definition: scheduler.h:146
schedule_strategy_t schedule_strategy
Schedule strategy.
Definition: scheduler.h:185
uint32_t current_schedule_slot
Slot of the task being executed.
Definition: scheduler.h:160
Task set.
Definition: scheduler.h:156
uint32_t delay_var_squared
Standard deviation of the delay.
Definition: scheduler.h:145
uint32_t delay_max
Maximum delay between expected execution and actual execution.
Definition: scheduler.h:143
uint32_t execution_time
Execution time.
Definition: scheduler.h:142
bool debug
Indicates whether the scheduler should print debug messages.
Definition: scheduler.h:173
task_entry_t tasks[]
Array of tasks_entry to be executed, needs memory allocation.
Definition: scheduler.h:161
uint32_t repeat_period
Period between two calls (us)
Definition: scheduler.h:140