source: rtems/cpukit/include/rtems/score/schedulerimpl.h @ 5803f37

5
Last change on this file since 5803f37 was 5803f37, checked in by Sebastian Huber <sebastian.huber@…>, on 06/28/19 at 06:30:11

score: Add and use _Thread_Get_unmapped_priority().

Add and use _Thread_Get_unmapped_real_priority().

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