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 RTEMSScorePriorityBitmap
0007  *
0008  * @brief This header file provides interfaces of the
0009  *   @ref RTEMSScorePriorityBitmap which are only used by the implementation.
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-2010.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifndef _RTEMS_SCORE_PRIORITYBITMAPIMPL_H
0039 #define _RTEMS_SCORE_PRIORITYBITMAPIMPL_H
0040 
0041 #include <rtems/score/prioritybitmap.h>
0042 
0043 #include <string.h>
0044 
0045 #ifdef __cplusplus
0046 extern "C" {
0047 #endif
0048 
0049 /**
0050  * @addtogroup RTEMSScorePriority
0051  *
0052  * @{
0053  */
0054 
0055 /**
0056  *  This table is used by the generic bitfield routines to perform
0057  *  a highly optimized bit scan without the use of special CPU
0058  *  instructions.
0059  */
0060 extern const unsigned char _Bitfield_Leading_zeros[256];
0061 
0062 /**
0063  * @brief Returns the bit number of the first bit set in the specified value.
0064  *
0065  * The correspondence between the bit number and actual bit position is CPU
0066  * architecture dependent.  The search for the first bit set may run from most
0067  * to least significant bit or vice-versa.
0068  *
0069  * @param value The value to bit scan.
0070  *
0071  * @return The bit number of the first bit set.
0072  *
0073  * @see _Priority_Bits_index() and _Priority_Mask().
0074  */
0075 static inline unsigned int _Bitfield_Find_first_bit(
0076   unsigned int value
0077 )
0078 {
0079   unsigned int bit_number;
0080 
0081 #if ( CPU_USE_GENERIC_BITFIELD_CODE == FALSE )
0082   _CPU_Bitfield_Find_first_bit( value, bit_number );
0083 #elif defined(__GNUC__)
0084   bit_number = (unsigned int) __builtin_clz( value )
0085     - __SIZEOF_INT__ * __CHAR_BIT__ + 16;
0086 #else
0087   if ( value < 0x100 ) {
0088     bit_number = _Bitfield_Leading_zeros[ value ] + 8;
0089   } else { \
0090     bit_number = _Bitfield_Leading_zeros[ value >> 8 ];
0091   }
0092 #endif
0093 
0094   return bit_number;
0095 }
0096 
0097 /**
0098  * @brief Returns the priority bit mask for the specified major or minor bit
0099  * number.
0100  *
0101  * @param bit_number The bit number for which we need a mask.
0102  *
0103  * @return The priority bit mask.
0104  */
0105 static inline Priority_bit_map_Word _Priority_Mask(
0106   unsigned int bit_number
0107 )
0108 {
0109 #if ( CPU_USE_GENERIC_BITFIELD_CODE == FALSE )
0110   return _CPU_Priority_Mask( bit_number );
0111 #else
0112   return (Priority_bit_map_Word) ( 0x8000u >> bit_number );
0113 #endif
0114 }
0115 
0116 /**
0117  * @brief Returns the bit index position for the specified major or minor bit
0118  * number.
0119  *
0120  * @param bit_number The bit number for which we need an index.
0121  *
0122  * @return The corresponding array index into the priority bit map.
0123  */
0124 static inline unsigned int _Priority_Bits_index(
0125   unsigned int bit_number
0126 )
0127 {
0128 #if ( CPU_USE_GENERIC_BITFIELD_CODE == FALSE )
0129   return _CPU_Priority_bits_index( bit_number );
0130 #else
0131   return bit_number;
0132 #endif
0133 }
0134 
0135 /**
0136  * @brief Returns the major portion of the_priority.
0137  *
0138  * @param the_priority The priority of which we want the major portion.
0139  *
0140  * @return The major portion of the priority.
0141  */
0142 static inline unsigned int _Priority_Major( unsigned int the_priority )
0143 {
0144   return the_priority / 16;
0145 }
0146 
0147 /**
0148  * @brief Returns the minor portion of the_priority.
0149  *
0150  * @param the_priority The priority of which we want the minor portion.
0151  *
0152  * @return The minor portion of the priority.
0153  */
0154 static inline unsigned int _Priority_Minor( unsigned int the_priority )
0155 {
0156   return the_priority % 16;
0157 }
0158 
0159 /**
0160  * @brief Initializes a bit map.
0161  *
0162  * @param[out] bit_map The bit map to initialize.
0163  */
0164 static inline void _Priority_bit_map_Initialize(
0165   Priority_bit_map_Control *bit_map
0166 )
0167 {
0168   memset( bit_map, 0, sizeof( *bit_map ) );
0169 }
0170 
0171 /**
0172  * @brief Adds Priority queue bit map information.
0173  *
0174  * Priority Queue implemented by bit map.
0175  *
0176  * @param[out] bit_map The bit map to be altered by @a bit_map_info.
0177  * @param bit_map_info The information with which to alter @a bit_map.
0178  */
0179 static inline void _Priority_bit_map_Add (
0180   Priority_bit_map_Control     *bit_map,
0181   Priority_bit_map_Information *bit_map_info
0182 )
0183 {
0184   *bit_map_info->minor |= bit_map_info->ready_minor;
0185   bit_map->major_bit_map |= bit_map_info->ready_major;
0186 }
0187 
0188 /**
0189  * @brief Removes Priority queue bit map information.
0190  *
0191  * Priority Queue implemented by bit map.
0192  *
0193  * @param[out] bit_map The bit map to be altered by @a bit_map_info.
0194  * @param bit_map_info The information with which to alter @a bit_map.
0195  */
0196 static inline void _Priority_bit_map_Remove (
0197   Priority_bit_map_Control     *bit_map,
0198   Priority_bit_map_Information *bit_map_info
0199 )
0200 {
0201   *bit_map_info->minor &= bit_map_info->block_minor;
0202   if ( *bit_map_info->minor == 0 )
0203     bit_map->major_bit_map &= bit_map_info->block_major;
0204 }
0205 
0206 /**
0207  * @brief Gets highest portion of Priority queue bit map.
0208  *
0209  * @param bit_map The bitmap to get the highest portion from.
0210  *
0211  * @return The highest portion of the bitmap.
0212  */
0213 static inline unsigned int _Priority_bit_map_Get_highest(
0214   const Priority_bit_map_Control *bit_map
0215 )
0216 {
0217   unsigned int minor;
0218   unsigned int major;
0219 
0220   major = _Bitfield_Find_first_bit( bit_map->major_bit_map );
0221   minor = _Bitfield_Find_first_bit( bit_map->bit_map[ major ] );
0222 
0223   return (_Priority_Bits_index( major ) << 4) +
0224           _Priority_Bits_index( minor );
0225 }
0226 
0227 /**
0228  * @brief Checks if the Priority queue bit map is empty.
0229  *
0230  * @param bit_map The bit map of which to check if it is empty.
0231  *
0232  * @retval true The Priority queue bit map is empty
0233  * @retval false The Priority queue bit map is not empty.
0234  */
0235 static inline bool _Priority_bit_map_Is_empty(
0236   const Priority_bit_map_Control *bit_map
0237 )
0238 {
0239   return bit_map->major_bit_map == 0;
0240 }
0241 
0242 /**
0243  * @brief Initializes the bit map information.
0244  *
0245  * @param bit_map The bit map for the initialization of the bit
0246  *      map information.
0247  * @param[out] bit_map_info The bit map information to initialize.
0248  * @param new_priority The new priority for the initialization
0249  *      of the bit map information.
0250  */
0251 static inline void _Priority_bit_map_Initialize_information(
0252   Priority_bit_map_Control     *bit_map,
0253   Priority_bit_map_Information *bit_map_info,
0254   unsigned int                  new_priority
0255 )
0256 {
0257   unsigned int major;
0258   unsigned int minor;
0259   Priority_bit_map_Word mask;
0260 
0261   major = _Priority_Major( new_priority );
0262   minor = _Priority_Minor( new_priority );
0263 
0264   bit_map_info->minor = &bit_map->bit_map[ _Priority_Bits_index( major ) ];
0265 
0266   mask = _Priority_Mask( major );
0267   bit_map_info->ready_major = mask;
0268   bit_map_info->block_major = (Priority_bit_map_Word) ~mask;
0269 
0270   mask = _Priority_Mask( minor );
0271   bit_map_info->ready_minor = mask;
0272   bit_map_info->block_minor = (Priority_bit_map_Word) ~mask;
0273 }
0274 
0275 /** @} */
0276 
0277 #ifdef __cplusplus
0278 }
0279 #endif
0280 
0281 #endif
0282 /* end of include file */