1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Deterministic Priority SMP Scheduler Implementation |
---|
5 | * |
---|
6 | * @ingroup ScoreSchedulerSMP |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2013-2014 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Dornierstr. 4 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #if HAVE_CONFIG_H |
---|
24 | #include "config.h" |
---|
25 | #endif |
---|
26 | |
---|
27 | #include <rtems/score/schedulerprioritysmp.h> |
---|
28 | #include <rtems/score/schedulerpriorityimpl.h> |
---|
29 | #include <rtems/score/schedulersmpimpl.h> |
---|
30 | #include <rtems/score/wkspace.h> |
---|
31 | |
---|
32 | static Scheduler_priority_SMP_Context * |
---|
33 | _Scheduler_priority_SMP_Get_context( const Scheduler_Control *scheduler ) |
---|
34 | { |
---|
35 | return (Scheduler_priority_SMP_Context *) scheduler->context; |
---|
36 | } |
---|
37 | |
---|
38 | static Scheduler_priority_SMP_Context * |
---|
39 | _Scheduler_priority_SMP_Self_from_SMP_base( Scheduler_SMP_Context *smp_base ) |
---|
40 | { |
---|
41 | return (Scheduler_priority_SMP_Context *) |
---|
42 | ( (char *) smp_base |
---|
43 | - offsetof( Scheduler_priority_SMP_Context, Base ) ); |
---|
44 | } |
---|
45 | |
---|
46 | void _Scheduler_priority_SMP_Initialize( const Scheduler_Control *scheduler ) |
---|
47 | { |
---|
48 | Scheduler_priority_SMP_Context *self = |
---|
49 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
50 | |
---|
51 | _Scheduler_SMP_Initialize( &self->Base ); |
---|
52 | _Priority_bit_map_Initialize( &self->Bit_map ); |
---|
53 | _Scheduler_priority_Ready_queue_initialize( &self->Ready[ 0 ] ); |
---|
54 | } |
---|
55 | |
---|
56 | void _Scheduler_priority_SMP_Update( |
---|
57 | const Scheduler_Control *scheduler, |
---|
58 | Thread_Control *thread |
---|
59 | ) |
---|
60 | { |
---|
61 | Scheduler_priority_SMP_Context *self = |
---|
62 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
63 | |
---|
64 | _Scheduler_priority_Update_body( |
---|
65 | thread, |
---|
66 | &self->Bit_map, |
---|
67 | &self->Ready[ 0 ] |
---|
68 | ); |
---|
69 | } |
---|
70 | |
---|
71 | static Thread_Control *_Scheduler_priority_SMP_Get_highest_ready( |
---|
72 | Scheduler_SMP_Context *smp_base |
---|
73 | ) |
---|
74 | { |
---|
75 | Scheduler_priority_SMP_Context *self = |
---|
76 | _Scheduler_priority_SMP_Self_from_SMP_base( smp_base ); |
---|
77 | Thread_Control *highest_ready = NULL; |
---|
78 | |
---|
79 | if ( !_Priority_bit_map_Is_empty( &self->Bit_map ) ) { |
---|
80 | highest_ready = _Scheduler_priority_Ready_queue_first( |
---|
81 | &self->Bit_map, |
---|
82 | &self->Ready[ 0 ] |
---|
83 | ); |
---|
84 | } |
---|
85 | |
---|
86 | return highest_ready; |
---|
87 | } |
---|
88 | |
---|
89 | static void _Scheduler_priority_SMP_Move_from_scheduled_to_ready( |
---|
90 | Scheduler_SMP_Context *smp_base, |
---|
91 | Thread_Control *scheduled_to_ready |
---|
92 | ) |
---|
93 | { |
---|
94 | Scheduler_priority_SMP_Context *self = |
---|
95 | _Scheduler_priority_SMP_Self_from_SMP_base( smp_base ); |
---|
96 | |
---|
97 | _Chain_Extract_unprotected( &scheduled_to_ready->Object.Node ); |
---|
98 | _Scheduler_priority_Ready_queue_enqueue_first( |
---|
99 | scheduled_to_ready, |
---|
100 | &self->Bit_map |
---|
101 | ); |
---|
102 | } |
---|
103 | |
---|
104 | static void _Scheduler_priority_SMP_Move_from_ready_to_scheduled( |
---|
105 | Scheduler_SMP_Context *smp_base, |
---|
106 | Thread_Control *ready_to_scheduled |
---|
107 | ) |
---|
108 | { |
---|
109 | Scheduler_priority_SMP_Context *self = |
---|
110 | _Scheduler_priority_SMP_Self_from_SMP_base( smp_base ); |
---|
111 | |
---|
112 | _Scheduler_priority_Ready_queue_extract( |
---|
113 | ready_to_scheduled, |
---|
114 | &self->Bit_map |
---|
115 | ); |
---|
116 | _Scheduler_simple_Insert_priority_fifo( |
---|
117 | &self->Base.Scheduled, |
---|
118 | ready_to_scheduled |
---|
119 | ); |
---|
120 | } |
---|
121 | |
---|
122 | static void _Scheduler_priority_SMP_Insert_ready_lifo( |
---|
123 | Scheduler_SMP_Context *smp_base, |
---|
124 | Thread_Control *thread |
---|
125 | ) |
---|
126 | { |
---|
127 | Scheduler_priority_SMP_Context *self = |
---|
128 | _Scheduler_priority_SMP_Self_from_SMP_base( smp_base ); |
---|
129 | |
---|
130 | _Scheduler_priority_Ready_queue_enqueue( thread, &self->Bit_map ); |
---|
131 | } |
---|
132 | |
---|
133 | static void _Scheduler_priority_SMP_Insert_ready_fifo( |
---|
134 | Scheduler_SMP_Context *smp_base, |
---|
135 | Thread_Control *thread |
---|
136 | ) |
---|
137 | { |
---|
138 | Scheduler_priority_SMP_Context *self = |
---|
139 | _Scheduler_priority_SMP_Self_from_SMP_base( smp_base ); |
---|
140 | |
---|
141 | _Scheduler_priority_Ready_queue_enqueue_first( thread, &self->Bit_map ); |
---|
142 | } |
---|
143 | |
---|
144 | static void _Scheduler_priority_SMP_Do_extract( |
---|
145 | Scheduler_SMP_Context *smp_base, |
---|
146 | Thread_Control *thread |
---|
147 | ) |
---|
148 | { |
---|
149 | Scheduler_priority_SMP_Context *self = |
---|
150 | _Scheduler_priority_SMP_Self_from_SMP_base( smp_base ); |
---|
151 | bool is_scheduled = thread->is_scheduled; |
---|
152 | |
---|
153 | thread->is_in_the_air = is_scheduled; |
---|
154 | thread->is_scheduled = false; |
---|
155 | |
---|
156 | if ( is_scheduled ) { |
---|
157 | _Chain_Extract_unprotected( &thread->Object.Node ); |
---|
158 | } else { |
---|
159 | _Scheduler_priority_Ready_queue_extract( thread, &self->Bit_map ); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | void _Scheduler_priority_SMP_Block( |
---|
164 | const Scheduler_Control *scheduler, |
---|
165 | Thread_Control *thread |
---|
166 | ) |
---|
167 | { |
---|
168 | Scheduler_priority_SMP_Context *self = |
---|
169 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
170 | |
---|
171 | _Scheduler_SMP_Block( |
---|
172 | &self->Base, |
---|
173 | thread, |
---|
174 | _Scheduler_priority_SMP_Do_extract, |
---|
175 | _Scheduler_priority_SMP_Get_highest_ready, |
---|
176 | _Scheduler_priority_SMP_Move_from_ready_to_scheduled |
---|
177 | ); |
---|
178 | } |
---|
179 | |
---|
180 | static void _Scheduler_priority_SMP_Enqueue_ordered( |
---|
181 | Scheduler_SMP_Context *self, |
---|
182 | Thread_Control *thread, |
---|
183 | Chain_Node_order order, |
---|
184 | Scheduler_SMP_Insert insert_ready, |
---|
185 | Scheduler_SMP_Insert insert_scheduled |
---|
186 | ) |
---|
187 | { |
---|
188 | _Scheduler_SMP_Enqueue_ordered( |
---|
189 | self, |
---|
190 | thread, |
---|
191 | order, |
---|
192 | _Scheduler_priority_SMP_Get_highest_ready, |
---|
193 | insert_ready, |
---|
194 | insert_scheduled, |
---|
195 | _Scheduler_priority_SMP_Move_from_ready_to_scheduled, |
---|
196 | _Scheduler_priority_SMP_Move_from_scheduled_to_ready |
---|
197 | ); |
---|
198 | } |
---|
199 | |
---|
200 | void _Scheduler_priority_SMP_Enqueue_lifo( |
---|
201 | const Scheduler_Control *scheduler, |
---|
202 | Thread_Control *thread |
---|
203 | ) |
---|
204 | { |
---|
205 | Scheduler_priority_SMP_Context *self = |
---|
206 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
207 | |
---|
208 | _Scheduler_priority_SMP_Enqueue_ordered( |
---|
209 | &self->Base, |
---|
210 | thread, |
---|
211 | _Scheduler_simple_Insert_priority_lifo_order, |
---|
212 | _Scheduler_priority_SMP_Insert_ready_lifo, |
---|
213 | _Scheduler_SMP_Insert_scheduled_lifo |
---|
214 | ); |
---|
215 | } |
---|
216 | |
---|
217 | void _Scheduler_priority_SMP_Enqueue_fifo( |
---|
218 | const Scheduler_Control *scheduler, |
---|
219 | Thread_Control *thread |
---|
220 | ) |
---|
221 | { |
---|
222 | Scheduler_priority_SMP_Context *self = |
---|
223 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
224 | |
---|
225 | _Scheduler_priority_SMP_Enqueue_ordered( |
---|
226 | &self->Base, |
---|
227 | thread, |
---|
228 | _Scheduler_simple_Insert_priority_fifo_order, |
---|
229 | _Scheduler_priority_SMP_Insert_ready_fifo, |
---|
230 | _Scheduler_SMP_Insert_scheduled_fifo |
---|
231 | ); |
---|
232 | } |
---|
233 | |
---|
234 | void _Scheduler_priority_SMP_Extract( |
---|
235 | const Scheduler_Control *scheduler, |
---|
236 | Thread_Control *thread |
---|
237 | ) |
---|
238 | { |
---|
239 | Scheduler_priority_SMP_Context *self = |
---|
240 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
241 | |
---|
242 | _Scheduler_SMP_Extract( |
---|
243 | &self->Base, |
---|
244 | thread, |
---|
245 | _Scheduler_priority_SMP_Do_extract |
---|
246 | ); |
---|
247 | } |
---|
248 | |
---|
249 | void _Scheduler_priority_SMP_Yield( |
---|
250 | const Scheduler_Control *scheduler, |
---|
251 | Thread_Control *thread |
---|
252 | ) |
---|
253 | { |
---|
254 | ISR_Level level; |
---|
255 | |
---|
256 | _ISR_Disable( level ); |
---|
257 | |
---|
258 | _Scheduler_priority_SMP_Extract( scheduler, thread ); |
---|
259 | _Scheduler_priority_SMP_Enqueue_fifo( scheduler, thread ); |
---|
260 | |
---|
261 | _ISR_Enable( level ); |
---|
262 | } |
---|
263 | |
---|
264 | void _Scheduler_priority_SMP_Schedule( |
---|
265 | const Scheduler_Control *scheduler, |
---|
266 | Thread_Control *thread |
---|
267 | ) |
---|
268 | { |
---|
269 | Scheduler_priority_SMP_Context *self = |
---|
270 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
271 | |
---|
272 | _Scheduler_SMP_Schedule( |
---|
273 | &self->Base, |
---|
274 | thread, |
---|
275 | _Scheduler_priority_SMP_Get_highest_ready, |
---|
276 | _Scheduler_priority_SMP_Move_from_ready_to_scheduled |
---|
277 | ); |
---|
278 | } |
---|
279 | |
---|
280 | void _Scheduler_priority_SMP_Start_idle( |
---|
281 | const Scheduler_Control *scheduler, |
---|
282 | Thread_Control *thread, |
---|
283 | Per_CPU_Control *cpu |
---|
284 | ) |
---|
285 | { |
---|
286 | Scheduler_priority_SMP_Context *self = |
---|
287 | _Scheduler_priority_SMP_Get_context( scheduler ); |
---|
288 | |
---|
289 | _Scheduler_SMP_Start_idle( &self->Base, thread, cpu ); |
---|
290 | } |
---|