forked from pichenettes/avril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.h
executable file
·130 lines (112 loc) · 3.43 KB
/
task.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2009 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Implementation of multitasking by coroutines, and naive deterministic
// scheduler.
#ifndef AVRLIB_TASK_H_
#define AVRLIB_TASK_H_
#include "avrlib/base.h"
#define TASK_BEGIN static uint16_t state = 0; \
switch(state) { \
case 0:;
// This is very unreliable because it assumes the line numbers will fit in an
// uint8_t. Don't use this unless you want to save a couple of bytes by using
// 8 bits comparisons instead of 16 bits comparisons.
#define TASK_BEGIN_NEAR static uint8_t state = 0; \
switch(state) { \
case 0:;
#define TASK_RETURN(value) \
do { \
state = __LINE__; \
return (value); \
case __LINE__:; \
} while (0)
#define TASK_SWITCH \
do { \
state = __LINE__; \
return; \
case __LINE__:; \
} while (0)
#define TASK_END } return;
namespace avrlib {
typedef struct {
void (*code)();
uint8_t priority;
} Task;
// This naive deterministic scheduler stores an array of "slots", each element
// of which stores a 0 (nop) or a task id. During initialization, the array is
// filled in such a way that $task.priority occurrences of a task are present in
// the array, and are roughly evenly spaced.
// For example if the tasks/priority are:
// Task 1: 8
// Task 2: 4
// Task 3: 3
// Task 4: 1
//
// The slots will contain:
// 1 2 1 3 1 2 1 4 1 2 1 3 2 3 0 0
//
// And the scheduler will execute the tasks in this sequence.
template<uint8_t num_slots>
class NaiveScheduler {
public:
void Init() {
uint8_t slot = 0;
// For a given task, occupy $priority available slots, spaced apart by
// #total slots / $priority.
for (uint8_t i = 0; i < sizeof(slots_); ++i) {
slots_[i] = 0;
}
for (uint8_t i = 0; i < sizeof(tasks_) / sizeof(Task); ++i) {
for (uint8_t j = 0; j < tasks_[i].priority; ++j) {
// Search for the next available slot.
while (1) {
if (slot >= sizeof(slots_)) {
slot = 0;
}
if (slots_[slot] == 0) {
break;
}
++slot;
}
slots_[slot] = i + 1;
slot += sizeof(slots_) / tasks_[i].priority;
}
}
}
void Run() {
while (1) {
++current_slot_;
if (current_slot_ >= sizeof(slots_)) {
current_slot_ = 0;
}
if (slots_[current_slot_]) {
tasks_[slots_[current_slot_] - 1].code();
}
}
}
private:
static Task tasks_[];
static uint8_t slots_[num_slots];
static uint8_t current_slot_;
};
template<uint8_t num_slots>
uint8_t NaiveScheduler<num_slots>::slots_[num_slots];
template<uint8_t num_slots>
uint8_t NaiveScheduler<num_slots>::current_slot_;
} // namespace avrlib
#endif // AVRLIB_TASK_H_