2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
|
|
|
// 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++ extended (blocking) thread
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2019-10-27 12:26:31 -04:00
|
|
|
#ifndef QXTHREAD_HPP
|
|
|
|
#define QXTHREAD_HPP
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
namespace QP {
|
|
|
|
|
2020-03-17 21:33:58 -04:00
|
|
|
//! no-timeout sepcification when blocking on queues or semaphores
|
|
|
|
static constexpr std::uint_fast16_t QXTHREAD_NO_TIMEOUT = 0U;
|
|
|
|
|
2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
2015-12-31 14:56:37 -05:00
|
|
|
//! Extended (blocking) thread of the QXK preemptive kernel
|
2022-04-19 19:23:30 -04:00
|
|
|
//! @description
|
|
|
|
//! QP::QXThread represents the extended (blocking) thread of the QXK kernel.
|
|
|
|
//! Each blocking thread in the application must be represented by the
|
|
|
|
//! corresponding QP::QXThread instance
|
|
|
|
//!
|
|
|
|
//! @note
|
|
|
|
//! Typically QP::QXThread is instantiated directly in the application code.
|
|
|
|
//! The customization of the thread occurs in the constructor, where you
|
|
|
|
//! provide the thred-handler function as the parameter.
|
|
|
|
//!
|
|
|
|
//! @sa QP::QActive
|
|
|
|
//!
|
|
|
|
//! @usage
|
|
|
|
//! The following example illustrates how to instantiate and use an extended
|
|
|
|
//! thread in your application.
|
|
|
|
//! @include qxk_thread.cpp
|
|
|
|
//!
|
2016-12-01 10:31:49 -05:00
|
|
|
class QXThread : public QActive {
|
2015-12-31 14:56:37 -05:00
|
|
|
public:
|
|
|
|
|
|
|
|
//! public constructor
|
2017-08-04 16:26:43 -04:00
|
|
|
QXThread(QXThreadHandler const handler,
|
2020-03-17 21:33:58 -04:00
|
|
|
std::uint_fast8_t const tickRate = 0U) noexcept;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2016-09-29 19:54:50 -04:00
|
|
|
//! delay (block) the current extended thread for a specified # ticks
|
2020-03-17 21:33:58 -04:00
|
|
|
static bool delay(std::uint_fast16_t const nTicks) noexcept;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
//! cancel the delay
|
2020-03-17 21:33:58 -04:00
|
|
|
bool delayCancel(void) noexcept;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
//! obtain a message from the private message queue (block if no messages)
|
2017-08-21 18:21:15 -04:00
|
|
|
static QEvt const *queueGet(
|
2020-03-17 21:33:58 -04:00
|
|
|
std::uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT) noexcept;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
// virtual function overrides...
|
2020-03-17 21:33:58 -04:00
|
|
|
//! Executes the top-most initial transition in HSM
|
2020-10-01 12:50:17 -04:00
|
|
|
void init(void const * const e,
|
|
|
|
std::uint_fast8_t const qs_id) noexcept override;
|
|
|
|
void init(std::uint_fast8_t const qs_id) noexcept override {
|
|
|
|
this->init(nullptr, qs_id);
|
|
|
|
}
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2020-03-17 21:33:58 -04:00
|
|
|
//! Dispatches an event to HSM
|
2020-10-01 12:50:17 -04:00
|
|
|
void dispatch(QEvt const * const e,
|
|
|
|
std::uint_fast8_t const qs_id) noexcept override;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2016-09-29 19:54:50 -04:00
|
|
|
//! Starts execution of an extended thread and registers the thread
|
2015-12-31 14:56:37 -05:00
|
|
|
//! with the framework.
|
2020-03-17 21:33:58 -04:00
|
|
|
void start(std::uint_fast8_t const prio,
|
|
|
|
QEvt const * * const qSto, std::uint_fast16_t const qLen,
|
|
|
|
void * const stkSto, std::uint_fast16_t const stkSize,
|
|
|
|
void const * const par) override;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
//! Overloaded start function (no initialization event)
|
2020-03-17 21:33:58 -04:00
|
|
|
void start(std::uint_fast8_t const prio,
|
|
|
|
QEvt const * * const qSto, std::uint_fast16_t const qLen,
|
|
|
|
void * const stkSto, std::uint_fast16_t const stkSize) override
|
2015-12-31 14:56:37 -05:00
|
|
|
{
|
2020-03-17 21:33:58 -04:00
|
|
|
this->start(prio, qSto, qLen, stkSto, stkSize, nullptr);
|
2015-12-31 14:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef Q_SPY
|
2016-09-29 19:54:50 -04:00
|
|
|
//! Posts an event @p e directly to the event queue of the extended
|
2015-12-31 14:56:37 -05:00
|
|
|
//! thread @p me using the First-In-First-Out (FIFO) policy.
|
2020-03-17 21:33:58 -04:00
|
|
|
bool post_(QEvt const * const e,
|
|
|
|
std::uint_fast16_t const margin) noexcept override;
|
2015-12-31 14:56:37 -05:00
|
|
|
#else
|
2020-03-17 21:33:58 -04:00
|
|
|
bool post_(QEvt const * const e, std::uint_fast16_t const margin,
|
|
|
|
void const * const sender) noexcept override;
|
2015-12-31 14:56:37 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//! Posts an event directly to the event queue of the active object
|
|
|
|
//! using the Last-In-First-Out (LIFO) policy.
|
2020-03-17 21:33:58 -04:00
|
|
|
void postLIFO(QEvt const * const e) noexcept override;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
private:
|
2020-03-17 21:33:58 -04:00
|
|
|
void block_(void) const noexcept;
|
|
|
|
void unblock_(void) const noexcept;
|
|
|
|
void teArm_(enum_t const sig, std::uint_fast16_t const nTicks) noexcept;
|
|
|
|
bool teDisarm_(void) noexcept;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
// attributes...
|
2019-01-16 19:31:40 -05:00
|
|
|
QTimeEvt m_timeEvt; //!< time event to handle blocking timeouts
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
// friendships...
|
|
|
|
friend class QXSemaphore;
|
2017-08-21 18:21:15 -04:00
|
|
|
friend class QXMutex;
|
2015-12-31 14:56:37 -05:00
|
|
|
};
|
|
|
|
|
2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
2015-12-31 14:56:37 -05:00
|
|
|
//! Counting Semaphore of the QXK preemptive kernel
|
2022-04-19 19:23:30 -04:00
|
|
|
//!
|
|
|
|
//! @description
|
|
|
|
//! QP::QXSemaphore is a blocking mechanism intended primarily for signaling
|
|
|
|
//! @ref QP::QXThread "extended threads". The semaphore is initialized with
|
|
|
|
//! the maximum count (see QP::QXSemaphore::init()), which allows you to
|
|
|
|
//! create a binary semaphore (when the maximum count is 1) and
|
|
|
|
//! counting semaphore when the maximum count is > 1.
|
|
|
|
//!
|
|
|
|
//! @usage
|
|
|
|
//! The following example illustrates how to instantiate and use the semaphore
|
|
|
|
//! in your application.
|
|
|
|
//! @include qxk_sema.cpp
|
|
|
|
//!
|
2015-12-31 14:56:37 -05:00
|
|
|
class QXSemaphore {
|
|
|
|
public:
|
|
|
|
//! initialize the counting semaphore
|
2020-03-17 21:33:58 -04:00
|
|
|
void init(std::uint_fast16_t const count,
|
|
|
|
std::uint_fast16_t const max_count = 0xFFFFU) noexcept;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2017-08-21 18:21:15 -04:00
|
|
|
//! wait (block) on the semaphore
|
2020-03-17 21:33:58 -04:00
|
|
|
bool wait(std::uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT) noexcept;
|
2017-08-21 18:21:15 -04:00
|
|
|
|
|
|
|
//! try wait on the semaphore (non-blocking)
|
2020-03-17 21:33:58 -04:00
|
|
|
bool tryWait(void) noexcept;
|
2017-08-21 18:21:15 -04:00
|
|
|
|
2015-12-31 14:56:37 -05:00
|
|
|
//! signal (unblock) the semaphore
|
2020-03-17 21:33:58 -04:00
|
|
|
bool signal(void) noexcept;
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
private:
|
2016-09-29 19:54:50 -04:00
|
|
|
QPSet m_waitSet; //!< set of extended threads waiting on this semaphore
|
2020-03-17 21:33:58 -04:00
|
|
|
std::uint16_t volatile m_count; //!< semaphore up-down counter
|
|
|
|
std::uint16_t m_max_count; //!< maximum value of the semaphore counter
|
2015-12-31 14:56:37 -05:00
|
|
|
};
|
|
|
|
|
2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
2017-09-29 15:35:30 -04:00
|
|
|
//! Priority Ceiling Mutex the QXK preemptive kernel
|
2022-04-19 19:23:30 -04:00
|
|
|
//!
|
|
|
|
//! @description
|
|
|
|
//! QP::QXMutex is a blocking mutual exclusion mechanism that can also apply
|
|
|
|
//! the **priority ceiling protocol** to avoid unbounded priority inversion
|
|
|
|
//! (if initialized with a non-zero ceiling priority, see QP::QXMutex::init()).
|
|
|
|
//! In that case, QP::QXMutex requires its own uinque QP priority level, which
|
|
|
|
//! cannot be used by any thread or any other QP::QXMutex.
|
|
|
|
//! If initialzied with zero ceiling priority, QP::QXMutex does **not** use
|
|
|
|
//! the priority ceiling protocol and does not require a unique QP priority
|
|
|
|
//! (see QP::QXMutex::init()).
|
|
|
|
//! QP::QXMutex is **recursive** (reentrant), which means that it can be
|
|
|
|
//! locked mutiliple times (up to 255 levels) by the *same* thread without
|
|
|
|
//! causing deadlock.
|
|
|
|
//! QP::QXMutex is primarily intended for the @ref QP::QXThread
|
|
|
|
//! "extened (blocking) threads", but can also be used by the @ref QP::QActive
|
|
|
|
//! "basic threads" through the non-blocking QP::QXMutex::tryLock() API.
|
|
|
|
//!
|
|
|
|
//! @note
|
|
|
|
//! QP::QXMutex should be used in situations when at least one of the extended
|
|
|
|
//! threads contending for the mutex blocks while holding the mutex (between
|
|
|
|
//! the QP::QXMutex::lock() and QP::QXMutex::unlock() operations). If no
|
|
|
|
//! blocking is needed while holding the mutex, the more efficient
|
|
|
|
//! non-blocking mechanism of @ref QP::QXK::schedLock() "selective QXK
|
|
|
|
//! scheduler locking" should be used instead. @ref QP::QXK::schedLock()
|
|
|
|
//! "Selective scheduler locking" is available for both @ref QP::QActive
|
|
|
|
//! "basic threads" and @ref QP::QXThread "extended threads", so it is
|
|
|
|
//! applicable to situations where resources are shared among all
|
|
|
|
//! these threads.
|
|
|
|
//!
|
|
|
|
//! @usage
|
|
|
|
//! The following example illustrates how to instantiate and use the mutex
|
|
|
|
//! in your application.
|
|
|
|
//! @include qxk_mutex.cpp
|
|
|
|
//!
|
2017-08-21 18:21:15 -04:00
|
|
|
class QXMutex {
|
|
|
|
public:
|
|
|
|
//! initialize the QXK priority-ceiling mutex QP::QXMutex
|
2020-03-17 21:33:58 -04:00
|
|
|
void init(std::uint_fast8_t const ceiling) noexcept;
|
2017-08-21 18:21:15 -04:00
|
|
|
|
|
|
|
//! lock the QXK priority-ceiling mutex QP::QXMutex
|
2020-03-17 21:33:58 -04:00
|
|
|
bool lock(std::uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT) noexcept;
|
2017-08-21 18:21:15 -04:00
|
|
|
|
|
|
|
//! try to lock the QXK priority-ceiling mutex QP::QXMutex
|
2020-03-17 21:33:58 -04:00
|
|
|
bool tryLock(void) noexcept;
|
2017-08-21 18:21:15 -04:00
|
|
|
|
|
|
|
//! unlock the QXK priority-ceiling mutex QP::QXMutex
|
2020-03-17 21:33:58 -04:00
|
|
|
void unlock(void) noexcept;
|
2017-08-21 18:21:15 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
QPSet m_waitSet; //!< set of extended-threads waiting on this mutex
|
2020-03-17 21:33:58 -04:00
|
|
|
std::uint8_t volatile m_lockNest; //!< lock-nesting up-down counter
|
|
|
|
std::uint8_t volatile m_holderPrio; //!< prio of the lock holder thread
|
|
|
|
std::uint8_t m_ceiling; //!< prioirty ceiling of this mutex
|
2017-08-21 18:21:15 -04:00
|
|
|
};
|
|
|
|
|
2015-12-31 14:56:37 -05:00
|
|
|
} // namespace QP
|
|
|
|
|
2019-10-27 12:26:31 -04:00
|
|
|
#endif // QXTHREAD_HPP
|