[a936aa49] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Simple SMP Scheduler Implementation |
---|
| 5 | * |
---|
[beab7329] | 6 | * @ingroup ScoreSchedulerSMPSimple |
---|
[a936aa49] | 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
[351c14d] | 10 | * Copyright (c) 2013, 2016 embedded brains GmbH. |
---|
[a936aa49] | 11 | * |
---|
| 12 | * The license and distribution terms for this file may be |
---|
| 13 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 14 | * http://www.rtems.org/license/LICENSE. |
---|
[a936aa49] | 15 | */ |
---|
| 16 | |
---|
| 17 | #if HAVE_CONFIG_H |
---|
| 18 | #include "config.h" |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | #include <rtems/score/schedulersimplesmp.h> |
---|
[9d83f58a] | 22 | #include <rtems/score/schedulersmpimpl.h> |
---|
[a936aa49] | 23 | |
---|
[e1598a6] | 24 | static Scheduler_simple_SMP_Context * |
---|
| 25 | _Scheduler_simple_SMP_Get_context( const Scheduler_Control *scheduler ) |
---|
[494c2e3] | 26 | { |
---|
[2369b10] | 27 | return (Scheduler_simple_SMP_Context *) _Scheduler_Get_context( scheduler ); |
---|
[494c2e3] | 28 | } |
---|
| 29 | |
---|
[e1598a6] | 30 | static Scheduler_simple_SMP_Context * |
---|
[3730a07f] | 31 | _Scheduler_simple_SMP_Get_self( Scheduler_Context *context ) |
---|
[494c2e3] | 32 | { |
---|
[3730a07f] | 33 | return (Scheduler_simple_SMP_Context *) context; |
---|
[494c2e3] | 34 | } |
---|
| 35 | |
---|
[e9ee2f0] | 36 | void _Scheduler_simple_SMP_Initialize( const Scheduler_Control *scheduler ) |
---|
[a936aa49] | 37 | { |
---|
[e1598a6] | 38 | Scheduler_simple_SMP_Context *self = |
---|
| 39 | _Scheduler_simple_SMP_Get_context( scheduler ); |
---|
[a936aa49] | 40 | |
---|
[494c2e3] | 41 | _Scheduler_SMP_Initialize( &self->Base ); |
---|
| 42 | _Chain_Initialize_empty( &self->Ready ); |
---|
[a936aa49] | 43 | } |
---|
| 44 | |
---|
[8e467384] | 45 | void _Scheduler_simple_SMP_Node_initialize( |
---|
[beab7329] | 46 | const Scheduler_Control *scheduler, |
---|
[df2177ab] | 47 | Scheduler_Node *node, |
---|
[9bfad8c] | 48 | Thread_Control *the_thread, |
---|
| 49 | Priority_Control priority |
---|
[beab7329] | 50 | ) |
---|
| 51 | { |
---|
[351c14d] | 52 | Scheduler_SMP_Node *smp_node; |
---|
[beab7329] | 53 | |
---|
[351c14d] | 54 | smp_node = _Scheduler_SMP_Node_downcast( node ); |
---|
| 55 | _Scheduler_SMP_Node_initialize( scheduler, smp_node, the_thread, priority ); |
---|
[beab7329] | 56 | } |
---|
| 57 | |
---|
[f39f667a] | 58 | static void _Scheduler_simple_SMP_Do_update( |
---|
| 59 | Scheduler_Context *context, |
---|
[df2177ab] | 60 | Scheduler_Node *node, |
---|
[8f0c7a46] | 61 | Priority_Control new_priority |
---|
[f39f667a] | 62 | ) |
---|
| 63 | { |
---|
[351c14d] | 64 | Scheduler_SMP_Node *smp_node; |
---|
[8f0c7a46] | 65 | |
---|
[f39f667a] | 66 | (void) context; |
---|
[8f0c7a46] | 67 | |
---|
[351c14d] | 68 | smp_node = _Scheduler_SMP_Node_downcast( node ); |
---|
| 69 | _Scheduler_SMP_Node_update_priority( smp_node, new_priority ); |
---|
[f39f667a] | 70 | } |
---|
| 71 | |
---|
[8f0c7a46] | 72 | static Scheduler_Node *_Scheduler_simple_SMP_Get_highest_ready( |
---|
[238629f] | 73 | Scheduler_Context *context, |
---|
[8f0c7a46] | 74 | Scheduler_Node *node |
---|
[aea4a91] | 75 | ) |
---|
| 76 | { |
---|
[e1598a6] | 77 | Scheduler_simple_SMP_Context *self = |
---|
[3730a07f] | 78 | _Scheduler_simple_SMP_Get_self( context ); |
---|
[333f9426] | 79 | Scheduler_Node *first = (Scheduler_Node *) _Chain_First( &self->Ready ); |
---|
[aea4a91] | 80 | |
---|
[8f0c7a46] | 81 | (void) node; |
---|
[238629f] | 82 | |
---|
[333f9426] | 83 | _Assert( &first->Node != _Chain_Tail( &self->Ready ) ); |
---|
| 84 | |
---|
| 85 | return first; |
---|
[aea4a91] | 86 | } |
---|
| 87 | |
---|
[e9ee2f0] | 88 | static void _Scheduler_simple_SMP_Move_from_scheduled_to_ready( |
---|
[3730a07f] | 89 | Scheduler_Context *context, |
---|
[8f0c7a46] | 90 | Scheduler_Node *scheduled_to_ready |
---|
[a936aa49] | 91 | ) |
---|
| 92 | { |
---|
[e1598a6] | 93 | Scheduler_simple_SMP_Context *self = |
---|
[3730a07f] | 94 | _Scheduler_simple_SMP_Get_self( context ); |
---|
[494c2e3] | 95 | |
---|
[8f0c7a46] | 96 | _Chain_Extract_unprotected( &scheduled_to_ready->Node ); |
---|
| 97 | _Chain_Insert_ordered_unprotected( |
---|
[494c2e3] | 98 | &self->Ready, |
---|
[8f0c7a46] | 99 | &scheduled_to_ready->Node, |
---|
| 100 | _Scheduler_SMP_Insert_priority_lifo_order |
---|
[aea4a91] | 101 | ); |
---|
[a936aa49] | 102 | } |
---|
| 103 | |
---|
[e9ee2f0] | 104 | static void _Scheduler_simple_SMP_Move_from_ready_to_scheduled( |
---|
[3730a07f] | 105 | Scheduler_Context *context, |
---|
[8f0c7a46] | 106 | Scheduler_Node *ready_to_scheduled |
---|
[a936aa49] | 107 | ) |
---|
| 108 | { |
---|
[3730a07f] | 109 | Scheduler_simple_SMP_Context *self = |
---|
| 110 | _Scheduler_simple_SMP_Get_self( context ); |
---|
| 111 | |
---|
[8f0c7a46] | 112 | _Chain_Extract_unprotected( &ready_to_scheduled->Node ); |
---|
| 113 | _Chain_Insert_ordered_unprotected( |
---|
[3730a07f] | 114 | &self->Base.Scheduled, |
---|
[8f0c7a46] | 115 | &ready_to_scheduled->Node, |
---|
| 116 | _Scheduler_SMP_Insert_priority_fifo_order |
---|
[aea4a91] | 117 | ); |
---|
[a936aa49] | 118 | } |
---|
| 119 | |
---|
[e9ee2f0] | 120 | static void _Scheduler_simple_SMP_Insert_ready_lifo( |
---|
[3730a07f] | 121 | Scheduler_Context *context, |
---|
[8f0c7a46] | 122 | Scheduler_Node *node_to_insert |
---|
[a936aa49] | 123 | ) |
---|
| 124 | { |
---|
[e1598a6] | 125 | Scheduler_simple_SMP_Context *self = |
---|
[3730a07f] | 126 | _Scheduler_simple_SMP_Get_self( context ); |
---|
[494c2e3] | 127 | |
---|
[48c4a55] | 128 | _Chain_Insert_ordered_unprotected( |
---|
[494c2e3] | 129 | &self->Ready, |
---|
[8f0c7a46] | 130 | &node_to_insert->Node, |
---|
| 131 | _Scheduler_SMP_Insert_priority_lifo_order |
---|
[48c4a55] | 132 | ); |
---|
[a936aa49] | 133 | } |
---|
| 134 | |
---|
[e9ee2f0] | 135 | static void _Scheduler_simple_SMP_Insert_ready_fifo( |
---|
[3730a07f] | 136 | Scheduler_Context *context, |
---|
[8f0c7a46] | 137 | Scheduler_Node *node_to_insert |
---|
[aea4a91] | 138 | ) |
---|
| 139 | { |
---|
[e1598a6] | 140 | Scheduler_simple_SMP_Context *self = |
---|
[3730a07f] | 141 | _Scheduler_simple_SMP_Get_self( context ); |
---|
[494c2e3] | 142 | |
---|
[48c4a55] | 143 | _Chain_Insert_ordered_unprotected( |
---|
[494c2e3] | 144 | &self->Ready, |
---|
[8f0c7a46] | 145 | &node_to_insert->Node, |
---|
| 146 | _Scheduler_SMP_Insert_priority_fifo_order |
---|
[48c4a55] | 147 | ); |
---|
[aea4a91] | 148 | } |
---|
| 149 | |
---|
[f39f667a] | 150 | static void _Scheduler_simple_SMP_Extract_from_ready( |
---|
[3730a07f] | 151 | Scheduler_Context *context, |
---|
[8f0c7a46] | 152 | Scheduler_Node *node_to_extract |
---|
[a936aa49] | 153 | ) |
---|
| 154 | { |
---|
[3730a07f] | 155 | (void) context; |
---|
[a936aa49] | 156 | |
---|
[8f0c7a46] | 157 | _Chain_Extract_unprotected( &node_to_extract->Node ); |
---|
[aea4a91] | 158 | } |
---|
[a936aa49] | 159 | |
---|
[e9ee2f0] | 160 | void _Scheduler_simple_SMP_Block( |
---|
[e1598a6] | 161 | const Scheduler_Control *scheduler, |
---|
[e382a1b] | 162 | Thread_Control *thread, |
---|
| 163 | Scheduler_Node *node |
---|
[24934e36] | 164 | ) |
---|
[aea4a91] | 165 | { |
---|
[f39f667a] | 166 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
[a936aa49] | 167 | |
---|
[48c4a55] | 168 | _Scheduler_SMP_Block( |
---|
[f39f667a] | 169 | context, |
---|
[48c4a55] | 170 | thread, |
---|
[e382a1b] | 171 | node, |
---|
[f39f667a] | 172 | _Scheduler_simple_SMP_Extract_from_ready, |
---|
[e9ee2f0] | 173 | _Scheduler_simple_SMP_Get_highest_ready, |
---|
[238629f] | 174 | _Scheduler_simple_SMP_Move_from_ready_to_scheduled, |
---|
[27783f6] | 175 | _Scheduler_SMP_Allocate_processor_lazy |
---|
[48c4a55] | 176 | ); |
---|
| 177 | } |
---|
[a936aa49] | 178 | |
---|
[8568341] | 179 | static Thread_Control *_Scheduler_simple_SMP_Enqueue_ordered( |
---|
| 180 | Scheduler_Context *context, |
---|
| 181 | Scheduler_Node *node, |
---|
| 182 | Thread_Control *needs_help, |
---|
| 183 | Chain_Node_order order, |
---|
| 184 | Scheduler_SMP_Insert insert_ready, |
---|
| 185 | Scheduler_SMP_Insert insert_scheduled |
---|
[48c4a55] | 186 | ) |
---|
| 187 | { |
---|
[8568341] | 188 | return _Scheduler_SMP_Enqueue_ordered( |
---|
[3730a07f] | 189 | context, |
---|
[8f0c7a46] | 190 | node, |
---|
[8568341] | 191 | needs_help, |
---|
[48c4a55] | 192 | order, |
---|
| 193 | insert_ready, |
---|
| 194 | insert_scheduled, |
---|
[238629f] | 195 | _Scheduler_simple_SMP_Move_from_scheduled_to_ready, |
---|
| 196 | _Scheduler_SMP_Get_lowest_scheduled, |
---|
[27783f6] | 197 | _Scheduler_SMP_Allocate_processor_lazy |
---|
[48c4a55] | 198 | ); |
---|
[a936aa49] | 199 | } |
---|
| 200 | |
---|
[8568341] | 201 | static Thread_Control *_Scheduler_simple_SMP_Enqueue_lifo( |
---|
[f39f667a] | 202 | Scheduler_Context *context, |
---|
[8568341] | 203 | Scheduler_Node *node, |
---|
| 204 | Thread_Control *needs_help |
---|
[24934e36] | 205 | ) |
---|
[a936aa49] | 206 | { |
---|
[8568341] | 207 | return _Scheduler_simple_SMP_Enqueue_ordered( |
---|
[f39f667a] | 208 | context, |
---|
[8f0c7a46] | 209 | node, |
---|
[8568341] | 210 | needs_help, |
---|
[8f0c7a46] | 211 | _Scheduler_SMP_Insert_priority_lifo_order, |
---|
[e9ee2f0] | 212 | _Scheduler_simple_SMP_Insert_ready_lifo, |
---|
[48c4a55] | 213 | _Scheduler_SMP_Insert_scheduled_lifo |
---|
[a936aa49] | 214 | ); |
---|
| 215 | } |
---|
| 216 | |
---|
[8568341] | 217 | static Thread_Control *_Scheduler_simple_SMP_Enqueue_fifo( |
---|
[f39f667a] | 218 | Scheduler_Context *context, |
---|
[8568341] | 219 | Scheduler_Node *node, |
---|
| 220 | Thread_Control *needs_help |
---|
[24934e36] | 221 | ) |
---|
[a936aa49] | 222 | { |
---|
[8568341] | 223 | return _Scheduler_simple_SMP_Enqueue_ordered( |
---|
[f39f667a] | 224 | context, |
---|
[8f0c7a46] | 225 | node, |
---|
[8568341] | 226 | needs_help, |
---|
[8f0c7a46] | 227 | _Scheduler_SMP_Insert_priority_fifo_order, |
---|
[c0bff5e] | 228 | _Scheduler_simple_SMP_Insert_ready_fifo, |
---|
| 229 | _Scheduler_SMP_Insert_scheduled_fifo |
---|
| 230 | ); |
---|
| 231 | } |
---|
| 232 | |
---|
[8568341] | 233 | static Thread_Control *_Scheduler_simple_SMP_Enqueue_scheduled_ordered( |
---|
[c0bff5e] | 234 | Scheduler_Context *context, |
---|
[8f0c7a46] | 235 | Scheduler_Node *node, |
---|
[c0bff5e] | 236 | Chain_Node_order order, |
---|
| 237 | Scheduler_SMP_Insert insert_ready, |
---|
| 238 | Scheduler_SMP_Insert insert_scheduled |
---|
| 239 | ) |
---|
| 240 | { |
---|
[8568341] | 241 | return _Scheduler_SMP_Enqueue_scheduled_ordered( |
---|
[c0bff5e] | 242 | context, |
---|
[8f0c7a46] | 243 | node, |
---|
[c0bff5e] | 244 | order, |
---|
[5c3d250] | 245 | _Scheduler_simple_SMP_Extract_from_ready, |
---|
[c0bff5e] | 246 | _Scheduler_simple_SMP_Get_highest_ready, |
---|
| 247 | insert_ready, |
---|
| 248 | insert_scheduled, |
---|
[238629f] | 249 | _Scheduler_simple_SMP_Move_from_ready_to_scheduled, |
---|
[27783f6] | 250 | _Scheduler_SMP_Allocate_processor_lazy |
---|
[c0bff5e] | 251 | ); |
---|
| 252 | } |
---|
| 253 | |
---|
[8568341] | 254 | static Thread_Control *_Scheduler_simple_SMP_Enqueue_scheduled_lifo( |
---|
[c0bff5e] | 255 | Scheduler_Context *context, |
---|
[8f0c7a46] | 256 | Scheduler_Node *node |
---|
[c0bff5e] | 257 | ) |
---|
| 258 | { |
---|
[8568341] | 259 | return _Scheduler_simple_SMP_Enqueue_scheduled_ordered( |
---|
[c0bff5e] | 260 | context, |
---|
[8f0c7a46] | 261 | node, |
---|
| 262 | _Scheduler_SMP_Insert_priority_lifo_order, |
---|
[c0bff5e] | 263 | _Scheduler_simple_SMP_Insert_ready_lifo, |
---|
| 264 | _Scheduler_SMP_Insert_scheduled_lifo |
---|
| 265 | ); |
---|
| 266 | } |
---|
| 267 | |
---|
[8568341] | 268 | static Thread_Control *_Scheduler_simple_SMP_Enqueue_scheduled_fifo( |
---|
[c0bff5e] | 269 | Scheduler_Context *context, |
---|
[8f0c7a46] | 270 | Scheduler_Node *node |
---|
[c0bff5e] | 271 | ) |
---|
| 272 | { |
---|
[8568341] | 273 | return _Scheduler_simple_SMP_Enqueue_scheduled_ordered( |
---|
[c0bff5e] | 274 | context, |
---|
[8f0c7a46] | 275 | node, |
---|
| 276 | _Scheduler_SMP_Insert_priority_fifo_order, |
---|
[e9ee2f0] | 277 | _Scheduler_simple_SMP_Insert_ready_fifo, |
---|
[48c4a55] | 278 | _Scheduler_SMP_Insert_scheduled_fifo |
---|
[a936aa49] | 279 | ); |
---|
| 280 | } |
---|
| 281 | |
---|
[8568341] | 282 | Thread_Control *_Scheduler_simple_SMP_Unblock( |
---|
[e1598a6] | 283 | const Scheduler_Control *scheduler, |
---|
[72e0bdb] | 284 | Thread_Control *thread, |
---|
| 285 | Scheduler_Node *node |
---|
[24934e36] | 286 | ) |
---|
[a936aa49] | 287 | { |
---|
[f39f667a] | 288 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
| 289 | |
---|
[8568341] | 290 | return _Scheduler_SMP_Unblock( |
---|
[c0bff5e] | 291 | context, |
---|
| 292 | thread, |
---|
[72e0bdb] | 293 | node, |
---|
[9bfad8c] | 294 | _Scheduler_simple_SMP_Do_update, |
---|
[27783f6] | 295 | _Scheduler_simple_SMP_Enqueue_fifo |
---|
[c0bff5e] | 296 | ); |
---|
[f39f667a] | 297 | } |
---|
[a936aa49] | 298 | |
---|
[351c14d] | 299 | static bool _Scheduler_simple_SMP_Do_ask_for_help( |
---|
| 300 | Scheduler_Context *context, |
---|
| 301 | Thread_Control *the_thread, |
---|
| 302 | Scheduler_Node *node |
---|
| 303 | ) |
---|
| 304 | { |
---|
| 305 | return _Scheduler_SMP_Ask_for_help( |
---|
| 306 | context, |
---|
| 307 | the_thread, |
---|
| 308 | node, |
---|
| 309 | _Scheduler_SMP_Insert_priority_lifo_order, |
---|
| 310 | _Scheduler_simple_SMP_Insert_ready_lifo, |
---|
| 311 | _Scheduler_SMP_Insert_scheduled_lifo, |
---|
| 312 | _Scheduler_simple_SMP_Move_from_scheduled_to_ready, |
---|
| 313 | _Scheduler_SMP_Get_lowest_scheduled, |
---|
| 314 | _Scheduler_SMP_Allocate_processor_lazy |
---|
| 315 | ); |
---|
| 316 | } |
---|
| 317 | |
---|
[9c238e1] | 318 | void _Scheduler_simple_SMP_Update_priority( |
---|
[f39f667a] | 319 | const Scheduler_Control *scheduler, |
---|
[501043a] | 320 | Thread_Control *thread, |
---|
| 321 | Scheduler_Node *node |
---|
[f39f667a] | 322 | ) |
---|
| 323 | { |
---|
| 324 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
| 325 | |
---|
[9c238e1] | 326 | _Scheduler_SMP_Update_priority( |
---|
[f39f667a] | 327 | context, |
---|
[48c4a55] | 328 | thread, |
---|
[501043a] | 329 | node, |
---|
[f39f667a] | 330 | _Scheduler_simple_SMP_Extract_from_ready, |
---|
| 331 | _Scheduler_simple_SMP_Do_update, |
---|
| 332 | _Scheduler_simple_SMP_Enqueue_fifo, |
---|
[c0bff5e] | 333 | _Scheduler_simple_SMP_Enqueue_lifo, |
---|
| 334 | _Scheduler_simple_SMP_Enqueue_scheduled_fifo, |
---|
[351c14d] | 335 | _Scheduler_simple_SMP_Enqueue_scheduled_lifo, |
---|
| 336 | _Scheduler_simple_SMP_Do_ask_for_help |
---|
| 337 | ); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | bool _Scheduler_simple_SMP_Ask_for_help( |
---|
| 341 | const Scheduler_Control *scheduler, |
---|
| 342 | Thread_Control *the_thread, |
---|
| 343 | Scheduler_Node *node |
---|
| 344 | ) |
---|
| 345 | { |
---|
| 346 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
| 347 | |
---|
| 348 | return _Scheduler_simple_SMP_Do_ask_for_help( context, the_thread, node ); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | void _Scheduler_simple_SMP_Reconsider_help_request( |
---|
| 352 | const Scheduler_Control *scheduler, |
---|
| 353 | Thread_Control *the_thread, |
---|
| 354 | Scheduler_Node *node |
---|
| 355 | ) |
---|
| 356 | { |
---|
| 357 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
| 358 | |
---|
| 359 | _Scheduler_SMP_Reconsider_help_request( |
---|
| 360 | context, |
---|
| 361 | the_thread, |
---|
| 362 | node, |
---|
| 363 | _Scheduler_simple_SMP_Extract_from_ready |
---|
| 364 | ); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | void _Scheduler_simple_SMP_Withdraw_node( |
---|
| 368 | const Scheduler_Control *scheduler, |
---|
| 369 | Thread_Control *the_thread, |
---|
| 370 | Scheduler_Node *node, |
---|
| 371 | Thread_Scheduler_state next_state |
---|
| 372 | ) |
---|
| 373 | { |
---|
| 374 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
| 375 | |
---|
| 376 | _Scheduler_SMP_Withdraw_node( |
---|
| 377 | context, |
---|
| 378 | the_thread, |
---|
| 379 | node, |
---|
| 380 | next_state, |
---|
| 381 | _Scheduler_simple_SMP_Extract_from_ready, |
---|
| 382 | _Scheduler_simple_SMP_Get_highest_ready, |
---|
| 383 | _Scheduler_simple_SMP_Move_from_ready_to_scheduled, |
---|
| 384 | _Scheduler_SMP_Allocate_processor_lazy |
---|
[48c4a55] | 385 | ); |
---|
[a936aa49] | 386 | } |
---|
| 387 | |
---|
[8568341] | 388 | Thread_Control *_Scheduler_simple_SMP_Yield( |
---|
[e1598a6] | 389 | const Scheduler_Control *scheduler, |
---|
[2df4abc] | 390 | Thread_Control *thread, |
---|
| 391 | Scheduler_Node *node |
---|
[24934e36] | 392 | ) |
---|
[a936aa49] | 393 | { |
---|
[f39f667a] | 394 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
[a936aa49] | 395 | |
---|
[701dd96f] | 396 | return _Scheduler_SMP_Yield( |
---|
| 397 | context, |
---|
| 398 | thread, |
---|
[2df4abc] | 399 | node, |
---|
[701dd96f] | 400 | _Scheduler_simple_SMP_Extract_from_ready, |
---|
| 401 | _Scheduler_simple_SMP_Enqueue_fifo, |
---|
| 402 | _Scheduler_simple_SMP_Enqueue_scheduled_fifo |
---|
| 403 | ); |
---|
[a936aa49] | 404 | } |
---|