[1f0d013] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Inlined Routines Associated with the Manipulation of the Scheduler |
---|
[0faa9dad] | 5 | * |
---|
[1f0d013] | 6 | * This inline file contains all of the inlined routines associated with |
---|
| 7 | * the manipulation of the scheduler. |
---|
[0faa9dad] | 8 | */ |
---|
| 9 | |
---|
| 10 | /* |
---|
| 11 | * Copyright (C) 2010 Gedare Bloom. |
---|
[010192d] | 12 | * Copyright (C) 2011 On-Line Applications Research Corporation (OAR). |
---|
[0faa9dad] | 13 | * |
---|
| 14 | * The license and distribution terms for this file may be |
---|
| 15 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 16 | * http://www.rtems.org/license/LICENSE. |
---|
[0faa9dad] | 17 | */ |
---|
| 18 | |
---|
[c6e21ee1] | 19 | #ifndef _RTEMS_SCORE_SCHEDULERIMPL_H |
---|
| 20 | #define _RTEMS_SCORE_SCHEDULERIMPL_H |
---|
| 21 | |
---|
| 22 | #include <rtems/score/scheduler.h> |
---|
[0712d17] | 23 | #include <rtems/score/cpusetimpl.h> |
---|
[c5831a3f] | 24 | #include <rtems/score/smpimpl.h> |
---|
[e5ca54c9] | 25 | #include <rtems/score/threadimpl.h> |
---|
[0faa9dad] | 26 | |
---|
[c6e21ee1] | 27 | #ifdef __cplusplus |
---|
| 28 | extern "C" { |
---|
| 29 | #endif |
---|
[0faa9dad] | 30 | |
---|
| 31 | /** |
---|
[1f0d013] | 32 | * @addtogroup ScoreScheduler |
---|
[0faa9dad] | 33 | */ |
---|
[b697bc6] | 34 | /**@{**/ |
---|
[0faa9dad] | 35 | |
---|
[c6e21ee1] | 36 | /** |
---|
| 37 | * @brief Initializes the scheduler to the policy chosen by the user. |
---|
| 38 | * |
---|
| 39 | * This routine initializes the scheduler to the policy chosen by the user |
---|
| 40 | * through confdefs, or to the priority scheduler with ready chains by |
---|
| 41 | * default. |
---|
| 42 | */ |
---|
| 43 | void _Scheduler_Handler_initialization( void ); |
---|
| 44 | |
---|
[c5831a3f] | 45 | RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_CPU_index( |
---|
| 46 | uint32_t cpu_index |
---|
| 47 | ) |
---|
| 48 | { |
---|
| 49 | #if defined(RTEMS_SMP) |
---|
| 50 | return _Scheduler_Assignments[ cpu_index ].scheduler; |
---|
| 51 | #else |
---|
| 52 | (void) cpu_index; |
---|
| 53 | |
---|
| 54 | return &_Scheduler_Table[ 0 ]; |
---|
| 55 | #endif |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_CPU( |
---|
| 59 | const Per_CPU_Control *cpu |
---|
| 60 | ) |
---|
| 61 | { |
---|
| 62 | uint32_t cpu_index = _Per_CPU_Get_index( cpu ); |
---|
| 63 | |
---|
| 64 | return _Scheduler_Get_by_CPU_index( cpu_index ); |
---|
| 65 | } |
---|
| 66 | |
---|
[0faa9dad] | 67 | /** |
---|
[1f0d013] | 68 | * The preferred method to add a new scheduler is to define the jump table |
---|
| 69 | * entries and add a case to the _Scheduler_Initialize routine. |
---|
[0faa9dad] | 70 | * |
---|
[1f0d013] | 71 | * Generic scheduling implementations that rely on the ready queue only can |
---|
[0faa9dad] | 72 | * be found in the _Scheduler_queue_XXX functions. |
---|
| 73 | */ |
---|
| 74 | |
---|
[1f0d013] | 75 | /* |
---|
| 76 | * Passing the Scheduler_Control* to these functions allows for multiple |
---|
| 77 | * scheduler's to exist simultaneously, which could be useful on an SMP |
---|
| 78 | * system. Then remote Schedulers may be accessible. How to protect such |
---|
[0faa9dad] | 79 | * accesses remains an open problem. |
---|
| 80 | */ |
---|
| 81 | |
---|
[1f0d013] | 82 | /** |
---|
| 83 | * @brief Scheduler schedule. |
---|
[0faa9dad] | 84 | * |
---|
[1f0d013] | 85 | * This kernel routine implements the scheduling decision logic for |
---|
| 86 | * the scheduler. It does NOT dispatch. |
---|
[e5ca54c9] | 87 | * |
---|
[24934e36] | 88 | * @param[in] the_thread The thread which state changed previously. |
---|
[0faa9dad] | 89 | */ |
---|
[24934e36] | 90 | RTEMS_INLINE_ROUTINE void _Scheduler_Schedule( |
---|
[e1598a6] | 91 | const Scheduler_Control *scheduler, |
---|
| 92 | Thread_Control *the_thread |
---|
[24934e36] | 93 | ) |
---|
[0faa9dad] | 94 | { |
---|
[24934e36] | 95 | ( *scheduler->Operations.schedule )( scheduler, the_thread ); |
---|
[0faa9dad] | 96 | } |
---|
| 97 | |
---|
[1f0d013] | 98 | /** |
---|
[6eba7c85] | 99 | * @brief Scheduler yield with a particular thread. |
---|
[0faa9dad] | 100 | * |
---|
[6eba7c85] | 101 | * This routine is invoked when a thread wishes to voluntarily transfer control |
---|
| 102 | * of the processor to another thread. |
---|
| 103 | * |
---|
[24934e36] | 104 | * @param[in] the_thread The yielding thread. |
---|
[0faa9dad] | 105 | */ |
---|
[6eba7c85] | 106 | RTEMS_INLINE_ROUTINE void _Scheduler_Yield( |
---|
[e1598a6] | 107 | const Scheduler_Control *scheduler, |
---|
| 108 | Thread_Control *the_thread |
---|
[6eba7c85] | 109 | ) |
---|
[0faa9dad] | 110 | { |
---|
[24934e36] | 111 | ( *scheduler->Operations.yield )( scheduler, the_thread ); |
---|
[0faa9dad] | 112 | } |
---|
| 113 | |
---|
[1f0d013] | 114 | /** |
---|
| 115 | * @brief Scheduler block. |
---|
[0faa9dad] | 116 | * |
---|
[1f0d013] | 117 | * This routine removes @a the_thread from the scheduling decision for |
---|
| 118 | * the scheduler. The primary task is to remove the thread from the |
---|
| 119 | * ready queue. It performs any necessary schedulering operations |
---|
| 120 | * including the selection of a new heir thread. |
---|
[0faa9dad] | 121 | */ |
---|
[1f0d013] | 122 | RTEMS_INLINE_ROUTINE void _Scheduler_Block( |
---|
[e1598a6] | 123 | const Scheduler_Control *scheduler, |
---|
| 124 | Thread_Control *the_thread |
---|
[0faa9dad] | 125 | ) |
---|
| 126 | { |
---|
[24934e36] | 127 | ( *scheduler->Operations.block )( scheduler, the_thread ); |
---|
[0faa9dad] | 128 | } |
---|
| 129 | |
---|
[1f0d013] | 130 | /** |
---|
| 131 | * @brief Scheduler unblock. |
---|
[0faa9dad] | 132 | * |
---|
[1f0d013] | 133 | * This routine adds @a the_thread to the scheduling decision for |
---|
| 134 | * the scheduler. The primary task is to add the thread to the |
---|
| 135 | * ready queue per the schedulering policy and update any appropriate |
---|
| 136 | * scheduling variables, for example the heir thread. |
---|
[0faa9dad] | 137 | */ |
---|
| 138 | RTEMS_INLINE_ROUTINE void _Scheduler_Unblock( |
---|
[e1598a6] | 139 | const Scheduler_Control *scheduler, |
---|
| 140 | Thread_Control *the_thread |
---|
[0faa9dad] | 141 | ) |
---|
| 142 | { |
---|
[24934e36] | 143 | ( *scheduler->Operations.unblock )( scheduler, the_thread ); |
---|
[0faa9dad] | 144 | } |
---|
| 145 | |
---|
[1f0d013] | 146 | /** |
---|
| 147 | * @brief Scheduler allocate. |
---|
[0faa9dad] | 148 | * |
---|
| 149 | * This routine allocates @a the_thread->scheduler |
---|
| 150 | */ |
---|
[69aa3349] | 151 | RTEMS_INLINE_ROUTINE bool _Scheduler_Allocate( |
---|
[e1598a6] | 152 | const Scheduler_Control *scheduler, |
---|
| 153 | Thread_Control *the_thread |
---|
[0faa9dad] | 154 | ) |
---|
| 155 | { |
---|
[24934e36] | 156 | return ( *scheduler->Operations.allocate )( scheduler, the_thread ); |
---|
[0faa9dad] | 157 | } |
---|
| 158 | |
---|
[1f0d013] | 159 | /** |
---|
| 160 | * @brief Scheduler free. |
---|
[0faa9dad] | 161 | * |
---|
| 162 | * This routine frees @a the_thread->scheduler |
---|
| 163 | */ |
---|
[1f0d013] | 164 | RTEMS_INLINE_ROUTINE void _Scheduler_Free( |
---|
[e1598a6] | 165 | const Scheduler_Control *scheduler, |
---|
| 166 | Thread_Control *the_thread |
---|
[0faa9dad] | 167 | ) |
---|
| 168 | { |
---|
[24934e36] | 169 | ( *scheduler->Operations.free )( scheduler, the_thread ); |
---|
[0faa9dad] | 170 | } |
---|
| 171 | |
---|
[1f0d013] | 172 | /** |
---|
| 173 | * @brief Scheduler update. |
---|
[0faa9dad] | 174 | * |
---|
| 175 | * This routine updates @a the_thread->scheduler |
---|
| 176 | */ |
---|
[1f0d013] | 177 | RTEMS_INLINE_ROUTINE void _Scheduler_Update( |
---|
[e1598a6] | 178 | const Scheduler_Control *scheduler, |
---|
| 179 | Thread_Control *the_thread |
---|
[0faa9dad] | 180 | ) |
---|
| 181 | { |
---|
[24934e36] | 182 | ( *scheduler->Operations.update )( scheduler, the_thread ); |
---|
[108c4b0] | 183 | } |
---|
| 184 | |
---|
[1f0d013] | 185 | /** |
---|
[5b1ff71a] | 186 | * @brief Enqueues a thread as the last of its priority group. |
---|
[108c4b0] | 187 | * |
---|
[5b1ff71a] | 188 | * @param[in] scheduler The scheduler instance. |
---|
| 189 | * @param[in] the_thread The thread to enqueue. |
---|
[108c4b0] | 190 | */ |
---|
[1f0d013] | 191 | RTEMS_INLINE_ROUTINE void _Scheduler_Enqueue( |
---|
[e1598a6] | 192 | const Scheduler_Control *scheduler, |
---|
| 193 | Thread_Control *the_thread |
---|
[108c4b0] | 194 | ) |
---|
| 195 | { |
---|
[24934e36] | 196 | ( *scheduler->Operations.enqueue )( scheduler, the_thread ); |
---|
[108c4b0] | 197 | } |
---|
| 198 | |
---|
[1f0d013] | 199 | /** |
---|
[5b1ff71a] | 200 | * @brief Enqueues a thread as the first of its priority group. |
---|
[108c4b0] | 201 | * |
---|
[5b1ff71a] | 202 | * @param[in] scheduler The scheduler instance. |
---|
| 203 | * @param[in] the_thread The thread to enqueue. |
---|
[108c4b0] | 204 | */ |
---|
[1f0d013] | 205 | RTEMS_INLINE_ROUTINE void _Scheduler_Enqueue_first( |
---|
[e1598a6] | 206 | const Scheduler_Control *scheduler, |
---|
| 207 | Thread_Control *the_thread |
---|
[108c4b0] | 208 | ) |
---|
| 209 | { |
---|
[24934e36] | 210 | ( *scheduler->Operations.enqueue_first )( scheduler, the_thread ); |
---|
[108c4b0] | 211 | } |
---|
| 212 | |
---|
[1f0d013] | 213 | /** |
---|
| 214 | * @brief Scheduler extract. |
---|
[108c4b0] | 215 | * |
---|
| 216 | * This routine extract @a the_thread->scheduler |
---|
| 217 | */ |
---|
[1f0d013] | 218 | RTEMS_INLINE_ROUTINE void _Scheduler_Extract( |
---|
[e1598a6] | 219 | const Scheduler_Control *scheduler, |
---|
| 220 | Thread_Control *the_thread |
---|
[108c4b0] | 221 | ) |
---|
| 222 | { |
---|
[24934e36] | 223 | ( *scheduler->Operations.extract )( scheduler, the_thread ); |
---|
[0faa9dad] | 224 | } |
---|
| 225 | |
---|
[ac9d2ecc] | 226 | /** |
---|
[5b1ff71a] | 227 | * @brief Compares two priority values. |
---|
[ac9d2ecc] | 228 | * |
---|
[5b1ff71a] | 229 | * @param[in] scheduler The scheduler instance. |
---|
| 230 | * @param[in] p1 The first priority value. |
---|
| 231 | * @param[in] p2 The second priority value. |
---|
| 232 | * |
---|
| 233 | * @retval negative The value @a p1 encodes a lower priority than @a p2 in the |
---|
| 234 | * intuitive sense of priority. |
---|
| 235 | * @retval 0 The priorities @a p1 and @a p2 are equal. |
---|
| 236 | * @retval positive The value @a p1 encodes a higher priority than @a p2 in the |
---|
| 237 | * intuitive sense of priority. |
---|
| 238 | * |
---|
| 239 | * @see _Scheduler_Is_priority_lower_than() and |
---|
| 240 | * _Scheduler_Is_priority_higher_than(). |
---|
[ac9d2ecc] | 241 | */ |
---|
| 242 | RTEMS_INLINE_ROUTINE int _Scheduler_Priority_compare( |
---|
[e1598a6] | 243 | const Scheduler_Control *scheduler, |
---|
| 244 | Priority_Control p1, |
---|
| 245 | Priority_Control p2 |
---|
[ac9d2ecc] | 246 | ) |
---|
| 247 | { |
---|
[24934e36] | 248 | return ( *scheduler->Operations.priority_compare )( p1, p2 ); |
---|
[ac9d2ecc] | 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
[1f0d013] | 252 | * @brief Scheduler release job. |
---|
[ac9d2ecc] | 253 | * |
---|
| 254 | * This routine is called when a new period of task is issued. |
---|
| 255 | */ |
---|
| 256 | RTEMS_INLINE_ROUTINE void _Scheduler_Release_job( |
---|
[e1598a6] | 257 | const Scheduler_Control *scheduler, |
---|
| 258 | Thread_Control *the_thread, |
---|
| 259 | uint32_t length |
---|
[ac9d2ecc] | 260 | ) |
---|
| 261 | { |
---|
[24934e36] | 262 | ( *scheduler->Operations.release_job )( scheduler, the_thread, length ); |
---|
[ac9d2ecc] | 263 | } |
---|
| 264 | |
---|
[1f0d013] | 265 | /** |
---|
| 266 | * @brief Scheduler method invoked at each clock tick. |
---|
[3203e09] | 267 | * |
---|
| 268 | * This method is invoked at each clock tick to allow the scheduler |
---|
[1f0d013] | 269 | * implementation to perform any activities required. For the |
---|
[3203e09] | 270 | * scheduler which support standard RTEMS features, this includes |
---|
| 271 | * time-slicing management. |
---|
| 272 | */ |
---|
[c5831a3f] | 273 | RTEMS_INLINE_ROUTINE void _Scheduler_Tick( void ) |
---|
[3203e09] | 274 | { |
---|
[c5831a3f] | 275 | uint32_t cpu_count = _SMP_Get_processor_count(); |
---|
| 276 | uint32_t cpu_index; |
---|
| 277 | |
---|
| 278 | for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) { |
---|
| 279 | const Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index ); |
---|
| 280 | const Scheduler_Control *scheduler = _Scheduler_Get_by_CPU( cpu ); |
---|
| 281 | |
---|
| 282 | if ( scheduler != NULL ) { |
---|
| 283 | ( *scheduler->Operations.tick )( scheduler, cpu->executing ); |
---|
| 284 | } |
---|
| 285 | } |
---|
[3203e09] | 286 | } |
---|
| 287 | |
---|
[1ccb64e1] | 288 | /** |
---|
| 289 | * @brief Starts the idle thread for a particular processor. |
---|
| 290 | * |
---|
[24934e36] | 291 | * @param[in,out] the_thread The idle thread for the processor. |
---|
[961669d] | 292 | * @parma[in,out] processor The processor for the idle thread. |
---|
[1ccb64e1] | 293 | * |
---|
| 294 | * @see _Thread_Create_idle(). |
---|
| 295 | */ |
---|
| 296 | RTEMS_INLINE_ROUTINE void _Scheduler_Start_idle( |
---|
[e1598a6] | 297 | const Scheduler_Control *scheduler, |
---|
| 298 | Thread_Control *the_thread, |
---|
| 299 | Per_CPU_Control *cpu |
---|
[1ccb64e1] | 300 | ) |
---|
| 301 | { |
---|
[24934e36] | 302 | ( *scheduler->Operations.start_idle )( scheduler, the_thread, cpu ); |
---|
[1ccb64e1] | 303 | } |
---|
| 304 | |
---|
[c5831a3f] | 305 | #if defined(RTEMS_SMP) |
---|
| 306 | RTEMS_INLINE_ROUTINE const Scheduler_Assignment *_Scheduler_Get_assignment( |
---|
| 307 | uint32_t cpu_index |
---|
| 308 | ) |
---|
| 309 | { |
---|
| 310 | return &_Scheduler_Assignments[ cpu_index ]; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | RTEMS_INLINE_ROUTINE bool _Scheduler_Is_mandatory_processor( |
---|
| 314 | const Scheduler_Assignment *assignment |
---|
| 315 | ) |
---|
| 316 | { |
---|
| 317 | return (assignment->attributes & SCHEDULER_ASSIGN_PROCESSOR_MANDATORY) != 0; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | RTEMS_INLINE_ROUTINE bool _Scheduler_Should_start_processor( |
---|
| 321 | const Scheduler_Assignment *assignment |
---|
| 322 | ) |
---|
| 323 | { |
---|
| 324 | return assignment->scheduler != NULL; |
---|
| 325 | } |
---|
| 326 | #endif /* defined(RTEMS_SMP) */ |
---|
| 327 | |
---|
| 328 | RTEMS_INLINE_ROUTINE bool _Scheduler_Has_processor_ownership( |
---|
| 329 | const Scheduler_Control *scheduler, |
---|
| 330 | uint32_t cpu_index |
---|
| 331 | ) |
---|
| 332 | { |
---|
| 333 | #if defined(RTEMS_SMP) |
---|
| 334 | const Scheduler_Assignment *assignment = |
---|
| 335 | _Scheduler_Get_assignment( cpu_index ); |
---|
| 336 | |
---|
| 337 | return assignment->scheduler == scheduler; |
---|
| 338 | #else |
---|
| 339 | (void) scheduler; |
---|
| 340 | (void) cpu_index; |
---|
| 341 | |
---|
| 342 | return true; |
---|
| 343 | #endif |
---|
| 344 | } |
---|
| 345 | |
---|
[390e73c4] | 346 | RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get( |
---|
| 347 | Thread_Control *the_thread |
---|
| 348 | ) |
---|
| 349 | { |
---|
| 350 | #if defined(RTEMS_SMP) |
---|
| 351 | return the_thread->scheduler; |
---|
| 352 | #else |
---|
| 353 | (void) the_thread; |
---|
| 354 | |
---|
| 355 | return &_Scheduler_Table[ 0 ]; |
---|
| 356 | #endif |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | RTEMS_INLINE_ROUTINE void _Scheduler_Set( |
---|
| 360 | const Scheduler_Control *scheduler, |
---|
| 361 | Thread_Control *the_thread |
---|
| 362 | ) |
---|
| 363 | { |
---|
| 364 | #if defined(RTEMS_SMP) |
---|
| 365 | const Scheduler_Control *current_scheduler = _Scheduler_Get( the_thread ); |
---|
| 366 | |
---|
| 367 | if ( current_scheduler != scheduler ) { |
---|
| 368 | _Thread_Set_state( the_thread, STATES_MIGRATING ); |
---|
| 369 | _Scheduler_Free( _Scheduler_Get( the_thread ), the_thread ); |
---|
| 370 | the_thread->scheduler = scheduler; |
---|
| 371 | _Scheduler_Allocate( scheduler, the_thread ); |
---|
| 372 | _Scheduler_Update( scheduler, the_thread ); |
---|
| 373 | _Thread_Clear_state( the_thread, STATES_MIGRATING ); |
---|
| 374 | } |
---|
| 375 | #else |
---|
| 376 | (void) scheduler; |
---|
| 377 | #endif |
---|
| 378 | } |
---|
| 379 | |
---|
[0712d17] | 380 | #if defined(__RTEMS_HAVE_SYS_CPUSET_H__) |
---|
| 381 | |
---|
| 382 | RTEMS_INLINE_ROUTINE void _Scheduler_Get_processor_set( |
---|
| 383 | const Scheduler_Control *scheduler, |
---|
| 384 | size_t cpusetsize, |
---|
| 385 | cpu_set_t *cpuset |
---|
| 386 | ) |
---|
| 387 | { |
---|
| 388 | uint32_t cpu_count = _SMP_Get_processor_count(); |
---|
| 389 | uint32_t cpu_index; |
---|
| 390 | |
---|
| 391 | CPU_ZERO_S( cpusetsize, cpuset ); |
---|
| 392 | |
---|
| 393 | for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) { |
---|
[c5831a3f] | 394 | #if defined(RTEMS_SMP) |
---|
| 395 | if ( _Scheduler_Has_processor_ownership( scheduler, cpu_index ) ) { |
---|
| 396 | CPU_SET_S( (int) cpu_index, cpusetsize, cpuset ); |
---|
| 397 | } |
---|
| 398 | #else |
---|
| 399 | (void) scheduler; |
---|
| 400 | |
---|
[0712d17] | 401 | CPU_SET_S( (int) cpu_index, cpusetsize, cpuset ); |
---|
[c5831a3f] | 402 | #endif |
---|
[0712d17] | 403 | } |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | RTEMS_INLINE_ROUTINE bool _Scheduler_default_Get_affinity_body( |
---|
| 407 | const Scheduler_Control *scheduler, |
---|
| 408 | Thread_Control *the_thread, |
---|
| 409 | size_t cpusetsize, |
---|
| 410 | cpu_set_t *cpuset |
---|
| 411 | ) |
---|
| 412 | { |
---|
| 413 | (void) the_thread; |
---|
| 414 | |
---|
| 415 | _Scheduler_Get_processor_set( scheduler, cpusetsize, cpuset ); |
---|
| 416 | |
---|
| 417 | return true; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | bool _Scheduler_Get_affinity( |
---|
| 421 | const Scheduler_Control *scheduler, |
---|
| 422 | Thread_Control *the_thread, |
---|
| 423 | size_t cpusetsize, |
---|
| 424 | cpu_set_t *cpuset |
---|
| 425 | ); |
---|
| 426 | |
---|
| 427 | RTEMS_INLINE_ROUTINE bool _Scheduler_default_Set_affinity_body( |
---|
| 428 | const Scheduler_Control *scheduler, |
---|
| 429 | Thread_Control *the_thread, |
---|
| 430 | size_t cpusetsize, |
---|
| 431 | const cpu_set_t *cpuset |
---|
| 432 | ) |
---|
| 433 | { |
---|
| 434 | size_t cpu_max = _CPU_set_Maximum_CPU_count( cpusetsize ); |
---|
| 435 | uint32_t cpu_count = _SMP_Get_processor_count(); |
---|
| 436 | uint32_t cpu_index; |
---|
| 437 | bool ok = true; |
---|
| 438 | |
---|
| 439 | for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) { |
---|
[c5831a3f] | 440 | #if defined(RTEMS_SMP) |
---|
| 441 | const Scheduler_Control *scheduler_of_cpu = |
---|
| 442 | _Scheduler_Get_by_CPU_index( cpu_index ); |
---|
| 443 | |
---|
| 444 | ok = ok |
---|
| 445 | && ( ( CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset ) |
---|
| 446 | && scheduler == scheduler_of_cpu ) |
---|
| 447 | || ( !CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset ) |
---|
| 448 | && scheduler != scheduler_of_cpu ) ); |
---|
| 449 | #else |
---|
| 450 | (void) scheduler; |
---|
| 451 | |
---|
[0712d17] | 452 | ok = ok && CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset ); |
---|
[c5831a3f] | 453 | #endif |
---|
[cfe457f] | 454 | } |
---|
| 455 | |
---|
[0712d17] | 456 | for ( ; cpu_index < cpu_max ; ++cpu_index ) { |
---|
| 457 | ok = ok && !CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset ); |
---|
[cfe457f] | 458 | } |
---|
[0712d17] | 459 | |
---|
[38b59a6] | 460 | _Scheduler_Set( scheduler, the_thread ); |
---|
[c5831a3f] | 461 | |
---|
[0712d17] | 462 | return ok; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | bool _Scheduler_Set_affinity( |
---|
| 466 | Thread_Control *the_thread, |
---|
| 467 | size_t cpusetsize, |
---|
| 468 | const cpu_set_t *cpuset |
---|
| 469 | ); |
---|
| 470 | |
---|
| 471 | #endif /* defined(__RTEMS_HAVE_SYS_CPUSET_H__) */ |
---|
[cfe457f] | 472 | |
---|
[e5ca54c9] | 473 | RTEMS_INLINE_ROUTINE void _Scheduler_Update_heir( |
---|
| 474 | Thread_Control *heir, |
---|
| 475 | bool force_dispatch |
---|
| 476 | ) |
---|
| 477 | { |
---|
| 478 | Thread_Control *executing = _Thread_Executing; |
---|
| 479 | |
---|
| 480 | _Thread_Heir = heir; |
---|
| 481 | |
---|
| 482 | if ( executing != heir && ( force_dispatch || executing->is_preemptible ) ) |
---|
| 483 | _Thread_Dispatch_necessary = true; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | RTEMS_INLINE_ROUTINE void _Scheduler_Generic_block( |
---|
[e1598a6] | 487 | const Scheduler_Control *scheduler, |
---|
| 488 | Thread_Control *the_thread, |
---|
| 489 | void ( *extract )( |
---|
| 490 | const Scheduler_Control *, |
---|
| 491 | Thread_Control * ), |
---|
| 492 | void ( *schedule )( |
---|
| 493 | const Scheduler_Control *, |
---|
| 494 | Thread_Control *, |
---|
| 495 | bool ) |
---|
[e5ca54c9] | 496 | ) |
---|
| 497 | { |
---|
[24934e36] | 498 | ( *extract )( scheduler, the_thread ); |
---|
[e5ca54c9] | 499 | |
---|
| 500 | /* TODO: flash critical section? */ |
---|
| 501 | |
---|
[24934e36] | 502 | if ( _Thread_Is_executing( the_thread ) || _Thread_Is_heir( the_thread ) ) { |
---|
| 503 | ( *schedule )( scheduler, the_thread, true ); |
---|
[e5ca54c9] | 504 | } |
---|
| 505 | } |
---|
| 506 | |
---|
[c6e21ee1] | 507 | /** |
---|
[5b1ff71a] | 508 | * @brief Returns true if @a p1 encodes a lower priority than @a p2 in the |
---|
[298d0fd] | 509 | * intuitive sense of priority. |
---|
[c6e21ee1] | 510 | */ |
---|
[298d0fd] | 511 | RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_lower_than( |
---|
[e1598a6] | 512 | const Scheduler_Control *scheduler, |
---|
| 513 | Priority_Control p1, |
---|
| 514 | Priority_Control p2 |
---|
[298d0fd] | 515 | ) |
---|
| 516 | { |
---|
[24934e36] | 517 | return _Scheduler_Priority_compare( scheduler, p1, p2 ) < 0; |
---|
[298d0fd] | 518 | } |
---|
[c6e21ee1] | 519 | |
---|
| 520 | /** |
---|
[5b1ff71a] | 521 | * @brief Returns true if @a p1 encodes a higher priority than @a p2 in the |
---|
[298d0fd] | 522 | * intuitive sense of priority. |
---|
| 523 | */ |
---|
| 524 | RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_higher_than( |
---|
[e1598a6] | 525 | const Scheduler_Control *scheduler, |
---|
| 526 | Priority_Control p1, |
---|
| 527 | Priority_Control p2 |
---|
[298d0fd] | 528 | ) |
---|
| 529 | { |
---|
[24934e36] | 530 | return _Scheduler_Priority_compare( scheduler, p1, p2 ) > 0; |
---|
[298d0fd] | 531 | } |
---|
| 532 | |
---|
| 533 | /** |
---|
| 534 | * @brief Returns the priority encoding @a p1 or @a p2 with the higher priority |
---|
[c6e21ee1] | 535 | * in the intuitive sense of priority. |
---|
| 536 | */ |
---|
[298d0fd] | 537 | RTEMS_INLINE_ROUTINE Priority_Control _Scheduler_Highest_priority_of_two( |
---|
[e1598a6] | 538 | const Scheduler_Control *scheduler, |
---|
| 539 | Priority_Control p1, |
---|
| 540 | Priority_Control p2 |
---|
[298d0fd] | 541 | ) |
---|
| 542 | { |
---|
[24934e36] | 543 | return _Scheduler_Is_priority_higher_than( scheduler, p1, p2 ) ? p1 : p2; |
---|
[298d0fd] | 544 | } |
---|
[c6e21ee1] | 545 | |
---|
[c2225d7] | 546 | /** |
---|
| 547 | * @brief Sets the thread priority to @a priority if it is higher than the |
---|
| 548 | * current priority of the thread in the intuitive sense of priority. |
---|
| 549 | */ |
---|
| 550 | RTEMS_INLINE_ROUTINE void _Scheduler_Set_priority_if_higher( |
---|
[e1598a6] | 551 | const Scheduler_Control *scheduler, |
---|
| 552 | Thread_Control *the_thread, |
---|
| 553 | Priority_Control priority |
---|
[c2225d7] | 554 | ) |
---|
| 555 | { |
---|
| 556 | Priority_Control current = the_thread->current_priority; |
---|
| 557 | |
---|
[24934e36] | 558 | if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) { |
---|
[c2225d7] | 559 | _Thread_Set_priority( the_thread, priority ); |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | /** |
---|
| 564 | * @brief Changes the thread priority to @a priority if it is higher than the |
---|
| 565 | * current priority of the thread in the intuitive sense of priority. |
---|
| 566 | */ |
---|
| 567 | RTEMS_INLINE_ROUTINE void _Scheduler_Change_priority_if_higher( |
---|
[e1598a6] | 568 | const Scheduler_Control *scheduler, |
---|
| 569 | Thread_Control *the_thread, |
---|
| 570 | Priority_Control priority, |
---|
| 571 | bool prepend_it |
---|
[c2225d7] | 572 | ) |
---|
| 573 | { |
---|
| 574 | Priority_Control current = the_thread->current_priority; |
---|
| 575 | |
---|
[24934e36] | 576 | if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) { |
---|
[c2225d7] | 577 | _Thread_Change_priority( the_thread, priority, prepend_it ); |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | |
---|
[e239760] | 581 | RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_processor_count( |
---|
| 582 | const Scheduler_Control *scheduler |
---|
| 583 | ) |
---|
| 584 | { |
---|
| 585 | #if defined(RTEMS_SMP) |
---|
| 586 | return scheduler->context->processor_count; |
---|
| 587 | #else |
---|
| 588 | (void) scheduler; |
---|
| 589 | |
---|
| 590 | return 1; |
---|
| 591 | #endif |
---|
| 592 | } |
---|
| 593 | |
---|
[b427a92] | 594 | RTEMS_INLINE_ROUTINE Objects_Id _Scheduler_Build_id( uint32_t scheduler_index ) |
---|
| 595 | { |
---|
| 596 | return _Objects_Build_id( |
---|
| 597 | OBJECTS_FAKE_OBJECTS_API, |
---|
| 598 | OBJECTS_FAKE_OBJECTS_SCHEDULERS, |
---|
| 599 | _Objects_Local_node, |
---|
| 600 | scheduler_index + 1 |
---|
| 601 | ); |
---|
| 602 | } |
---|
| 603 | |
---|
[1b67535d] | 604 | RTEMS_INLINE_ROUTINE bool _Scheduler_Get_by_id( |
---|
| 605 | Objects_Id id, |
---|
[e239760] | 606 | const Scheduler_Control **scheduler_p |
---|
[1b67535d] | 607 | ) |
---|
| 608 | { |
---|
| 609 | uint32_t minimum_id = _Scheduler_Build_id( 0 ); |
---|
| 610 | uint32_t index = id - minimum_id; |
---|
[e239760] | 611 | const Scheduler_Control *scheduler = &_Scheduler_Table[ index ]; |
---|
[1b67535d] | 612 | |
---|
[e239760] | 613 | *scheduler_p = scheduler; |
---|
[1b67535d] | 614 | |
---|
[e239760] | 615 | return index < _Scheduler_Count |
---|
| 616 | && _Scheduler_Get_processor_count( scheduler ) > 0; |
---|
[1b67535d] | 617 | } |
---|
| 618 | |
---|
[27270b0d] | 619 | RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index( |
---|
| 620 | const Scheduler_Control *scheduler |
---|
| 621 | ) |
---|
| 622 | { |
---|
| 623 | return (uint32_t) (scheduler - &_Scheduler_Table[ 0 ]); |
---|
| 624 | } |
---|
| 625 | |
---|
[beab7329] | 626 | RTEMS_INLINE_ROUTINE Scheduler_Node *_Scheduler_Node_get( |
---|
| 627 | Thread_Control *the_thread |
---|
| 628 | ) |
---|
| 629 | { |
---|
| 630 | return the_thread->scheduler_node; |
---|
| 631 | } |
---|
| 632 | |
---|
[1f0d013] | 633 | /** @} */ |
---|
[0faa9dad] | 634 | |
---|
[c6e21ee1] | 635 | #ifdef __cplusplus |
---|
| 636 | } |
---|
| 637 | #endif |
---|
| 638 | |
---|
[0faa9dad] | 639 | #endif |
---|
| 640 | /* end of include file */ |
---|