source: rtems/cpukit/score/include/rtems/score/schedulerimpl.h @ c597fb1

5
Last change on this file since c597fb1 was c597fb1, checked in by Sebastian Huber <sebastian.huber@…>, on 11/09/17 at 15:21:37

score: Optimize scheduler priority updates

Thread priority changes may append or prepend the thread to its priority
group on the scheduler ready queue. Previously, a separate priority
value and a prepend-it flag in the scheduler node were used to propagate
a priority change to the scheduler.

Now, use an append-it bit in the priority control and reduce the plain
priority value to 63 bits.

This change leads to a significant code size reduction (about 25%) of
the SMP schedulers. The negligible increase of the standard priority
scheduler is due to some additional shift operations
(SCHEDULER_PRIORITY_MAP() and SCHEDULER_PRIORITY_UNMAP()).

Before:

text filename

136 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleblock.o
464 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimplechangepriority.o

24 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimple.o

108 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleschedule.o
292 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleunblock.o
264 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleyield.o

text filename

280 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityblock.o
488 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerprioritychangepriority.o
200 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriority.o
164 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityschedule.o
328 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityunblock.o
200 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityyield.o

text filename

24112 arm-rtems5/c/imx7/cpukit/score/src/libscore_a-scheduleredfsmp.o

text filename

37204 sparc-rtems5/c/gr740/cpukit/score/src/libscore_a-scheduleredfsmp.o

text filename

42236 powerpc-rtems5/c/qoriq_e6500_32/cpukit/score/src/libscore_a-scheduleredfsmp.o

After:

text filename

136 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleblock.o
272 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimplechangepriority.o

24 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimple.o

108 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleschedule.o
292 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleunblock.o
264 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulersimpleyield.o

text filename

280 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityblock.o
488 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerprioritychangepriority.o
208 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriority.o
164 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityschedule.o
332 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityunblock.o
200 sparc-rtems5/c/erc32/cpukit/score/src/libscore_a-schedulerpriorityyield.o

text filename

18860 arm-rtems5/c/imx7/cpukit/score/src/libscore_a-scheduleredfsmp.o

text filename

28520 sparc-rtems5/c/gr740/cpukit/score/src/libscore_a-scheduleredfsmp.o

text filename

32664 powerpc-rtems5/c/qoriq_e6500_32/cpukit/score/src/libscore_a-scheduleredfsmp.o

  • Property mode set to 100644
