Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:13

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSScoreScheduler
0007  *
0008  * @brief This header file provides interfaces of the supporting the
0009  *   implementation of uniprocessor schedulers.
0010  */
0011 
0012 /*
0013  *  Copyright (C) 2010 Gedare Bloom.
0014  *  Copyright (C) 2011 On-Line Applications Research Corporation (OAR).
0015  *  Copyright (C) 2014, 2022 embedded brains GmbH & Co. KG
0016  *
0017  * Redistribution and use in source and binary forms, with or without
0018  * modification, are permitted provided that the following conditions
0019  * are met:
0020  * 1. Redistributions of source code must retain the above copyright
0021  *    notice, this list of conditions and the following disclaimer.
0022  * 2. Redistributions in binary form must reproduce the above copyright
0023  *    notice, this list of conditions and the following disclaimer in the
0024  *    documentation and/or other materials provided with the distribution.
0025  *
0026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0036  * POSSIBILITY OF SUCH DAMAGE.
0037  */
0038 
0039 #ifndef _RTEMS_SCORE_SCHEDULERUNIIMPL_H
0040 #define _RTEMS_SCORE_SCHEDULERUNIIMPL_H
0041 
0042 #include <rtems/score/schedulerimpl.h>
0043 
0044 #ifdef __cplusplus
0045 extern "C" {
0046 #endif
0047 
0048 /**
0049  * @addtogroup RTEMSScoreScheduler
0050  *
0051  * @{
0052  */
0053 
0054 /**
0055  * @brief Updates the heir thread of the processor.
0056  *
0057  * @param[in, out] heir is the current heir thread.
0058  * @param[in, out] new_heir is the new heir thread.
0059  */
0060 static inline void _Scheduler_uniprocessor_Update_heir(
0061   Thread_Control *heir,
0062   Thread_Control *new_heir
0063 )
0064 {
0065   _Assert( heir != new_heir );
0066 #if defined(RTEMS_SMP)
0067   /*
0068    * We need this state only for _Thread_Get_CPU_time_used_locked().  Cannot
0069    * use _Scheduler_Thread_change_state() since THREAD_SCHEDULER_BLOCKED to
0070    * THREAD_SCHEDULER_BLOCKED state changes are illegal for the real SMP
0071    * schedulers.
0072    */
0073   heir->Scheduler.state = THREAD_SCHEDULER_BLOCKED;
0074   new_heir->Scheduler.state = THREAD_SCHEDULER_SCHEDULED;
0075 #endif
0076   _Thread_Update_CPU_time_used( heir, _Thread_Get_CPU( heir ) );
0077   _Thread_Heir = new_heir;
0078   _Thread_Dispatch_necessary = true;
0079 }
0080 
0081 /**
0082  * @brief Updates the heir thread of the processor if the current heir is
0083  *   not equal to the new heir thread.
0084  *
0085  * The update takes place even if the current heir thread is not preemptible.
0086  *
0087  * @param[in, out] new_heir is the new heir thread.
0088  */
0089 static inline void _Scheduler_uniprocessor_Update_heir_if_necessary(
0090   Thread_Control *new_heir
0091 )
0092 {
0093   Thread_Control *heir = _Thread_Heir;
0094 
0095   if ( heir != new_heir ) {
0096     _Scheduler_uniprocessor_Update_heir( heir, new_heir );
0097   }
0098 }
0099 
0100 /**
0101  * @brief Updates the heir thread of the processor if the current heir thread
0102  *   is preemptible.
0103  *
0104  * @param[in, out] heir is the current heir thread.
0105  * @param[in, out] new_heir is the new heir thread.
0106  */
0107 static inline void _Scheduler_uniprocessor_Update_heir_if_preemptible(
0108   Thread_Control *heir,
0109   Thread_Control *new_heir
0110 )
0111 {
0112   if ( heir != new_heir && heir->is_preemptible ) {
0113     _Scheduler_uniprocessor_Update_heir( heir, new_heir );
0114   }
0115 }
0116 
0117 /**
0118  * @brief Blocks the thread.
0119  *
0120  * @param scheduler is the scheduler.
0121  * @param the_thread is the thread to block.
0122  * @param node is the scheduler node of the thread.
0123  * @param extract is the handler to extract the thread.
0124  * @param get_highest_ready is the handler to get the highest ready thread.
0125  */
0126 static inline void _Scheduler_uniprocessor_Block(
0127   const Scheduler_Control *scheduler,
0128   Thread_Control          *the_thread,
0129   Scheduler_Node          *node,
0130   void                  ( *extract )(
0131                              const Scheduler_Control *,
0132                              Thread_Control *,
0133                              Scheduler_Node *
0134                         ),
0135   Thread_Control       *( *get_highest_ready )( const Scheduler_Control * )
0136 )
0137 {
0138   ( *extract )( scheduler, the_thread, node );
0139 
0140   /* TODO: flash critical section? */
0141 
0142   if ( _Thread_Is_heir( the_thread ) ) {
0143     Thread_Control *highest_ready;
0144 
0145     highest_ready = ( *get_highest_ready )( scheduler );
0146     _Scheduler_uniprocessor_Update_heir( _Thread_Heir, highest_ready );
0147   }
0148 }
0149 
0150 /**
0151  * @brief Schedule the unblocked thread if it is the highest ready thread.
0152  *
0153  * @param scheduler is the scheduler.
0154  * @param the_thread is the thread.
0155  * @param priority is the priority of the thread.
0156  */
0157 static inline void _Scheduler_uniprocessor_Unblock(
0158   const Scheduler_Control *scheduler,
0159   Thread_Control          *the_thread,
0160   Priority_Control         priority
0161 )
0162 {
0163   Thread_Control *heir;
0164 
0165   heir = _Thread_Heir;
0166 
0167   /*
0168    * If the thread is more important than the heir, then we have a new heir.
0169    * This may or may not result in a context switch.  If the current heir
0170    * thread is preemptible, then we need to do a context switch.
0171    */
0172   if ( priority < _Thread_Get_priority( heir ) ) {
0173     _Scheduler_uniprocessor_Update_heir_if_preemptible( heir, the_thread );
0174   }
0175 }
0176 
0177 /**
0178  * @brief Schedules the highest ready thread if the current heir thread of the
0179  * processor is preemptible.
0180  *
0181  * @param scheduler is the scheduler.
0182  * @param get_highest_ready is the handler to get the highest ready thread.
0183  */
0184 static inline void _Scheduler_uniprocessor_Schedule(
0185   const Scheduler_Control *scheduler,
0186   Thread_Control       *( *get_highest_ready )( const Scheduler_Control * )
0187 )
0188 {
0189   Thread_Control *highest_ready;
0190 
0191   highest_ready = ( *get_highest_ready )( scheduler );
0192   _Scheduler_uniprocessor_Update_heir_if_preemptible(
0193     _Thread_Heir,
0194     highest_ready
0195   );
0196 }
0197 
0198 /**
0199  * @brief Yields to the highest ready thread.
0200  *
0201  * @param scheduler is the scheduler.
0202  * @param get_highest_ready is the handler to get the highest ready thread.
0203  */
0204 static inline void _Scheduler_uniprocessor_Yield(
0205   const Scheduler_Control *scheduler,
0206   Thread_Control       *( *get_highest_ready )( const Scheduler_Control * )
0207 )
0208 {
0209   Thread_Control *highest_ready;
0210 
0211   highest_ready = ( *get_highest_ready )( scheduler );
0212   _Scheduler_uniprocessor_Update_heir_if_necessary( highest_ready );
0213 }
0214 
0215 /** @} */
0216 
0217 #ifdef __cplusplus
0218 }
0219 #endif
0220 
0221 #endif /* _RTEMS_SCORE_SCHEDULERUNIIMPL_H */