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 RTEMSScoreProcessorMask
0007  *
0008  * @brief This header file provides the interfaces of the
0009  *   @ref RTEMSScoreProcessorMask.
0010  */
0011 
0012 /*
0013  * Copyright (C) 2016, 2023 embedded brains GmbH & Co. KG
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifndef _RTEMS_SCORE_PROCESSORMASKIMPL_H
0038 #define _RTEMS_SCORE_PROCESSORMASKIMPL_H
0039 
0040 #include <rtems/score/processormask.h>
0041 
0042 #include <sys/cpuset.h>
0043 
0044 #include <strings.h>
0045 
0046 #ifdef __cplusplus
0047 extern "C" {
0048 #endif /* __cplusplus */
0049 
0050 /**
0051  * @addtogroup RTEMSScoreProcessorMask
0052  *
0053  * @{
0054  */
0055 
0056 /**
0057  * @brief Sets the bits of the mask to zero, also considers CPU_MAXIMUM_PROCESSORS.
0058  *
0059  * @param[out] mask The mask to set to zero.
0060  */
0061 static inline void _Processor_mask_Zero( Processor_mask *mask )
0062 {
0063   __BIT_ZERO( CPU_MAXIMUM_PROCESSORS, mask );
0064 }
0065 
0066 /**
0067  * @brief Checks if the mask is zero, also considers CPU_MAXIMUM_PROCESSORS.
0068  *
0069  * @param mask The mask to check whether is is zero
0070  *
0071  * @retval true The mask is zero.
0072  * @retval false The mask is not zero.
0073  */
0074 static inline bool _Processor_mask_Is_zero( const Processor_mask *mask )
0075 {
0076   return __BIT_EMPTY( CPU_MAXIMUM_PROCESSORS, mask );
0077 }
0078 
0079 /**
0080  * @brief Fills the mask, also considers CPU_MAXIMUM_PROCESSORS.
0081  *
0082  * @param[out] mask The mask to fill
0083  */
0084 static inline void _Processor_mask_Fill( Processor_mask *mask )
0085 {
0086   __BIT_FILL( CPU_MAXIMUM_PROCESSORS, mask );
0087 }
0088 
0089 /**
0090  * @brief Copies the mask to another mask, also considers CPU_MAXIMUM_PROCESSORS.
0091  *
0092  * @param[out] dst The mask to copy @a src to.
0093  * @param src The mask to copy to @a dst.
0094  */
0095 static inline void _Processor_mask_Assign(
0096   Processor_mask *dst, const Processor_mask *src
0097 )
0098 {
0099   __BIT_COPY( CPU_MAXIMUM_PROCESSORS, src, dst );
0100 }
0101 
0102 /**
0103  * @brief Sets the specified index bit of the mask.
0104  *
0105  * @param[out] mask The mask to set the bit of.
0106  * @param index The index of the bit that shall be set.
0107  */
0108 static inline void _Processor_mask_Set(
0109   Processor_mask *mask,
0110   uint32_t        index
0111 )
0112 {
0113   __BIT_SET( CPU_MAXIMUM_PROCESSORS, index, mask );
0114 }
0115 
0116 /**
0117  * @brief Clears the specified index bit of the mask.
0118  *
0119  * @param[out] mask The mask to clear the bit of.
0120  * @param index The index of the bit that shall be cleared.
0121  */
0122 static inline void _Processor_mask_Clear(
0123   Processor_mask *mask,
0124   uint32_t        index
0125 )
0126 {
0127   __BIT_CLR( CPU_MAXIMUM_PROCESSORS, index, mask );
0128 }
0129 
0130 /**
0131  * @brief Checks if the specified index bit of the mask is set.
0132  *
0133  * @param mask The mask to check if the specified bit is set.
0134  * @param index The index of the bit that is checked.
0135  *
0136  * @retval true The specified index bit is set.
0137  * @retval false The specified index bit is not set.
0138  */
0139 static inline bool _Processor_mask_Is_set(
0140   const Processor_mask *mask,
0141   uint32_t              index
0142 )
0143 {
0144   return __BIT_ISSET( CPU_MAXIMUM_PROCESSORS, index, mask );
0145 }
0146 
0147 /**
0148  * @brief Checks if the processor sets a and b are equal.
0149  *
0150  * @param a The first processor set.
0151  * @param b The seconde processor set.
0152  *
0153  * @retval true The processor sets a and b are equal.
0154  * @retval false The processor sets a and b are not equal.
0155  */
0156 static inline bool _Processor_mask_Is_equal(
0157   const Processor_mask *a,
0158   const Processor_mask *b
0159 )
0160 {
0161   return !__BIT_CMP( CPU_MAXIMUM_PROCESSORS, a, b );
0162 }
0163 
0164 /**
0165  * @brief Checks if the intersection of the processor sets a and b is
0166  * non-empty.
0167  *
0168  * @param a The first processor set.
0169  * @param b The second processor set.
0170  *
0171  * @retval true The intersection of the processor sets a and b is non-empty.
0172  * @retval false The intersection of the processor sets a and b is empty.
0173  */
0174 static inline bool _Processor_mask_Has_overlap(
0175   const Processor_mask *a,
0176   const Processor_mask *b
0177 )
0178 {
0179   return __BIT_OVERLAP( CPU_MAXIMUM_PROCESSORS, a, b );
0180 }
0181 
0182 /**
0183  * @brief Checks if the processor set small is a subset of processor set
0184  * big.
0185  *
0186  * @param big The bigger processor set.
0187  * @param small The smaller processor set.
0188  *
0189  * @retval true @a small is a subset of @a big.
0190  * @retval false @a small is not a subset of @a big.
0191  */
0192 static inline bool _Processor_mask_Is_subset(
0193   const Processor_mask *big,
0194   const Processor_mask *small
0195 )
0196 {
0197   return __BIT_SUBSET( CPU_MAXIMUM_PROCESSORS, big, small );
0198 }
0199 
0200 /**
0201  * @brief Performs a bitwise a = b & c.
0202  *
0203  * @param[out] a The processor mask that is set by this operation.
0204  * @param b The first parameter of the AND-operation.
0205  * @param c The second parameter of the AND-operation.
0206  */
0207 static inline void _Processor_mask_And(
0208   Processor_mask       *a,
0209   const Processor_mask *b,
0210   const Processor_mask *c
0211 )
0212 {
0213   __BIT_AND2( CPU_MAXIMUM_PROCESSORS, a, b, c );
0214 }
0215 
0216 /**
0217  * @brief Performs a bitwise a = b & ~c.
0218  *
0219  * @param[out] a The processor mask that is set by this operation.
0220  * @param b The first parameter of the ANDNOT-operation.
0221  * @param c The second parameter of the ANDNOT-operation.
0222  */
0223 static inline void _Processor_mask_And_not(
0224   Processor_mask       *a,
0225   const Processor_mask *b,
0226   const Processor_mask *c
0227 )
0228 {
0229   __BIT_ANDNOT2( CPU_MAXIMUM_PROCESSORS, a, b, c );
0230 }
0231 
0232 /**
0233  * @brief Performs a bitwise a = b | c.
0234  *
0235  * @param[out] a The processor mask that is set by this operation.
0236  * @param b The first parameter of the OR-operation.
0237  * @param c The second parameter of the OR-operation.
0238  */
0239 static inline void _Processor_mask_Or(
0240   Processor_mask       *a,
0241   const Processor_mask *b,
0242   const Processor_mask *c
0243 )
0244 {
0245   __BIT_OR2( CPU_MAXIMUM_PROCESSORS, a, b, c );
0246 }
0247 
0248 /**
0249  * @brief Performs a bitwise a = b ^ c.
0250  *
0251  * @param[out] a The processor mask that is set by this operation.
0252  * @param b The first parameter of the XOR-operation.
0253  * @param c The second parameter of the XOR-operation.
0254  */
0255 static inline void _Processor_mask_Xor(
0256   Processor_mask       *a,
0257   const Processor_mask *b,
0258   const Processor_mask *c
0259 )
0260 {
0261   __BIT_XOR2( CPU_MAXIMUM_PROCESSORS, a, b, c );
0262 }
0263 
0264 /**
0265  * @brief Gets the number of set bits in the processor mask.
0266  *
0267  * @param a The processor mask of which the set bits are counted.
0268  *
0269  * @return The number of set bits in @a a.
0270  */
0271 static inline uint32_t _Processor_mask_Count( const Processor_mask *a )
0272 {
0273   return (uint32_t) __BIT_COUNT( CPU_MAXIMUM_PROCESSORS, a );
0274 }
0275 
0276 /**
0277  * @brief Finds the last set of the processor mask.
0278  *
0279  * @param a The processor mask wo find the last set of.
0280  *
0281  * @return The last set of @a a.
0282  */
0283 static inline uint32_t _Processor_mask_Find_last_set( const Processor_mask *a )
0284 {
0285   return (uint32_t) __BIT_FLS( CPU_MAXIMUM_PROCESSORS, a );
0286 }
0287 
0288 /**
0289  * @brief Returns the subset of 32 processors containing the specified index as
0290  * an unsigned 32-bit integer.
0291  *
0292  * @param mask The processor mask.
0293  * @param index The specified index.
0294  *
0295  * @return The subset containing the specified index as an unsigned 32-bit integer.
0296  */
0297 static inline uint32_t _Processor_mask_To_uint32_t(
0298   const Processor_mask *mask,
0299   uint32_t              index
0300 )
0301 {
0302   long bits = mask->__bits[ index / _BITSET_BITS ];
0303 
0304   return (uint32_t) ( bits >> ( 32 * ( ( index % _BITSET_BITS ) / 32 ) ) );
0305 }
0306 
0307 /**
0308  * @brief Creates a processor set from an unsigned 32-bit integer relative to
0309  * the specified index.
0310  *
0311  * @param[out] mask The mask that is created.
0312  * @param bits The bits for creating the mask.
0313  * @param index The index to which the mask is relative.
0314  */
0315 static inline void _Processor_mask_From_uint32_t(
0316   Processor_mask *mask,
0317   uint32_t        bits,
0318   uint32_t        index
0319 )
0320 {
0321   _Processor_mask_Zero( mask );
0322   mask->__bits[ __bitset_words( index ) ] = ((long) bits) << (32 * (index % _BITSET_BITS) / 32);
0323 }
0324 
0325 /**
0326  * @brief Creates a processor set from the specified index.
0327  *
0328  * @param[out] The mask that is created.
0329  * @param index The specified index.
0330  */
0331 static inline void _Processor_mask_From_index(
0332   Processor_mask *mask,
0333   uint32_t        index
0334 )
0335 {
0336   __BIT_SETOF( CPU_MAXIMUM_PROCESSORS, (int) index, mask );
0337 }
0338 
0339 typedef enum {
0340   PROCESSOR_MASK_COPY_LOSSLESS,
0341   PROCESSOR_MASK_COPY_PARTIAL_LOSS,
0342   PROCESSOR_MASK_COPY_COMPLETE_LOSS,
0343   PROCESSOR_MASK_COPY_INVALID_SIZE
0344 } Processor_mask_Copy_status;
0345 
0346 /**
0347  * @brief Checks if the copy status guarantees at most partial loss.
0348  *
0349  * @param status The copy status to check.
0350  *
0351  * @retval true At most partial loss can be guaranteed.
0352  * @retval false The status indicates more than partial loss.
0353  */
0354 static inline bool _Processor_mask_Is_at_most_partial_loss(
0355   Processor_mask_Copy_status status
0356 )
0357 {
0358   return (unsigned int) status <= PROCESSOR_MASK_COPY_PARTIAL_LOSS;
0359 }
0360 
0361 /**
0362  * @brief Copies one mask to another.
0363  *
0364  * @param[out] dst The destination of the copy operation.
0365  * @param dst_size The size of @a dst.
0366  * @param src The source of the copy operation.
0367  * @param src_size The size of @a src.
0368  *
0369  * @retval PROCESSOR_MASK_COPY_LOSSLESS It is guaranteed that the copy
0370  *      operation is lossless.
0371  * @retval PROCESSOR_MASK_COPY_PARTIAL_LOSS Partial loss happened due
0372  *      to the sizes of @a src and @a dst.
0373  * @retval PROCESSOR_MASK_COPY_COMPLETE_LOSS Complete loss happened due
0374  *      to the sizes of @a src and @a dst.
0375  * @retval PROCESSOR_MASK_COPY_INVALID_SIZE One of the arguments sizes
0376  *      is invalid (bigger than the size of a long).
0377  */
0378 Processor_mask_Copy_status _Processor_mask_Copy(
0379   long       *dst,
0380   size_t      dst_size,
0381   const long *src,
0382   size_t      src_size
0383 );
0384 
0385 /**
0386  * @brief Copies one mask to another.
0387  *
0388  * @param src The source for the copy operation.
0389  * @param dst_size The size of @a dst.
0390  * @param[out] dst The destination for the copy operation.
0391  *
0392  * @retval PROCESSOR_MASK_COPY_LOSSLESS It is guaranteed that the copy
0393  *      operation is lossless.
0394  * @retval PROCESSOR_MASK_COPY_PARTIAL_LOSS Partial loss happened due
0395  *      to the sizes of @a src and @a dst.
0396  * @retval PROCESSOR_MASK_COPY_COMPLETE_LOSS Complete loss happened due
0397  *      to the sizes of @a src and @a dst.
0398  * @retval PROCESSOR_MASK_COPY_INVALID_SIZE One of the arguments sizes
0399  *      is invalid (bigger than the size of a long).
0400  */
0401 static inline Processor_mask_Copy_status _Processor_mask_To_cpu_set_t(
0402   const Processor_mask *src,
0403   size_t                dst_size,
0404   cpu_set_t            *dst
0405 )
0406 {
0407   return _Processor_mask_Copy(
0408     &dst->__bits[ 0 ],
0409     dst_size,
0410     &src->__bits[ 0 ],
0411     sizeof( *src )
0412   );
0413 }
0414 
0415 /**
0416  * @brief Copies one mask to another.
0417  *
0418  * @param src The source for the copy operation.
0419  * @param src_size The size of @a src.
0420  * @param[out] dst The destination for the copy operation.
0421  *
0422  * @retval PROCESSOR_MASK_COPY_LOSSLESS It is guaranteed that the copy
0423  *      operation is lossless.
0424  * @retval PROCESSOR_MASK_COPY_PARTIAL_LOSS Partial loss happened due
0425  *      to the sizes of @a src and @a dst.
0426  * @retval PROCESSOR_MASK_COPY_COMPLETE_LOSS Complete loss happened due
0427  *      to the sizes of @a src and @a dst.
0428  * @retval PROCESSOR_MASK_COPY_INVALID_SIZE One of the arguments sizes
0429  *      is invalid (bigger than the size of a long).
0430  */
0431 static inline Processor_mask_Copy_status _Processor_mask_From_cpu_set_t(
0432   Processor_mask  *dst,
0433   size_t           src_size,
0434   const cpu_set_t *src
0435 )
0436 {
0437   return _Processor_mask_Copy(
0438     &dst->__bits[ 0 ],
0439     sizeof( *dst ),
0440     &src->__bits[ 0 ],
0441     src_size
0442   );
0443 }
0444 
0445 extern const Processor_mask _Processor_mask_The_one_and_only;
0446 
0447 /** @} */
0448 
0449 #ifdef __cplusplus
0450 }
0451 #endif /* __cplusplus */
0452 
0453 #endif /* _RTEMS_SCORE_PROCESSORMASKIMPL_H */