KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
scheduler.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : scheduler.h
11  * Created : 10 apr. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #ifndef SCHEDULER_H_
17 #define SCHEDULER_H_
18 
19 // #define SCHD_TRACE_LOAD
20 
21 /**
22  * @file
23  *
24  * @ingroup kernel
25  *
26  * Simple task scheduler for tasks. Tasks are functions with no argument and no return value which
27  * will be invoked by the scheduler. Its a form of cooperative multitasking, so if a function does
28  * not return it will stall the entire system.
29  *
30  * At the end the scheduler will be run by calling `schdRunForever()`. This is than the main-loop.
31  *
32  * More information can be found in the tutorial
33  * [Adding timed processes to the scheduler](@ref scheduler).
34  */
35 
36 #include <stdbool.h>
37 #include <util/macro.h>
38 
39 #define TASK_ID_NONE -1
40 
41 #define E_SCHD_INVTASKID ( E_SCHD + 0x01 ) ///< Invalid task ID.
42 #define E_SCHD_INVTASKID_DESCR "Invalid task ID"
43 
44 /**
45  * Task function typedef. Just a simple function with no arguments and no return value.
46  */
47 typedef void (*SchdTaskF)();
48 
49 /**
50  * Register a task with the scheduler.
51  *
52  * @param task The function to invoke (e.g. `void myFunc()`)
53  * @param priority Whether or not this is a high-priority function.
54  * @param taskId Will be filled in with the task identifier.
55  *
56  * @retval true All OK
57  * @retval false No can do, check errGet(). Most likely there is no more space left.
58  */
59 #ifndef SCHD_TRACE_LOAD
60 bool schdRegister(SchdTaskF task, bool priority, int * taskId);
61 #else
62 bool _schdRegister(SchdTaskF task, bool priority, int * taskId, const char * fName);
63 #define schdRegister(TASK, PRIORITY, TASKID) \
64  _schdRegister(TASK, PRIORITY, TASKID, #TASK)
65 #endif
66 
67 
68 /**
69  * Schedule a task to run periodically.
70  *
71  * @param taskId The task ID to run.
72  * @param interval The interval in milliseconds.
73  *
74  */
75 void schdRunPeriodic(int taskId, int interval);
76 
77 /**
78  * Stop a scheduled task.
79  *
80  * @param taskId The task ID to run.
81  */
82 void schdStop(int taskId);
83 
84 
85 /**
86  * Schedule a task to run after a delay.
87  *
88  * @param taskId The task ID to run.
89  * @param interval The delay in milliseconds.
90  *
91  */
92 void schdRunDelay(int taskId, int interval);
93 
94 /**
95  * Run a task 'now'. Well, not really now, but as soon as possible.
96  *
97  * @note Do not call this function form within an IRQ.
98  *
99  * @param taskId The task to run
100  *
101  */
102 void schdRun(int taskId);
103 
104 /**
105  * Schedule a task from an IRQ.
106  *
107  * @param taskId
108  */
109 void schdRunIRQ(int taskId);
110 
111 /**
112  * Enable of disable a task. If a task is disabled, it won't run. Not periodically, not when
113  * schdRun is invoked.
114  *
115  * @param taskId The task to run
116  * @param bool true - Enable the task, false - disable the task.
117  *
118  */
119 void schdSetEnable(int taskId, bool enabled);
120 
121 /**
122  * Checks if there are any tasks pending. Could be used if you wish to run for as long as there are
123  * no other tasks. Though this is not advised.
124  *
125  * @retval true Tasks are pending
126  * @retval false Tasks are not pending
127  */
128 bool schdTasksPending();
129 
130 /**
131  * Adds an idle task, the task which is executed when there is nothing else to do.
132  *
133  * Note that idle tasks can not be removed.
134  *
135  * @param idleTask The idle task.
136  * @retval true Idle task configured
137  * @retval false Idle task not configured
138  */
139 bool schdAddIdleTask(SchdTaskF idleTask);
140 
141 /**
142  * Invoked in the main, starts the scheduler.
143  */
144 void schdRunForever();
145 
146 #ifdef SCHD_TRACE_LOAD
147 void schdShowTraces();
148 void schdResetTraces();
149 #endif
150 
151 #endif /* SCHEDULER_H_ */
152 
153 
void schdRunPeriodic(int taskId, int interval)
Schedule a task to run periodically.
Definition: scheduler.c:212
void schdRunForever()
Invoked in the main, starts the scheduler.
Definition: scheduler.c:268
bool schdRegister(SchdTaskF task, bool priority, int *taskId)
Register a task with the scheduler.
Definition: scheduler.c:95
bool schdTasksPending()
Checks if there are any tasks pending.
Definition: scheduler.c:260
void(* SchdTaskF)()
Task function typedef.
Definition: scheduler.h:47
void schdStop(int taskId)
Stop a scheduled task.
Definition: scheduler.c:223
void schdRun(int taskId)
Run a task &#39;now&#39;.
Definition: scheduler.c:233
void schdRunDelay(int taskId, int interval)
Schedule a task to run after a delay.
Definition: scheduler.c:201
void schdRunIRQ(int taskId)
Schedule a task from an IRQ.
Definition: scheduler.c:242
bool schdAddIdleTask(SchdTaskF idleTask)
Adds an idle task, the task which is executed when there is nothing else to do.
Definition: scheduler.c:135
void schdSetEnable(int taskId, bool enabled)
Enable of disable a task.
Definition: scheduler.c:248
Provides common macros.