qpc/include/qpset.h

176 lines
6.4 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @file
2016-09-23 12:08:21 -04:00
* @brief QP native, platform-independent priority sets of 32 or 64 elements.
2015-04-28 13:45:35 -04:00
* @ingroup qf
* @cond
2014-04-06 11:43:13 -04:00
******************************************************************************
2020-03-17 21:33:20 -04:00
* Last updated for version 6.8.0
* Last updated on 2020-01-21
2012-08-14 18:07:04 -04:00
*
2019-01-14 12:09:45 -05:00
* Q u a n t u m L e a P s
* ------------------------
* Modern Embedded Software
2012-08-14 18:07:04 -04:00
*
2020-03-17 21:33:20 -04:00
* Copyright (C) 2005-2020 Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:07:04 -04:00
*
* This program is open source software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
2012-08-14 18:07:04 -04:00
* (at your option) any later version.
*
* Alternatively, this program may be distributed and modified under the
* terms of Quantum Leaps commercial licenses, which expressly supersede
* the GNU General Public License and are specifically designed for
* licensees interested in retaining the proprietary status of their code.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-10-27 11:57:33 -04:00
* along with this program. If not, see <www.gnu.org/licenses>.
2012-08-14 18:07:04 -04:00
*
* Contact information:
2019-12-31 15:55:08 -05:00
* <www.state-machine.com/licensing>
2019-10-27 11:57:33 -04:00
* <info@state-machine.com>
2014-04-06 11:43:13 -04:00
******************************************************************************
2015-04-28 13:45:35 -04:00
* @endcond
2014-04-06 11:43:13 -04:00
*/
2019-10-27 11:57:33 -04:00
#ifndef QPSET_H
#define QPSET_H
2012-08-14 18:07:04 -04:00
2019-12-31 15:55:08 -05:00
#ifndef QF_MAX_ACTIVE
/* default value when NOT defined */
2020-03-17 21:33:20 -04:00
#define QF_MAX_ACTIVE 32U
2019-12-31 15:55:08 -05:00
#endif
2020-03-17 21:33:20 -04:00
#if (QF_MAX_ACTIVE < 1U) || (64U < QF_MAX_ACTIVE)
#error "QF_MAX_ACTIVE out of range. Valid range is 1U..64U"
#elif (QF_MAX_ACTIVE <= 8U)
2019-01-14 12:09:45 -05:00
typedef uint8_t QPSetBits;
2020-03-17 21:33:20 -04:00
#elif (QF_MAX_ACTIVE <= 16U)
2019-01-14 12:09:45 -05:00
typedef uint16_t QPSetBits;
#else
/*! bitmask for the internal representation of QPSet elements */
typedef uint32_t QPSetBits;
2016-09-29 18:08:33 -04:00
#endif
2020-03-17 21:33:20 -04:00
#if (QF_MAX_ACTIVE <= 32U)
2016-09-23 12:08:21 -04:00
2012-08-14 18:07:04 -04:00
/****************************************************************************/
2016-09-23 12:08:21 -04:00
/*! Priority Set of up to 32 elements */
2014-04-06 11:43:13 -04:00
/**
2012-08-14 18:07:04 -04:00
* The priority set represents the set of active objects that are ready to
* run and need to be considered by the scheduling algorithm. The set is
2016-09-23 12:08:21 -04:00
* capable of storing up to 32 priority levels.
2012-08-14 18:07:04 -04:00
*/
2014-04-06 11:43:13 -04:00
typedef struct {
2019-01-14 12:09:45 -05:00
QPSetBits volatile bits; /*!< bitmask with a bit for each element */
2016-09-23 12:08:21 -04:00
} QPSet;
2012-08-14 18:07:04 -04:00
2016-09-23 12:08:21 -04:00
/*! Makes the priority set @p me_ empty */
2020-03-17 21:33:20 -04:00
#define QPSet_setEmpty(me_) ((me_)->bits = 0U)
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Evaluates to TRUE if the priority set @p me_ is empty */
2020-03-17 21:33:20 -04:00
#define QPSet_isEmpty(me_) ((me_)->bits == 0U)
2016-09-23 12:08:21 -04:00
/*! Evaluates to TRUE if the priority set @p me_ is not empty */
2020-03-17 21:33:20 -04:00
#define QPSet_notEmpty(me_) ((me_)->bits != 0U)
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Evaluates to TRUE if the priority set @p me_ has element @p n_ */
2016-09-23 12:08:21 -04:00
#define QPSet_hasElement(me_, n_) \
2020-03-17 21:33:20 -04:00
(((me_)->bits & ((QPSetBits)1 << ((n_) - 1U))) != 0U)
2012-08-14 18:07:04 -04:00
2020-03-17 21:33:20 -04:00
/*! Insert element @p n_ into the set @p me_, n_= 1U..32U */
2016-09-23 12:08:21 -04:00
#define QPSet_insert(me_, n_) \
2020-03-17 21:33:20 -04:00
((me_)->bits |= ((QPSetBits)1 << ((n_) - 1U)))
2012-08-14 18:07:04 -04:00
2016-09-23 12:08:21 -04:00
/*! Remove element n_ from the set @p me_, n_= 1..32 */
#define QPSet_remove(me_, n_) \
2020-03-17 21:33:20 -04:00
((me_)->bits &= (QPSetBits)(~((QPSetBits)1 << ((n_) - 1U))))
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Find the maximum element in the set, and assign it to n_ */
2015-04-28 13:45:35 -04:00
/** @note if the set @p me_ is empty, @p n_ is set to zero.
2012-08-14 18:07:04 -04:00
*/
2016-09-23 12:08:21 -04:00
#define QPSet_findMax(me_, n_) \
((n_) = QF_LOG2((me_)->bits))
2012-08-14 18:07:04 -04:00
2020-03-17 21:33:20 -04:00
#else /* QF_MAX_ACTIVE > 32U */
2012-08-14 18:07:04 -04:00
/****************************************************************************/
2016-09-23 12:08:21 -04:00
/*! Priority Set of up to 64 elements */
2014-04-06 11:43:13 -04:00
/**
2012-08-14 18:07:04 -04:00
* The priority set represents the set of active objects that are ready to
* run and need to be considered by the scheduling algorithm. The set is
* capable of storing up to 64 priority levels.
*/
2014-04-06 11:43:13 -04:00
typedef struct {
2016-09-29 18:08:33 -04:00
uint32_t volatile bits[2];
2016-09-23 12:08:21 -04:00
} QPSet;
2012-08-14 18:07:04 -04:00
2016-09-23 12:08:21 -04:00
/*! Makes the priority set @p me_ empty */
2020-03-17 21:33:20 -04:00
#define QPSet_setEmpty(me_) do { \
(me_)->bits[0] = 0U; \
(me_)->bits[1] = 0U; \
2019-12-31 15:55:08 -05:00
} while (false)
2012-08-14 18:07:04 -04:00
2016-09-23 12:08:21 -04:00
/*! Evaluates to TRUE if the priority set @p me_ is empty */
2016-09-29 18:08:33 -04:00
/* the following logic avoids UB in volatile access for MISRA compliantce */
2020-03-17 21:33:20 -04:00
#define QPSet_isEmpty(me_) \
(((me_)->bits[0] == 0U) \
? ((me_)->bits[1] == 0U) \
: false)
2012-08-14 18:07:04 -04:00
2016-09-23 12:08:21 -04:00
/*! Evaluates to TRUE if the priority set @p me_ is not empty */
2016-09-29 18:08:33 -04:00
/* the following logic avoids UB in volatile access for MISRA compliantce */
2020-03-17 21:33:20 -04:00
#define QPSet_notEmpty(me_) \
(((me_)->bits[0] != 0U) \
? true \
: ((me_)->bits[1] != 0U))
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Evaluates to TRUE if the priority set @p me_ has element @p n_. */
2020-03-17 21:33:20 -04:00
#define QPSet_hasElement(me_, n_) \
(((n_) <= 32U) \
? (((me_)->bits[0] & ((uint32_t)1 << ((n_) - 1U ))) != 0U) \
: (((me_)->bits[1] & ((uint32_t)1 << ((n_) - 33U))) != 0U))
2016-09-23 12:08:21 -04:00
/*! insert element @p n_ into the set @p me_, n_ = 1..64 */
2020-03-17 21:33:20 -04:00
#define QPSet_insert(me_, n_) do { \
if ((n_) <= 32U) { \
((me_)->bits[0] |= ((uint32_t)1 << ((n_) - 1U ))); \
} \
else { \
((me_)->bits[1] |= ((uint32_t)1 << ((n_) - 33U))); \
} \
2019-12-31 15:55:08 -05:00
} while (false)
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Remove element n_ from the set @p me_, n_= 1..64 */
2019-10-27 11:57:33 -04:00
#define QPSet_remove(me_, n_) do { \
2020-03-17 21:33:20 -04:00
if ((n_) <= 32U) { \
((me_)->bits[0] &= (uint32_t)(~((uint32_t)1 << ((n_) - 1U )))); \
2019-10-27 11:57:33 -04:00
} \
else { \
2020-03-17 21:33:20 -04:00
((me_)->bits[1] &= (uint32_t)(~((uint32_t)1 << ((n_) - 33U)))); \
2019-10-27 11:57:33 -04:00
} \
2019-12-31 15:55:08 -05:00
} while (false)
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Find the maximum element in the set, and assign it to @p n_ */
/** @note if the set @p me_ is empty, @p n_ is set to zero.
2012-08-14 18:07:04 -04:00
*/
2020-03-17 21:33:20 -04:00
#define QPSet_findMax(me_, n_) \
((n_) = ((me_)->bits[1] != 0U) \
? (QF_LOG2((me_)->bits[1]) + 32U) \
2016-09-23 12:08:21 -04:00
: (QF_LOG2((me_)->bits[0])))
#endif /* QF_MAX_ACTIVE */
/****************************************************************************/
/* Log-base-2 calculations ...*/
#ifndef QF_LOG2
2019-01-14 12:09:45 -05:00
uint_fast8_t QF_LOG2(QPSetBits x);
2016-09-23 12:08:21 -04:00
#endif /* QF_LOG2 */
2012-08-14 18:07:04 -04:00
2019-10-27 11:57:33 -04:00
#endif /* QPSET_H */