qpcpp/include/qxk.hpp

222 lines
8.2 KiB
C++
Raw Permalink Normal View History

//============================================================================
// QP/C++ Real-Time Embedded Framework (RTEF)
// Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
//
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
//
// This software is dual-licensed under the terms of the open source GNU
// General Public License version 3 (or any later version), or alternatively,
// under the terms of one of the closed source Quantum Leaps commercial
// licenses.
//
// The terms of the open source GNU General Public License version 3
// can be found at: <www.gnu.org/licenses/gpl-3.0>
//
// The terms of the closed source Quantum Leaps commercial licenses
// can be found at: <www.state-machine.com/licensing>
//
// Redistributions in source code must retain this top-level comment block.
// Plagiarizing this software to sidestep the license obligations is illegal.
//
// Contact information:
// <www.state-machine.com>
// <info@state-machine.com>
//============================================================================
//! @date Last updated on: 2021-12-23
//! @version Last updated for: @ref qpcpp_7_0_0
//!
//! @file
//! @brief QXK/C++ preemptive extended (blocking) kernel, platform-independent
//! public interface.
2015-12-31 14:56:37 -05:00
2019-10-27 12:26:31 -04:00
#ifndef QXK_HPP
#define QXK_HPP
2015-12-31 14:56:37 -05:00
2019-10-27 12:26:31 -04:00
#include "qequeue.hpp" // QXK kernel uses the native QF event queue
#include "qmpool.hpp" // QXK kernel uses the native QF memory pool
#include "qpset.hpp" // QXK kernel uses the native QF priority set
2015-12-31 14:56:37 -05:00
//============================================================================
2018-02-18 12:08:21 -05:00
// QF configuration for QXK -- data members of the QActive class...
2015-12-31 14:56:37 -05:00
2020-08-24 16:42:48 -04:00
// QXK event-queue used for AOs
2015-12-31 14:56:37 -05:00
#define QF_EQUEUE_TYPE QEQueue
2020-08-24 16:42:48 -04:00
// QXK OS-object used to store the private stack poiner for extended threads.
// (The private stack pointer is NULL for basic-threads).
2018-02-18 12:08:21 -05:00
//
2017-07-06 13:31:32 -04:00
#define QF_OS_OBJECT_TYPE void*
2020-08-24 16:42:48 -04:00
// QXK thread type used to store the private Thread-Local Storage pointer.
2016-09-29 19:54:50 -04:00
#define QF_THREAD_TYPE void*
2015-12-31 14:56:37 -05:00
2017-07-06 13:31:32 -04:00
//! Access Thread-Local Storage (TLS) and cast it on the given @p type_
#define QXK_TLS(type_) (static_cast<type_>(QXK_current()->m_thread))
//============================================================================
2017-07-06 13:31:32 -04:00
namespace QP {
class QActive; // forward declaration
} // namespace QP
//============================================================================
2015-12-31 14:56:37 -05:00
extern "C" {
//! attributes of the QXK kernel
struct QXK_Attr {
2020-08-24 16:42:48 -04:00
QP::QActive * volatile curr; //!< currently executing thread
QP::QActive * volatile next; //!< next thread to execute
2020-03-17 21:33:58 -04:00
std::uint8_t volatile actPrio; //!< prio of the active basic thread
std::uint8_t volatile lockPrio; //!< lock prio (0 == no-lock)
std::uint8_t volatile lockHolder; //!< prio of the lock holder
std::uint8_t volatile intNest; //!< ISR nesting level
2020-08-24 16:42:48 -04:00
QP::QActive * idleThread; //!< pointer to the idle thread
QP::QPSet readySet; //!< ready-set of all threads
2015-12-31 14:56:37 -05:00
};
//! global attributes of the QXK kernel
extern QXK_Attr QXK_attr_;
2016-09-29 19:54:50 -04:00
//! QXK scheduler finds the highest-priority thread ready to run
2020-03-17 21:33:58 -04:00
std::uint_fast8_t QXK_sched_(void) noexcept;
2016-09-29 19:54:50 -04:00
//! QXK activator activates the next active object. The activated AO preempts
// the currently executing AOs.
//
void QXK_activate_(void);
2017-07-06 13:31:32 -04:00
//! return the currently executing active-object/thread
2020-03-17 21:33:58 -04:00
QP::QActive *QXK_current(void) noexcept;
2017-07-06 13:31:32 -04:00
2018-02-18 12:08:21 -05:00
#ifdef QXK_ON_CONTEXT_SW
//! QXK context switch callback (customized in BSPs for QXK)
//!
//! @description
//! This callback function provides a mechanism to perform additional
//! custom operations when QXK switches context from one thread to
//! another.
//!
//! @param[in] prev pointer to the previous thread (active object)
//! (prev==0 means that @p prev was the QXK idle thread)
//! @param[in] next pointer to the next thread (active object)
//! (next==0) means that @p next is the QXK idle thread)
//! @attention
//! QXK_onContextSw() is invoked with interrupts **disabled** and must also
//! return with interrupts **disabled**.
//!
//! @note
//! This callback is enabled by defining the macro #QXK_ON_CONTEXT_SW.
//!
//! @include qxk_oncontextsw.cpp
//!
2018-02-18 12:08:21 -05:00
void QXK_onContextSw(QP::QActive *prev, QP::QActive *next);
#endif // QXK_ON_CONTEXT_SW
2015-12-31 14:56:37 -05:00
} // extern "C"
//============================================================================
2015-12-31 14:56:37 -05:00
namespace QP {
2017-08-21 18:21:15 -04:00
//! The scheduler lock status
2020-03-17 21:33:58 -04:00
using QSchedStatus = std::uint_fast16_t;
2017-08-21 18:21:15 -04:00
//============================================================================
2015-12-31 14:56:37 -05:00
//! QXK services.
//! @description
//! This class groups together QXK services. It has only static members and
//! should not be instantiated.
//!
//! @note The QXK initialization and the QXK scheduler belong conceptually
//! to the QXK class (as static class members). However, to avoid C++
//! potential name-mangling problems in assembly language, these elements
//! are defined outside of the QXK class and outside the QP namespace with
//! the extern "C" linkage specification.
2015-12-31 14:56:37 -05:00
class QXK {
public:
2017-08-21 18:21:15 -04:00
//! QXK selective scheduler lock
2020-03-17 21:33:58 -04:00
static QSchedStatus schedLock(std::uint_fast8_t const ceiling) noexcept;
2015-12-31 14:56:37 -05:00
2017-08-21 18:21:15 -04:00
//! QXK selective scheduler unlock
2020-03-17 21:33:58 -04:00
static void schedUnlock(QSchedStatus const stat) noexcept;
2015-12-31 14:56:37 -05:00
//! QXK idle callback (customized in BSPs for QXK)
//! @description
//! QP::QXK::onIdle() is called continously by the QXK idle loop. This
//! callback gives the application an opportunity to enter a power-saving
//! CPU mode, or perform some other idle processing.
//!
//! @note QP::QXK::onIdle() is invoked with interrupts enabled and must
//! also return with interrupts enabled.
//!
//! @sa QP::QF::onIdle()
2015-12-31 14:56:37 -05:00
static void onIdle(void);
};
} // namespace QP
//============================================================================
2015-12-31 14:56:37 -05:00
// interface used only inside QF, but not in applications
#ifdef QP_IMPL
#ifndef QXK_ISR_CONTEXT_
//! Internal port-specific macro that reports the execution context
// (ISR vs. thread).
//! @returns true if the code executes in the ISR context and false
//! otherwise
2015-12-31 14:56:37 -05:00
#define QXK_ISR_CONTEXT_() \
2020-03-17 21:33:58 -04:00
(QXK_attr_.intNest != 0U)
2015-12-31 14:56:37 -05:00
#endif // QXK_ISR_CONTEXT_
2016-09-29 19:54:50 -04:00
// QXK-specific scheduler locking
//! Internal macro to represent the scheduler lock status
// that needs to be preserved to allow nesting of locks.
//
2017-08-21 18:21:15 -04:00
#define QF_SCHED_STAT_ QSchedStatus lockStat_;
2016-09-29 19:54:50 -04:00
//! Internal macro for selective scheduler locking.
2020-03-17 21:33:58 -04:00
#define QF_SCHED_LOCK_(prio_) do { \
if (QXK_ISR_CONTEXT_()) { \
lockStat_ = 0xFFU; \
} else { \
2017-08-21 18:21:15 -04:00
lockStat_ = QXK::schedLock((prio_)); \
2020-03-17 21:33:58 -04:00
} \
2016-09-29 19:54:50 -04:00
} while (false)
//! Internal macro for selective scheduler unlocking.
2020-03-17 21:33:58 -04:00
#define QF_SCHED_UNLOCK_() do { \
if (lockStat_ != 0xFFU) { \
2017-08-21 18:21:15 -04:00
QXK::schedUnlock(lockStat_); \
2020-03-17 21:33:58 -04:00
} \
2016-09-29 19:54:50 -04:00
} while (false)
2016-04-01 13:55:34 -04:00
2016-10-08 12:46:03 -04:00
// QXK-specific native event queue operations...
2015-12-31 14:56:37 -05:00
#define QACTIVE_EQUEUE_WAIT_(me_) \
2020-03-17 21:33:58 -04:00
Q_ASSERT_ID(110, (me_)->m_eQueue.m_frontEvt != nullptr)
2020-10-01 12:50:17 -04:00
#define QACTIVE_EQUEUE_SIGNAL_(me_) do { \
QXK_attr_.readySet.insert( \
static_cast<std::uint_fast8_t>((me_)->m_dynPrio)); \
if (!QXK_ISR_CONTEXT_()) { \
if (QXK_sched_() != 0U) { \
QXK_activate_(); \
} \
} \
2015-12-31 14:56:37 -05:00
} while (false)
2016-10-08 12:46:03 -04:00
// QXK-specific native QF event pool operations...
2015-12-31 14:56:37 -05:00
#define QF_EPOOL_TYPE_ QMPool
#define QF_EPOOL_INIT_(p_, poolSto_, poolSize_, evtSize_) \
(p_).init((poolSto_), (poolSize_), (evtSize_))
#define QF_EPOOL_EVENT_SIZE_(p_) ((p_).m_blockSize)
2020-10-01 12:50:17 -04:00
#define QF_EPOOL_GET_(p_, e_, m_, qs_id_) \
((e_) = static_cast<QEvt *>((p_).get((m_), (qs_id_))))
#define QF_EPOOL_PUT_(p_, e_, qs_id_) ((p_).put((e_), (qs_id_)))
2015-12-31 14:56:37 -05:00
#endif // QP_IMPL
2019-10-27 12:26:31 -04:00
#endif // QXK_HPP