1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Simple SMP Scheduler Implementation |
---|
5 | * |
---|
6 | * @ingroup ScoreSchedulerSMPSimple |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2013-2014 embedded brains GmbH. |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/schedulersimplesmp.h> |
---|
22 | #include <rtems/score/schedulersmpimpl.h> |
---|
23 | |
---|
24 | static Scheduler_simple_SMP_Context * |
---|
25 | _Scheduler_simple_SMP_Get_context( const Scheduler_Control *scheduler ) |
---|
26 | { |
---|
27 | return (Scheduler_simple_SMP_Context *) _Scheduler_Get_context( scheduler ); |
---|
28 | } |
---|
29 | |
---|
30 | static Scheduler_simple_SMP_Context * |
---|
31 | _Scheduler_simple_SMP_Get_self( Scheduler_Context *context ) |
---|
32 | { |
---|
33 | return (Scheduler_simple_SMP_Context *) context; |
---|
34 | } |
---|
35 | |
---|
36 | void _Scheduler_simple_SMP_Initialize( const Scheduler_Control *scheduler ) |
---|
37 | { |
---|
38 | Scheduler_simple_SMP_Context *self = |
---|
39 | _Scheduler_simple_SMP_Get_context( scheduler ); |
---|
40 | |
---|
41 | _Scheduler_SMP_Initialize( &self->Base ); |
---|
42 | _Chain_Initialize_empty( &self->Ready ); |
---|
43 | } |
---|
44 | |
---|
45 | bool _Scheduler_simple_SMP_Allocate( |
---|
46 | const Scheduler_Control *scheduler, |
---|
47 | Thread_Control *the_thread |
---|
48 | ) |
---|
49 | { |
---|
50 | Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( the_thread ); |
---|
51 | |
---|
52 | _Scheduler_SMP_Node_initialize( node ); |
---|
53 | |
---|
54 | return true; |
---|
55 | } |
---|
56 | |
---|
57 | static void _Scheduler_simple_SMP_Do_update( |
---|
58 | Scheduler_Context *context, |
---|
59 | Scheduler_Node *node, |
---|
60 | Priority_Control new_priority |
---|
61 | ) |
---|
62 | { |
---|
63 | (void) context; |
---|
64 | (void) node; |
---|
65 | (void) new_priority; |
---|
66 | } |
---|
67 | |
---|
68 | static Thread_Control *_Scheduler_simple_SMP_Get_highest_ready( |
---|
69 | Scheduler_Context *context |
---|
70 | ) |
---|
71 | { |
---|
72 | Scheduler_simple_SMP_Context *self = |
---|
73 | _Scheduler_simple_SMP_Get_self( context ); |
---|
74 | |
---|
75 | return (Thread_Control *) _Chain_First( &self->Ready ); |
---|
76 | } |
---|
77 | |
---|
78 | static void _Scheduler_simple_SMP_Move_from_scheduled_to_ready( |
---|
79 | Scheduler_Context *context, |
---|
80 | Thread_Control *scheduled_to_ready |
---|
81 | ) |
---|
82 | { |
---|
83 | Scheduler_simple_SMP_Context *self = |
---|
84 | _Scheduler_simple_SMP_Get_self( context ); |
---|
85 | |
---|
86 | _Chain_Extract_unprotected( &scheduled_to_ready->Object.Node ); |
---|
87 | _Scheduler_simple_Insert_priority_lifo( |
---|
88 | &self->Ready, |
---|
89 | scheduled_to_ready |
---|
90 | ); |
---|
91 | } |
---|
92 | |
---|
93 | static void _Scheduler_simple_SMP_Move_from_ready_to_scheduled( |
---|
94 | Scheduler_Context *context, |
---|
95 | Thread_Control *ready_to_scheduled |
---|
96 | ) |
---|
97 | { |
---|
98 | Scheduler_simple_SMP_Context *self = |
---|
99 | _Scheduler_simple_SMP_Get_self( context ); |
---|
100 | |
---|
101 | _Chain_Extract_unprotected( &ready_to_scheduled->Object.Node ); |
---|
102 | _Scheduler_simple_Insert_priority_fifo( |
---|
103 | &self->Base.Scheduled, |
---|
104 | ready_to_scheduled |
---|
105 | ); |
---|
106 | } |
---|
107 | |
---|
108 | static void _Scheduler_simple_SMP_Insert_ready_lifo( |
---|
109 | Scheduler_Context *context, |
---|
110 | Thread_Control *thread |
---|
111 | ) |
---|
112 | { |
---|
113 | Scheduler_simple_SMP_Context *self = |
---|
114 | _Scheduler_simple_SMP_Get_self( context ); |
---|
115 | |
---|
116 | _Chain_Insert_ordered_unprotected( |
---|
117 | &self->Ready, |
---|
118 | &thread->Object.Node, |
---|
119 | _Scheduler_simple_Insert_priority_lifo_order |
---|
120 | ); |
---|
121 | } |
---|
122 | |
---|
123 | static void _Scheduler_simple_SMP_Insert_ready_fifo( |
---|
124 | Scheduler_Context *context, |
---|
125 | Thread_Control *thread |
---|
126 | ) |
---|
127 | { |
---|
128 | Scheduler_simple_SMP_Context *self = |
---|
129 | _Scheduler_simple_SMP_Get_self( context ); |
---|
130 | |
---|
131 | _Chain_Insert_ordered_unprotected( |
---|
132 | &self->Ready, |
---|
133 | &thread->Object.Node, |
---|
134 | _Scheduler_simple_Insert_priority_fifo_order |
---|
135 | ); |
---|
136 | } |
---|
137 | |
---|
138 | static void _Scheduler_simple_SMP_Extract_from_ready( |
---|
139 | Scheduler_Context *context, |
---|
140 | Thread_Control *thread |
---|
141 | ) |
---|
142 | { |
---|
143 | (void) context; |
---|
144 | |
---|
145 | _Chain_Extract_unprotected( &thread->Object.Node ); |
---|
146 | } |
---|
147 | |
---|
148 | void _Scheduler_simple_SMP_Block( |
---|
149 | const Scheduler_Control *scheduler, |
---|
150 | Thread_Control *thread |
---|
151 | ) |
---|
152 | { |
---|
153 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
154 | |
---|
155 | _Scheduler_SMP_Block( |
---|
156 | context, |
---|
157 | thread, |
---|
158 | _Scheduler_simple_SMP_Extract_from_ready, |
---|
159 | _Scheduler_simple_SMP_Get_highest_ready, |
---|
160 | _Scheduler_simple_SMP_Move_from_ready_to_scheduled |
---|
161 | ); |
---|
162 | } |
---|
163 | |
---|
164 | static void _Scheduler_simple_SMP_Enqueue_ordered( |
---|
165 | Scheduler_Context *context, |
---|
166 | Thread_Control *thread, |
---|
167 | Chain_Node_order order, |
---|
168 | Scheduler_SMP_Insert insert_ready, |
---|
169 | Scheduler_SMP_Insert insert_scheduled |
---|
170 | ) |
---|
171 | { |
---|
172 | _Scheduler_SMP_Enqueue_ordered( |
---|
173 | context, |
---|
174 | thread, |
---|
175 | order, |
---|
176 | insert_ready, |
---|
177 | insert_scheduled, |
---|
178 | _Scheduler_simple_SMP_Move_from_scheduled_to_ready |
---|
179 | ); |
---|
180 | } |
---|
181 | |
---|
182 | static void _Scheduler_simple_SMP_Enqueue_lifo( |
---|
183 | Scheduler_Context *context, |
---|
184 | Thread_Control *thread |
---|
185 | ) |
---|
186 | { |
---|
187 | _Scheduler_simple_SMP_Enqueue_ordered( |
---|
188 | context, |
---|
189 | thread, |
---|
190 | _Scheduler_simple_Insert_priority_lifo_order, |
---|
191 | _Scheduler_simple_SMP_Insert_ready_lifo, |
---|
192 | _Scheduler_SMP_Insert_scheduled_lifo |
---|
193 | ); |
---|
194 | } |
---|
195 | |
---|
196 | static void _Scheduler_simple_SMP_Enqueue_fifo( |
---|
197 | Scheduler_Context *context, |
---|
198 | Thread_Control *thread |
---|
199 | ) |
---|
200 | { |
---|
201 | _Scheduler_simple_SMP_Enqueue_ordered( |
---|
202 | context, |
---|
203 | thread, |
---|
204 | _Scheduler_simple_Insert_priority_fifo_order, |
---|
205 | _Scheduler_simple_SMP_Insert_ready_fifo, |
---|
206 | _Scheduler_SMP_Insert_scheduled_fifo |
---|
207 | ); |
---|
208 | } |
---|
209 | |
---|
210 | static void _Scheduler_simple_SMP_Enqueue_scheduled_ordered( |
---|
211 | Scheduler_Context *context, |
---|
212 | Thread_Control *thread, |
---|
213 | Chain_Node_order order, |
---|
214 | Scheduler_SMP_Insert insert_ready, |
---|
215 | Scheduler_SMP_Insert insert_scheduled |
---|
216 | ) |
---|
217 | { |
---|
218 | _Scheduler_SMP_Enqueue_scheduled_ordered( |
---|
219 | context, |
---|
220 | thread, |
---|
221 | order, |
---|
222 | _Scheduler_simple_SMP_Get_highest_ready, |
---|
223 | insert_ready, |
---|
224 | insert_scheduled, |
---|
225 | _Scheduler_simple_SMP_Move_from_ready_to_scheduled |
---|
226 | ); |
---|
227 | } |
---|
228 | |
---|
229 | static void _Scheduler_simple_SMP_Enqueue_scheduled_lifo( |
---|
230 | Scheduler_Context *context, |
---|
231 | Thread_Control *thread |
---|
232 | ) |
---|
233 | { |
---|
234 | _Scheduler_simple_SMP_Enqueue_scheduled_ordered( |
---|
235 | context, |
---|
236 | thread, |
---|
237 | _Scheduler_simple_Insert_priority_lifo_order, |
---|
238 | _Scheduler_simple_SMP_Insert_ready_lifo, |
---|
239 | _Scheduler_SMP_Insert_scheduled_lifo |
---|
240 | ); |
---|
241 | } |
---|
242 | |
---|
243 | static void _Scheduler_simple_SMP_Enqueue_scheduled_fifo( |
---|
244 | Scheduler_Context *context, |
---|
245 | Thread_Control *thread |
---|
246 | ) |
---|
247 | { |
---|
248 | _Scheduler_simple_SMP_Enqueue_scheduled_ordered( |
---|
249 | context, |
---|
250 | thread, |
---|
251 | _Scheduler_simple_Insert_priority_fifo_order, |
---|
252 | _Scheduler_simple_SMP_Insert_ready_fifo, |
---|
253 | _Scheduler_SMP_Insert_scheduled_fifo |
---|
254 | ); |
---|
255 | } |
---|
256 | |
---|
257 | void _Scheduler_simple_SMP_Unblock( |
---|
258 | const Scheduler_Control *scheduler, |
---|
259 | Thread_Control *thread |
---|
260 | ) |
---|
261 | { |
---|
262 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
263 | |
---|
264 | _Scheduler_SMP_Unblock( |
---|
265 | context, |
---|
266 | thread, |
---|
267 | _Scheduler_simple_SMP_Enqueue_fifo |
---|
268 | ); |
---|
269 | } |
---|
270 | |
---|
271 | void _Scheduler_simple_SMP_Change_priority( |
---|
272 | const Scheduler_Control *scheduler, |
---|
273 | Thread_Control *thread, |
---|
274 | Priority_Control new_priority, |
---|
275 | bool prepend_it |
---|
276 | ) |
---|
277 | { |
---|
278 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
279 | |
---|
280 | _Scheduler_SMP_Change_priority( |
---|
281 | context, |
---|
282 | thread, |
---|
283 | new_priority, |
---|
284 | prepend_it, |
---|
285 | _Scheduler_simple_SMP_Extract_from_ready, |
---|
286 | _Scheduler_simple_SMP_Do_update, |
---|
287 | _Scheduler_simple_SMP_Enqueue_fifo, |
---|
288 | _Scheduler_simple_SMP_Enqueue_lifo, |
---|
289 | _Scheduler_simple_SMP_Enqueue_scheduled_fifo, |
---|
290 | _Scheduler_simple_SMP_Enqueue_scheduled_lifo |
---|
291 | ); |
---|
292 | } |
---|
293 | |
---|
294 | void _Scheduler_simple_SMP_Yield( |
---|
295 | const Scheduler_Control *scheduler, |
---|
296 | Thread_Control *thread |
---|
297 | ) |
---|
298 | { |
---|
299 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
300 | ISR_Level level; |
---|
301 | |
---|
302 | _ISR_Disable( level ); |
---|
303 | |
---|
304 | _Scheduler_SMP_Extract_from_scheduled( thread ); |
---|
305 | _Scheduler_simple_SMP_Enqueue_scheduled_fifo( context, thread ); |
---|
306 | |
---|
307 | _ISR_Enable( level ); |
---|
308 | } |
---|