File size: 33.6 KB
Line 
1/**
2 * @file
3 *
4 * @brief Inlined Routines Associated with the Manipulation of the Scheduler
5 *
6 * This inline file contains all of the inlined routines associated with
7 * the manipulation of the scheduler.
8 */
9
10/*
11 *  Copyright (C) 2010 Gedare Bloom.
12 *  Copyright (C) 2011 On-Line Applications Research Corporation (OAR).
13 *  Copyright (c) 2014, 2017 embedded brains GmbH
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.org/license/LICENSE.
18 */
19
20#ifndef _RTEMS_SCORE_SCHEDULERIMPL_H
21#define _RTEMS_SCORE_SCHEDULERIMPL_H
22
23#include <rtems/score/scheduler.h>
24#include <rtems/score/assert.h>
25#include <rtems/score/priorityimpl.h>
26#include <rtems/score/smpimpl.h>
27#include <rtems/score/status.h>
28#include <rtems/score/threadimpl.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/**
35 * @addtogroup ScoreScheduler
36 */
37/**@{**/
38
39/**
40 * @brief Maps a priority value to support the append indicator.
41 */
42#define SCHEDULER_PRIORITY_MAP( priority ) ( ( priority ) << 1 )
43
44/**
45 * @brief Returns the plain priority value.
46 */
47#define SCHEDULER_PRIORITY_UNMAP( priority ) ( ( priority ) >> 1 )
48
49/**
50 * @brief Clears the priority append indicator bit.
51 */
52#define SCHEDULER_PRIORITY_PURIFY( priority )  \
53  ( ( priority ) & ~( (Priority_Control) SCHEDULER_PRIORITY_APPEND_FLAG ) )
54
55/**
56 * @brief Returns the priority control with the append indicator bit set.
57 */
58#define SCHEDULER_PRIORITY_APPEND( priority )  \
59  ( ( priority ) | SCHEDULER_PRIORITY_APPEND_FLAG )
60
61/**
62 * @brief Returns true, if the item should be appended to its priority group,
63 * otherwise returns false and the item should be prepended to its priority
64 * group.
65 */
66#define SCHEDULER_PRIORITY_IS_APPEND( priority ) \
67  ( ( ( priority ) & SCHEDULER_PRIORITY_APPEND_FLAG ) != 0 )
68
69/**
70 *  @brief Initializes the scheduler to the policy chosen by the user.
71 *
72 *  This routine initializes the scheduler to the policy chosen by the user
73 *  through confdefs, or to the priority scheduler with ready chains by
74 *  default.
75 */
76void _Scheduler_Handler_initialization( void );
77
78RTEMS_INLINE_ROUTINE Scheduler_Context *_Scheduler_Get_context(
79  const Scheduler_Control *scheduler
80)
81{
82  return scheduler->context;
83}
84
85RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_CPU(
86  const Per_CPU_Control *cpu
87)
88{
89#if defined(RTEMS_SMP)
90  return cpu->Scheduler.control;
91#else
92  (void) cpu;
93  return &_Scheduler_Table[ 0 ];
94#endif
95}
96
97/**
98 * @brief Acquires the scheduler instance inside a critical section (interrupts
99 * disabled).
100 *
101 * @param[in] scheduler The scheduler instance.
102 * @param[in] lock_context The lock context to use for
103 *   _Scheduler_Release_critical().
104 */
105RTEMS_INLINE_ROUTINE void _Scheduler_Acquire_critical(
106  const Scheduler_Control *scheduler,
107  ISR_lock_Context        *lock_context
108)
109{
110#if defined(RTEMS_SMP)
111  Scheduler_Context *context;
112
113  context = _Scheduler_Get_context( scheduler );
114  _ISR_lock_Acquire( &context->Lock, lock_context );
115#else
116  (void) scheduler;
117  (void) lock_context;
118#endif
119}
120
121/**
122 * @brief Releases the scheduler instance inside a critical section (interrupts
123 * disabled).
124 *
125 * @param[in] scheduler The scheduler instance.
126 * @param[in] lock_context The lock context used for
127 *   _Scheduler_Acquire_critical().
128 */
129RTEMS_INLINE_ROUTINE void _Scheduler_Release_critical(
130  const Scheduler_Control *scheduler,
131  ISR_lock_Context        *lock_context
132)
133{
134#if defined(RTEMS_SMP)
135  Scheduler_Context *context;
136
137  context = _Scheduler_Get_context( scheduler );
138  _ISR_lock_Release( &context->Lock, lock_context );
139#else
140  (void) scheduler;
141  (void) lock_context;
142#endif
143}
144
145#if defined(RTEMS_SMP)
146void _Scheduler_Request_ask_for_help( Thread_Control *the_thread );
147
148/**
149 * @brief Registers an ask for help request if necessary.
150 *
151 * The actual ask for help operation is carried out during
152 * _Thread_Do_dispatch() on a processor related to the thread.  This yields a
153 * better separation of scheduler instances.  A thread of one scheduler
154 * instance should not be forced to carry out too much work for threads on
155 * other scheduler instances.
156 *
157 * @param[in] the_thread The thread in need for help.
158 */
159RTEMS_INLINE_ROUTINE void _Scheduler_Ask_for_help( Thread_Control *the_thread )
160{
161  _Assert( _Thread_State_is_owner( the_thread ) );
162
163  if ( the_thread->Scheduler.helping_nodes > 0 ) {
164    _Scheduler_Request_ask_for_help( the_thread );
165  }
166}
167#endif
168
169/**
170 * The preferred method to add a new scheduler is to define the jump table
171 * entries and add a case to the _Scheduler_Initialize routine.
172 *
173 * Generic scheduling implementations that rely on the ready queue only can
174 * be found in the _Scheduler_queue_XXX functions.
175 */
176
177/*
178 * Passing the Scheduler_Control* to these functions allows for multiple
179 * scheduler's to exist simultaneously, which could be useful on an SMP
180 * system.  Then remote Schedulers may be accessible.  How to protect such
181 * accesses remains an open problem.
182 */
183
184/**
185 * @brief General scheduling decision.
186 *
187 * This kernel routine implements the scheduling decision logic for
188 * the scheduler. It does NOT dispatch.
189 *
190 * @param[in] the_thread The thread which state changed previously.
191 */
192RTEMS_INLINE_ROUTINE void _Scheduler_Schedule( Thread_Control *the_thread )
193{
194  const Scheduler_Control *scheduler;
195  ISR_lock_Context         lock_context;
196
197  scheduler = _Thread_Scheduler_get_home( the_thread );
198  _Scheduler_Acquire_critical( scheduler, &lock_context );
199
200  ( *scheduler->Operations.schedule )( scheduler, the_thread );
201
202  _Scheduler_Release_critical( scheduler, &lock_context );
203}
204
205/**
206 * @brief Scheduler yield with a particular thread.
207 *
208 * This routine is invoked when a thread wishes to voluntarily transfer control
209 * of the processor to another thread.
210 *
211 * @param[in] the_thread The yielding thread.
212 */
213RTEMS_INLINE_ROUTINE void _Scheduler_Yield( Thread_Control *the_thread )
214{
215  const Scheduler_Control *scheduler;
216  ISR_lock_Context         lock_context;
217
218  scheduler = _Thread_Scheduler_get_home( the_thread );
219  _Scheduler_Acquire_critical( scheduler, &lock_context );
220  ( *scheduler->Operations.yield )(
221    scheduler,
222    the_thread,
223    _Thread_Scheduler_get_home_node( the_thread )
224  );
225  _Scheduler_Release_critical( scheduler, &lock_context );
226}
227
228/**
229 * @brief Blocks a thread with respect to the scheduler.
230 *
231 * This routine removes @a the_thread from the scheduling decision for
232 * the scheduler. The primary task is to remove the thread from the
233 * ready queue.  It performs any necessary schedulering operations
234 * including the selection of a new heir thread.
235 *
236 * @param[in] the_thread The thread.
237 */
238RTEMS_INLINE_ROUTINE void _Scheduler_Block( Thread_Control *the_thread )
239{
240#if defined(RTEMS_SMP)
241  Chain_Node              *node;
242  const Chain_Node        *tail;
243  Scheduler_Node          *scheduler_node;
244  const Scheduler_Control *scheduler;
245  ISR_lock_Context         lock_context;
246
247  node = _Chain_First( &the_thread->Scheduler.Scheduler_nodes );
248  tail = _Chain_Immutable_tail( &the_thread->Scheduler.Scheduler_nodes );
249
250  scheduler_node = SCHEDULER_NODE_OF_THREAD_SCHEDULER_NODE( node );
251  scheduler = _Scheduler_Node_get_scheduler( scheduler_node );
252
253  _Scheduler_Acquire_critical( scheduler, &lock_context );
254  ( *scheduler->Operations.block )(
255    scheduler,
256    the_thread,
257    scheduler_node
258  );
259  _Scheduler_Release_critical( scheduler, &lock_context );
260
261  node = _Chain_Next( node );
262
263  while ( node != tail ) {
264    scheduler_node = SCHEDULER_NODE_OF_THREAD_SCHEDULER_NODE( node );
265    scheduler = _Scheduler_Node_get_scheduler( scheduler_node );
266
267    _Scheduler_Acquire_critical( scheduler, &lock_context );
268    ( *scheduler->Operations.withdraw_node )(
269      scheduler,
270      the_thread,
271      scheduler_node,
272      THREAD_SCHEDULER_BLOCKED
273    );
274    _Scheduler_Release_critical( scheduler, &lock_context );
275
276    node = _Chain_Next( node );
277  }
278#else
279  const Scheduler_Control *scheduler;
280
281  scheduler = _Thread_Scheduler_get_home( the_thread );
282  ( *scheduler->Operations.block )(
283    scheduler,
284    the_thread,
285    _Thread_Scheduler_get_home_node( the_thread )
286  );
287#endif
288}
289
290/**
291 * @brief Unblocks a thread with respect to the scheduler.
292 *
293 * This operation must fetch the latest thread priority value for this
294 * scheduler instance and update its internal state if necessary.
295 *
296 * @param[in] the_thread The thread.
297 *
298 * @see _Scheduler_Node_get_priority().
299 */
300RTEMS_INLINE_ROUTINE void _Scheduler_Unblock( Thread_Control *the_thread )
301{
302  const Scheduler_Control *scheduler;
303  ISR_lock_Context         lock_context;
304
305  scheduler = _Thread_Scheduler_get_home( the_thread );
306  _Scheduler_Acquire_critical( scheduler, &lock_context );
307  ( *scheduler->Operations.unblock )(
308    scheduler,
309    the_thread,
310    _Thread_Scheduler_get_home_node( the_thread )
311  );
312  _Scheduler_Release_critical( scheduler, &lock_context );
313}
314
315/**
316 * @brief Propagates a priority change of a thread to the scheduler.
317 *
318 * On uni-processor configurations, this operation must evaluate the thread
319 * state.  In case the thread is not ready, then the priority update should be
320 * deferred to the next scheduler unblock operation.
321 *
322 * The operation must update the heir and thread dispatch necessary variables
323 * in case the set of scheduled threads changes.
324 *
325 * @param[in] the_thread The thread changing its priority.
326 *
327 * @see _Scheduler_Node_get_priority().
328 */
329RTEMS_INLINE_ROUTINE void _Scheduler_Update_priority( Thread_Control *the_thread )
330{
331#if defined(RTEMS_SMP)
332  Chain_Node       *node;
333  const Chain_Node *tail;
334
335  _Thread_Scheduler_process_requests( the_thread );
336
337  node = _Chain_First( &the_thread->Scheduler.Scheduler_nodes );
338  tail = _Chain_Immutable_tail( &the_thread->Scheduler.Scheduler_nodes );
339
340  do {
341    Scheduler_Node          *scheduler_node;
342    const Scheduler_Control *scheduler;
343    ISR_lock_Context         lock_context;
344
345    scheduler_node = SCHEDULER_NODE_OF_THREAD_SCHEDULER_NODE( node );
346    scheduler = _Scheduler_Node_get_scheduler( scheduler_node );
347
348    _Scheduler_Acquire_critical( scheduler, &lock_context );
349    ( *scheduler->Operations.update_priority )(
350      scheduler,
351      the_thread,
352      scheduler_node
353    );
354    _Scheduler_Release_critical( scheduler, &lock_context );
355
356    node = _Chain_Next( node );
357  } while ( node != tail );
358#else
359  const Scheduler_Control *scheduler;
360
361  scheduler = _Thread_Scheduler_get_home( the_thread );
362  ( *scheduler->Operations.update_priority )(
363    scheduler,
364    the_thread,
365    _Thread_Scheduler_get_home_node( the_thread )
366  );
367#endif
368}
369
370#if defined(RTEMS_SMP)
371/**
372 * @brief Changes the sticky level of the home scheduler node and propagates a
373 * priority change of a thread to the scheduler.
374 *
375 * @param[in] the_thread The thread changing its priority or sticky level.
376 *
377 * @see _Scheduler_Update_priority().
378 */
379RTEMS_INLINE_ROUTINE void _Scheduler_Priority_and_sticky_update(
380  Thread_Control *the_thread,
381  int             sticky_level_change
382)
383{
384  Chain_Node              *node;
385  const Chain_Node        *tail;
386  Scheduler_Node          *scheduler_node;
387  const Scheduler_Control *scheduler;
388  ISR_lock_Context         lock_context;
389
390  _Thread_Scheduler_process_requests( the_thread );
391
392  node = _Chain_First( &the_thread->Scheduler.Scheduler_nodes );
393  scheduler_node = SCHEDULER_NODE_OF_THREAD_SCHEDULER_NODE( node );
394  scheduler = _Scheduler_Node_get_scheduler( scheduler_node );
395
396  _Scheduler_Acquire_critical( scheduler, &lock_context );
397
398  scheduler_node->sticky_level += sticky_level_change;
399  _Assert( scheduler_node->sticky_level >= 0 );
400
401  ( *scheduler->Operations.update_priority )(
402    scheduler,
403    the_thread,
404    scheduler_node
405  );
406
407  _Scheduler_Release_critical( scheduler, &lock_context );
408
409  tail = _Chain_Immutable_tail( &the_thread->Scheduler.Scheduler_nodes );
410  node = _Chain_Next( node );
411
412  while ( node != tail ) {
413    scheduler_node = SCHEDULER_NODE_OF_THREAD_SCHEDULER_NODE( node );
414    scheduler = _Scheduler_Node_get_scheduler( scheduler_node );
415
416    _Scheduler_Acquire_critical( scheduler, &lock_context );
417    ( *scheduler->Operations.update_priority )(
418      scheduler,
419      the_thread,
420      scheduler_node
421    );
422    _Scheduler_Release_critical( scheduler, &lock_context );
423
424    node = _Chain_Next( node );
425  }
426}
427#endif
428
429/**
430 * @brief Maps a thread priority from the user domain to the scheduler domain.
431 *
432 * Let M be the maximum scheduler priority.  The mapping must be bijective in
433 * the closed interval [0, M], e.g. _Scheduler_Unmap_priority( scheduler,
434 * _Scheduler_Map_priority( scheduler, p ) ) == p for all p in [0, M].  For
435 * other values the mapping is undefined.
436 *
437 * @param[in] scheduler The scheduler instance.
438 * @param[in] priority The user domain thread priority.
439 *
440 * @return The corresponding thread priority of the scheduler domain is returned.
441 */
442RTEMS_INLINE_ROUTINE Priority_Control _Scheduler_Map_priority(
443  const Scheduler_Control *scheduler,
444  Priority_Control         priority
445)
446{
447  return ( *scheduler->Operations.map_priority )( scheduler, priority );
448}
449
450/**
451 * @brief Unmaps a thread priority from the scheduler domain to the user domain.
452 *
453 * @param[in] scheduler The scheduler instance.
454 * @param[in] priority The scheduler domain thread priority.
455 *
456 * @return The corresponding thread priority of the user domain is returned.
457 */
458RTEMS_INLINE_ROUTINE Priority_Control _Scheduler_Unmap_priority(
459  const Scheduler_Control *scheduler,
460  Priority_Control         priority
461)
462{
463  return ( *scheduler->Operations.unmap_priority )( scheduler, priority );
464}
465
466/**
467 * @brief Initializes a scheduler node.
468 *
469 * The scheduler node contains arbitrary data on function entry.  The caller
470 * must ensure that _Scheduler_Node_destroy() will be called after a
471 * _Scheduler_Node_initialize() before the memory of the scheduler node is
472 * destroyed.
473 *
474 * @param[in] scheduler The scheduler instance.
475 * @param[in] node The scheduler node to initialize.
476 * @param[in] the_thread The thread of the scheduler node to initialize.
477 * @param[in] priority The thread priority.
478 */
479RTEMS_INLINE_ROUTINE void _Scheduler_Node_initialize(
480  const Scheduler_Control *scheduler,
481  Scheduler_Node          *node,
482  Thread_Control          *the_thread,
483  Priority_Control         priority
484)
485{
486  ( *scheduler->Operations.node_initialize )(
487    scheduler,
488    node,
489    the_thread,
490    priority
491  );
492}
493
494/**
495 * @brief Destroys a scheduler node.
496 *
497 * The caller must ensure that _Scheduler_Node_destroy() will be called only
498 * after a corresponding _Scheduler_Node_initialize().
499 *
500 * @param[in] scheduler The scheduler instance.
501 * @param[in] node The scheduler node to destroy.
502 */
503RTEMS_INLINE_ROUTINE void _Scheduler_Node_destroy(
504  const Scheduler_Control *scheduler,
505  Scheduler_Node          *node
506)
507{
508  ( *scheduler->Operations.node_destroy )( scheduler, node );
509}
510
511/**
512 * @brief Releases a job of a thread with respect to the scheduler.
513 *
514 * @param[in] the_thread The thread.
515 * @param[in] priority_node The priority node of the job.
516 * @param[in] deadline The deadline in watchdog ticks since boot.
517 * @param[in] queue_context The thread queue context to provide the set of
518 *   threads for _Thread_Priority_update().
519 */
520RTEMS_INLINE_ROUTINE void _Scheduler_Release_job(
521  Thread_Control       *the_thread,
522  Priority_Node        *priority_node,
523  uint64_t              deadline,
524  Thread_queue_Context *queue_context
525)
526{
527  const Scheduler_Control *scheduler = _Thread_Scheduler_get_home( the_thread );
528
529  _Thread_queue_Context_clear_priority_updates( queue_context );
530  ( *scheduler->Operations.release_job )(
531    scheduler,
532    the_thread,
533    priority_node,
534    deadline,
535    queue_context
536  );
537}
538
539/**
540 * @brief Cancels a job of a thread with respect to the scheduler.
541 *
542 * @param[in] the_thread The thread.
543 * @param[in] priority_node The priority node of the job.
544 * @param[in] queue_context The thread queue context to provide the set of
545 *   threads for _Thread_Priority_update().
546 */
547RTEMS_INLINE_ROUTINE void _Scheduler_Cancel_job(
548  Thread_Control       *the_thread,
549  Priority_Node        *priority_node,
550  Thread_queue_Context *queue_context
551)
552{
553  const Scheduler_Control *scheduler = _Thread_Scheduler_get_home( the_thread );
554
555  _Thread_queue_Context_clear_priority_updates( queue_context );
556  ( *scheduler->Operations.cancel_job )(
557    scheduler,
558    the_thread,
559    priority_node,
560    queue_context
561  );
562}
563
564/**
565 * @brief Scheduler method invoked at each clock tick.
566 *
567 * This method is invoked at each clock tick to allow the scheduler
568 * implementation to perform any activities required.  For the
569 * scheduler which support standard RTEMS features, this includes
570 * time-slicing management.
571 */
572RTEMS_INLINE_ROUTINE void _Scheduler_Tick( const Per_CPU_Control *cpu )
573{
574  const Scheduler_Control *scheduler = _Scheduler_Get_by_CPU( cpu );
575  Thread_Control *executing = cpu->executing;
576
577  if ( scheduler != NULL && executing != NULL ) {
578    ( *scheduler->Operations.tick )( scheduler, executing );
579  }
580}
581
582/**
583 * @brief Starts the idle thread for a particular processor.
584 *
585 * @param[in] scheduler The scheduler instance.
586 * @param[in,out] the_thread The idle thread for the processor.
587 * @param[in,out] cpu The processor for the idle thread.
588 *
589 * @see _Thread_Create_idle().
590 */
591RTEMS_INLINE_ROUTINE void _Scheduler_Start_idle(
592  const Scheduler_Control *scheduler,
593  Thread_Control          *the_thread,
594  Per_CPU_Control         *cpu
595)
596{
597  ( *scheduler->Operations.start_idle )( scheduler, the_thread, cpu );
598}
599
600RTEMS_INLINE_ROUTINE bool _Scheduler_Has_processor_ownership(
601  const Scheduler_Control *scheduler,
602  uint32_t                 cpu_index
603)
604{
605#if defined(RTEMS_SMP)
606  const Per_CPU_Control   *cpu;
607  const Scheduler_Control *scheduler_of_cpu;
608
609  cpu = _Per_CPU_Get_by_index( cpu_index );
610  scheduler_of_cpu = _Scheduler_Get_by_CPU( cpu );
611
612  return scheduler_of_cpu == scheduler;
613#else
614  (void) scheduler;
615  (void) cpu_index;
616
617  return true;
618#endif
619}
620
621RTEMS_INLINE_ROUTINE const Processor_mask *_Scheduler_Get_processors(
622  const Scheduler_Control *scheduler
623)
624{
625#if defined(RTEMS_SMP)
626  return &_Scheduler_Get_context( scheduler )->Processors;
627#else
628  return &_Processor_mask_The_one_and_only;
629#endif
630}
631
632bool _Scheduler_Get_affinity(
633  Thread_Control *the_thread,
634  size_t          cpusetsize,
635  cpu_set_t      *cpuset
636);
637
638RTEMS_INLINE_ROUTINE bool _Scheduler_default_Set_affinity_body(
639  const Scheduler_Control *scheduler,
640  Thread_Control          *the_thread,
641  Scheduler_Node          *node,
642  const Processor_mask    *affinity
643)
644{
645  (void) scheduler;
646  (void) the_thread;
647  (void) node;
648  return _Processor_mask_Is_subset( affinity, _SMP_Get_online_processors() );
649}
650
651bool _Scheduler_Set_affinity(
652  Thread_Control  *the_thread,
653  size_t           cpusetsize,
654  const cpu_set_t *cpuset
655);
656
657RTEMS_INLINE_ROUTINE void _Scheduler_Generic_block(
658  const Scheduler_Control *scheduler,
659  Thread_Control          *the_thread,
660  Scheduler_Node          *node,
661  void                  ( *extract )(
662                             const Scheduler_Control *,
663                             Thread_Control *,
664                             Scheduler_Node *
665                        ),
666  void                  ( *schedule )(
667                             const Scheduler_Control *,
668                             Thread_Control *,
669                             bool
670                        )
671)
672{
673  ( *extract )( scheduler, the_thread, node );
674
675  /* TODO: flash critical section? */
676
677  if ( _Thread_Is_executing( the_thread ) || _Thread_Is_heir( the_thread ) ) {
678    ( *schedule )( scheduler, the_thread, true );
679  }
680}
681
682RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_processor_count(
683  const Scheduler_Control *scheduler
684)
685{
686#if defined(RTEMS_SMP)
687  const Scheduler_Context *context = _Scheduler_Get_context( scheduler );
688
689  return _Processor_mask_Count( &context->Processors );
690#else
691  (void) scheduler;
692
693  return 1;
694#endif
695}
696
697RTEMS_INLINE_ROUTINE Objects_Id _Scheduler_Build_id( uint32_t scheduler_index )
698{
699  return _Objects_Build_id(
700    OBJECTS_FAKE_OBJECTS_API,
701    OBJECTS_FAKE_OBJECTS_SCHEDULERS,
702    _Objects_Local_node,
703    (uint16_t) ( scheduler_index + 1 )
704  );
705}
706
707RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index_by_id( Objects_Id id )
708{
709  uint32_t minimum_id = _Scheduler_Build_id( 0 );
710
711  return id - minimum_id;
712}
713
714RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_id(
715  Objects_Id id
716)
717{
718  uint32_t index;
719
720  index = _Scheduler_Get_index_by_id( id );
721
722  if ( index >= _Scheduler_Count ) {
723    return NULL;
724  }
725
726  return &_Scheduler_Table[ index ];
727}
728
729RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index(
730  const Scheduler_Control *scheduler
731)
732{
733  return (uint32_t) (scheduler - &_Scheduler_Table[ 0 ]);
734}
735
736#if defined(RTEMS_SMP)
737/**
738 * @brief Gets an idle thread from the scheduler instance.
739 *
740 * @param[in] context The scheduler instance context.
741 *
742 * @retval idle An idle thread for use.  This function must always return an
743 * idle thread.  If none is available, then this is a fatal error.
744 */
745typedef Thread_Control *( *Scheduler_Get_idle_thread )(
746  Scheduler_Context *context
747);
748
749/**
750 * @brief Releases an idle thread to the scheduler instance for reuse.
751 *
752 * @param[in] context The scheduler instance context.
753 * @param[in] idle The idle thread to release
754 */
755typedef void ( *Scheduler_Release_idle_thread )(
756  Scheduler_Context *context,
757  Thread_Control    *idle
758);
759
760RTEMS_INLINE_ROUTINE void _Scheduler_Thread_change_state(
761  Thread_Control         *the_thread,
762  Thread_Scheduler_state  new_state
763)
764{
765  _Assert(
766    _ISR_lock_Is_owner( &the_thread->Scheduler.Lock )
767      || the_thread->Scheduler.state == THREAD_SCHEDULER_BLOCKED
768      || !_System_state_Is_up( _System_state_Get() )
769  );
770
771  the_thread->Scheduler.state = new_state;
772}
773
774RTEMS_INLINE_ROUTINE void _Scheduler_Set_idle_thread(
775  Scheduler_Node *node,
776  Thread_Control *idle
777)
778{
779  _Assert( _Scheduler_Node_get_idle( node ) == NULL );
780  _Assert(
781    _Scheduler_Node_get_owner( node ) == _Scheduler_Node_get_user( node )
782  );
783
784  _Scheduler_Node_set_user( node, idle );
785  node->idle = idle;
786}
787
788/**
789 * @brief Use an idle thread for this scheduler node.
790 *
791 * A thread those home scheduler node has a sticky level greater than zero may
792 * use an idle thread in the home scheduler instance in case it executes
793 * currently in another scheduler instance or in case it is in a blocking
794 * state.
795 *
796 * @param[in] context The scheduler instance context.
797 * @param[in] node The node which wants to use the idle thread.
798 * @param[in] cpu The processor for the idle thread.
799 * @param[in] get_idle_thread Function to get an idle thread.
800 */
801RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Use_idle_thread(
802  Scheduler_Context         *context,
803  Scheduler_Node            *node,
804  Per_CPU_Control           *cpu,
805  Scheduler_Get_idle_thread  get_idle_thread
806)
807{
808  Thread_Control *idle = ( *get_idle_thread )( context );
809
810  _Scheduler_Set_idle_thread( node, idle );
811  _Thread_Set_CPU( idle, cpu );
812  return idle;
813}
814
815typedef enum {
816  SCHEDULER_TRY_TO_SCHEDULE_DO_SCHEDULE,
817  SCHEDULER_TRY_TO_SCHEDULE_DO_IDLE_EXCHANGE,
818  SCHEDULER_TRY_TO_SCHEDULE_DO_BLOCK
819} Scheduler_Try_to_schedule_action;
820
821/**
822 * @brief Try to schedule this scheduler node.
823 *
824 * @param[in] context The scheduler instance context.
825 * @param[in] node The node which wants to get scheduled.
826 * @param[in] idle A potential idle thread used by a potential victim node.
827 * @param[in] get_idle_thread Function to get an idle thread.
828 *
829 * @retval true This node can be scheduled.
830 * @retval false Otherwise.
831 */
832RTEMS_INLINE_ROUTINE Scheduler_Try_to_schedule_action
833_Scheduler_Try_to_schedule_node(
834  Scheduler_Context         *context,
835  Scheduler_Node            *node,
836  Thread_Control            *idle,
837  Scheduler_Get_idle_thread  get_idle_thread
838)
839{
840  ISR_lock_Context                  lock_context;
841  Scheduler_Try_to_schedule_action  action;
842  Thread_Control                   *owner;
843
844  action = SCHEDULER_TRY_TO_SCHEDULE_DO_SCHEDULE;
845  owner = _Scheduler_Node_get_owner( node );
846  _Assert( _Scheduler_Node_get_user( node ) == owner );
847  _Assert( _Scheduler_Node_get_idle( node ) == NULL );
848
849  _Thread_Scheduler_acquire_critical( owner, &lock_context );
850
851  if ( owner->Scheduler.state == THREAD_SCHEDULER_READY ) {
852    _Thread_Scheduler_cancel_need_for_help( owner, _Thread_Get_CPU( owner ) );
853    _Scheduler_Thread_change_state( owner, THREAD_SCHEDULER_SCHEDULED );
854  } else if (
855    owner->Scheduler.state == THREAD_SCHEDULER_SCHEDULED
856      && node->sticky_level <= 1
857  ) {
858    action = SCHEDULER_TRY_TO_SCHEDULE_DO_BLOCK;
859  } else if ( node->sticky_level == 0 ) {
860    action = SCHEDULER_TRY_TO_SCHEDULE_DO_BLOCK;
861  } else if ( idle != NULL ) {
862    action = SCHEDULER_TRY_TO_SCHEDULE_DO_IDLE_EXCHANGE;
863  } else {
864    _Scheduler_Use_idle_thread(
865      context,
866      node,
867      _Thread_Get_CPU( owner ),
868      get_idle_thread
869    );
870  }
871
872  _Thread_Scheduler_release_critical( owner, &lock_context );
873  return action;
874}
875
876/**
877 * @brief Release an idle thread using this scheduler node.
878 *
879 * @param[in] context The scheduler instance context.
880 * @param[in] node The node which may have an idle thread as user.
881 * @param[in] release_idle_thread Function to release an idle thread.
882 *
883 * @retval idle The idle thread which used this node.
884 * @retval NULL This node had no idle thread as an user.
885 */
886RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Release_idle_thread(
887  Scheduler_Context             *context,
888  Scheduler_Node                *node,
889  Scheduler_Release_idle_thread  release_idle_thread
890)
891{
892  Thread_Control *idle = _Scheduler_Node_get_idle( node );
893
894  if ( idle != NULL ) {
895    Thread_Control *owner = _Scheduler_Node_get_owner( node );
896
897    node->idle = NULL;
898    _Scheduler_Node_set_user( node, owner );
899    ( *release_idle_thread )( context, idle );
900  }
901
902  return idle;
903}
904
905RTEMS_INLINE_ROUTINE void _Scheduler_Exchange_idle_thread(
906  Scheduler_Node *needs_idle,
907  Scheduler_Node *uses_idle,
908  Thread_Control *idle
909)
910{
911  uses_idle->idle = NULL;
912  _Scheduler_Node_set_user(
913    uses_idle,
914    _Scheduler_Node_get_owner( uses_idle )
915  );
916  _Scheduler_Set_idle_thread( needs_idle, idle );
917}
918
919/**
920 * @brief Block this scheduler node.
921 *
922 * @param[in] context The scheduler instance context.
923 * @param[in] thread The thread which wants to get blocked referencing this
924 *   node.  This is not necessarily the user of this node in case the node
925 *   participates in the scheduler helping protocol.
926 * @param[in] node The node which wants to get blocked.
927 * @param[in] is_scheduled This node is scheduled.
928 * @param[in] get_idle_thread Function to get an idle thread.
929 *
930 * @retval thread_cpu The processor of the thread.  Indicates to continue with
931 *   the blocking operation.
932 * @retval NULL Otherwise.
933 */
934RTEMS_INLINE_ROUTINE Per_CPU_Control *_Scheduler_Block_node(
935  Scheduler_Context         *context,
936  Thread_Control            *thread,
937  Scheduler_Node            *node,
938  bool                       is_scheduled,
939  Scheduler_Get_idle_thread  get_idle_thread
940)
941{
942  int               sticky_level;
943  ISR_lock_Context  lock_context;
944  Per_CPU_Control  *thread_cpu;
945
946  sticky_level = node->sticky_level;
947  --sticky_level;
948  node->sticky_level = sticky_level;
949  _Assert( sticky_level >= 0 );
950
951  _Thread_Scheduler_acquire_critical( thread, &lock_context );
952  thread_cpu = _Thread_Get_CPU( thread );
953  _Thread_Scheduler_cancel_need_for_help( thread, thread_cpu );
954  _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_BLOCKED );
955  _Thread_Scheduler_release_critical( thread, &lock_context );
956
957  if ( sticky_level > 0 ) {
958    if ( is_scheduled && _Scheduler_Node_get_idle( node ) == NULL ) {
959      Thread_Control *idle;
960
961      idle = _Scheduler_Use_idle_thread(
962        context,
963        node,
964        thread_cpu,
965        get_idle_thread
966      );
967      _Thread_Dispatch_update_heir( _Per_CPU_Get(), thread_cpu, idle );
968    }
969
970    return NULL;
971  }
972
973  _Assert( thread == _Scheduler_Node_get_user( node ) );
974  return thread_cpu;
975}
976
977RTEMS_INLINE_ROUTINE void _Scheduler_Discard_idle_thread(
978  Scheduler_Context             *context,
979  Thread_Control                *the_thread,
980  Scheduler_Node                *node,
981  Scheduler_Release_idle_thread  release_idle_thread
982)
983{
984  Thread_Control  *idle;
985  Thread_Control  *owner;
986  Per_CPU_Control *cpu;
987
988  idle = _Scheduler_Node_get_idle( node );
989  owner = _Scheduler_Node_get_owner( node );
990
991  node->idle = NULL;
992  _Assert( _Scheduler_Node_get_user( node ) == idle );
993  _Scheduler_Node_set_user( node, owner );
994  ( *release_idle_thread )( context, idle );
995
996  cpu = _Thread_Get_CPU( idle );
997  _Thread_Set_CPU( the_thread, cpu );
998  _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, the_thread );
999}
1000
1001/**
1002 * @brief Unblock this scheduler node.
1003 *
1004 * @param[in] context The scheduler instance context.
1005 * @param[in] the_thread The thread which wants to get unblocked.
1006 * @param[in] node The node which wants to get unblocked.
1007 * @param[in] is_scheduled This node is scheduled.
1008 * @param[in] release_idle_thread Function to release an idle thread.
1009 *
1010 * @retval true Continue with the unblocking operation.
1011 * @retval false Otherwise.
1012 */
1013RTEMS_INLINE_ROUTINE bool _Scheduler_Unblock_node(
1014  Scheduler_Context             *context,
1015  Thread_Control                *the_thread,
1016  Scheduler_Node                *node,
1017  bool                           is_scheduled,
1018  Scheduler_Release_idle_thread  release_idle_thread
1019)
1020{
1021  bool unblock;
1022
1023  ++node->sticky_level;
1024  _Assert( node->sticky_level > 0 );
1025
1026  if ( is_scheduled ) {
1027    _Scheduler_Discard_idle_thread(
1028      context,
1029      the_thread,
1030      node,
1031      release_idle_thread
1032    );
1033    _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_SCHEDULED );
1034    unblock = false;
1035  } else {
1036    _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_READY );
1037    unblock = true;
1038  }
1039
1040  return unblock;
1041}
1042#endif
1043
1044RTEMS_INLINE_ROUTINE void _Scheduler_Update_heir(
1045  Thread_Control *new_heir,
1046  bool            force_dispatch
1047)
1048{
1049  Thread_Control *heir = _Thread_Heir;
1050
1051  if ( heir != new_heir && ( heir->is_preemptible || force_dispatch ) ) {
1052#if defined(RTEMS_SMP)
1053    /*
1054     * We need this state only for _Thread_Get_CPU_time_used().  Cannot use
1055     * _Scheduler_Thread_change_state() since THREAD_SCHEDULER_BLOCKED to
1056     * THREAD_SCHEDULER_BLOCKED state changes are illegal for the real SMP
1057     * schedulers.
1058     */
1059    heir->Scheduler.state = THREAD_SCHEDULER_BLOCKED;
1060    new_heir->Scheduler.state = THREAD_SCHEDULER_SCHEDULED;
1061#endif
1062    _Thread_Update_CPU_time_used( heir, _Thread_Get_CPU( heir ) );
1063    _Thread_Heir = new_heir;
1064    _Thread_Dispatch_necessary = true;
1065  }
1066}
1067
1068RTEMS_INLINE_ROUTINE Status_Control _Scheduler_Set(
1069  const Scheduler_Control *new_scheduler,
1070  Thread_Control          *the_thread,
1071  Priority_Control         priority
1072)
1073{
1074  Scheduler_Node          *new_scheduler_node;
1075  Scheduler_Node          *old_scheduler_node;
1076#if defined(RTEMS_SMP)
1077  ISR_lock_Context         lock_context;
1078  const Scheduler_Control *old_scheduler;
1079
1080#endif
1081
1082  if ( the_thread->Wait.queue != NULL ) {
1083    return STATUS_RESOURCE_IN_USE;
1084  }
1085
1086  old_scheduler_node = _Thread_Scheduler_get_home_node( the_thread );
1087  _Priority_Plain_extract(
1088    &old_scheduler_node->Wait.Priority,
1089    &the_thread->Real_priority
1090  );
1091
1092  if ( !_Priority_Is_empty( &old_scheduler_node->Wait.Priority ) ) {
1093    _Priority_Plain_insert(
1094      &old_scheduler_node->Wait.Priority,
1095      &the_thread->Real_priority,
1096      the_thread->Real_priority.priority
1097    );
1098    return STATUS_RESOURCE_IN_USE;
1099  }
1100
1101#if defined(RTEMS_SMP)
1102  if ( !_Chain_Has_only_one_node( &the_thread->Scheduler.Wait_nodes ) ) {
1103    _Priority_Plain_insert(
1104      &old_scheduler_node->Wait.Priority,
1105      &the_thread->Real_priority,
1106      the_thread->Real_priority.priority
1107    );
1108    return STATUS_RESOURCE_IN_USE;
1109  }
1110
1111  old_scheduler = _Thread_Scheduler_get_home( the_thread );
1112  new_scheduler_node = _Thread_Scheduler_get_node_by_index(
1113    the_thread,
1114    _Scheduler_Get_index( new_scheduler )
1115  );
1116
1117  _Scheduler_Acquire_critical( new_scheduler, &lock_context );
1118
1119  if (
1120    _Scheduler_Get_processor_count( new_scheduler ) == 0
1121      || !( *new_scheduler->Operations.set_affinity )(
1122        new_scheduler,
1123        the_thread,
1124        new_scheduler_node,
1125        &the_thread->Scheduler.Affinity
1126      )
1127  ) {
1128    _Scheduler_Release_critical( new_scheduler, &lock_context );
1129    _Priority_Plain_insert(
1130      &old_scheduler_node->Wait.Priority,
1131      &the_thread->Real_priority,
1132      the_thread->Real_priority.priority
1133    );
1134    return STATUS_UNSATISFIED;
1135  }
1136
1137  the_thread->Scheduler.home = new_scheduler;
1138
1139  _Scheduler_Release_critical( new_scheduler, &lock_context );
1140
1141  _Thread_Scheduler_process_requests( the_thread );
1142#else
1143  new_scheduler_node = old_scheduler_node;
1144#endif
1145
1146  the_thread->Start.initial_priority = priority;
1147  _Priority_Node_set_priority( &the_thread->Real_priority, priority );
1148  _Priority_Initialize_one(
1149    &new_scheduler_node->Wait.Priority,
1150    &the_thread->Real_priority
1151  );
1152
1153#if defined(RTEMS_SMP)
1154  if ( old_scheduler != new_scheduler ) {
1155    States_Control current_state;
1156
1157    current_state = the_thread->current_state;
1158
1159    if ( _States_Is_ready( current_state ) ) {
1160      _Scheduler_Block( the_thread );
1161    }
1162
1163    _Assert( old_scheduler_node->sticky_level == 0 );
1164    _Assert( new_scheduler_node->sticky_level == 0 );
1165
1166    _Chain_Extract_unprotected( &old_scheduler_node->Thread.Wait_node );
1167    _Assert( _Chain_Is_empty( &the_thread->Scheduler.Wait_nodes ) );
1168    _Chain_Initialize_one(
1169      &the_thread->Scheduler.Wait_nodes,
1170      &new_scheduler_node->Thread.Wait_node
1171    );
1172    _Chain_Extract_unprotected(
1173      &old_scheduler_node->Thread.Scheduler_node.Chain
1174    );
1175    _Assert( _Chain_Is_empty( &the_thread->Scheduler.Scheduler_nodes ) );
1176    _Chain_Initialize_one(
1177      &the_thread->Scheduler.Scheduler_nodes,
1178      &new_scheduler_node->Thread.Scheduler_node.Chain
1179    );
1180
1181    _Scheduler_Node_set_priority( new_scheduler_node, priority, false );
1182
1183    if ( _States_Is_ready( current_state ) ) {
1184      _Scheduler_Unblock( the_thread );
1185    }
1186
1187    return STATUS_SUCCESSFUL;
1188  }
1189#endif
1190
1191  _Scheduler_Node_set_priority( new_scheduler_node, priority, false );
1192  _Scheduler_Update_priority( the_thread );
1193  return STATUS_SUCCESSFUL;
1194}
1195
1196/** @} */
1197
1198#ifdef __cplusplus
1199}
1200#endif
1201
1202#endif
1203/* end of include file */
Note: See TracBrowser for help on using the repository browser.