qpcpp/include/qxthread.hpp

244 lines
9.1 KiB
C++
Raw Normal View History

2015-12-31 14:56:37 -05:00
/// @file
2016-09-29 19:54:50 -04:00
/// @brief QXK/C++ extended (blocking) thread
2015-12-31 14:56:37 -05:00
/// @ingroup qxk
/// @cond
///***************************************************************************
2019-10-27 12:26:31 -04:00
/// Last updated for version 6.6.0
/// Last updated on 2019-09-12
2015-12-31 14:56:37 -05:00
///
2018-11-22 11:34:13 -05:00
/// Q u a n t u m L e a P s
/// ------------------------
/// Modern Embedded Software
2015-12-31 14:56:37 -05:00
///
2019-10-27 12:26:31 -04:00
/// Copyright (C) 2005-2019 Quantum Leaps. All rights reserved.
2015-12-31 14:56:37 -05: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
/// (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 12:26:31 -04:00
/// along with this program. If not, see <www.gnu.org/licenses>.
2015-12-31 14:56:37 -05:00
///
/// Contact information:
2019-10-27 12:26:31 -04:00
/// <www.state-machine.com>
/// <info@state-machine.com>
2015-12-31 14:56:37 -05:00
///***************************************************************************
/// @endcond
2019-10-27 12:26:31 -04:00
#ifndef QXTHREAD_HPP
#define QXTHREAD_HPP
2015-12-31 14:56:37 -05:00
2017-08-21 18:21:15 -04:00
//! no-timeout sepcification when blocking on queues or semaphores
#define QXTHREAD_NO_TIMEOUT (static_cast<uint_fast16_t>(0))
2015-12-31 14:56:37 -05:00
namespace QP {
2016-09-29 19:54:50 -04:00
class QXThread; // forward declaration
2016-04-01 13:55:34 -04:00
//! Thread handler pointer-to-function
2016-09-29 19:54:50 -04:00
typedef void (*QXThreadHandler)(QXThread * const me);
2016-04-01 13:55:34 -04:00
2015-12-31 14:56:37 -05:00
//****************************************************************************
//! Extended (blocking) thread of the QXK preemptive kernel
/// @description
2016-09-29 19:54:50 -04:00
/// QP::QXThread represents the extended (blocking) thread of the QXK kernel.
2015-12-31 14:56:37 -05:00
/// 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.
///
2016-12-01 10:31:49 -05:00
/// @sa QP::QActive
2015-12-31 14:56:37 -05:00
///
/// @usage
2019-01-16 19:31:40 -05:00
/// The following example illustrates how to instantiate and use an extended
/// thread in your application.
/// @include qxk_thread.cpp
2015-12-31 14:56:37 -05:00
///
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,
2017-07-28 22:54:35 -04:00
uint_fast8_t const tickRate = static_cast<uint_fast8_t>(0));
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
2017-08-21 18:21:15 -04:00
static bool delay(uint_fast16_t const nTicks);
2015-12-31 14:56:37 -05:00
//! cancel the delay
bool delayCancel(void);
//! obtain a message from the private message queue (block if no messages)
2017-08-21 18:21:15 -04:00
static QEvt const *queueGet(
uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT);
2015-12-31 14:56:37 -05:00
// virtual function overrides...
//! Executes the top-most initial transition in QP::QMsm.
2019-10-27 12:26:31 -04:00
virtual void init(void const * const par);
2015-12-31 14:56:37 -05:00
virtual void init(void) { this->init(static_cast<QEvt const *>(0)); }
//! Dispatches an event to QMsm
virtual void dispatch(QEvt const * const e);
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.
virtual void start(uint_fast8_t const prio,
QEvt const *qSto[], uint_fast16_t const qLen,
void * const stkSto, uint_fast16_t const stkSize,
2019-10-27 12:26:31 -04:00
void const * const par);
2015-12-31 14:56:37 -05:00
//! Overloaded start function (no initialization event)
virtual void start(uint_fast8_t const prio,
QEvt const *qSto[], uint_fast16_t const qLen,
void * const stkSto, uint_fast16_t const stkSize)
{
this->start(prio, qSto, qLen, stkSto, stkSize,
static_cast<QEvt const *>(0));
}
#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.
virtual bool post_(QEvt const * const e, uint_fast16_t const margin);
#else
virtual bool post_(QEvt const * const e, uint_fast16_t const margin,
void const * const sender);
#endif
//! Posts an event directly to the event queue of the active object
//! using the Last-In-First-Out (LIFO) policy.
virtual void postLIFO(QEvt const * const e);
2017-12-12 17:03:36 -05:00
//! get the blocking object for this thread (NULL if not blocked)
2018-11-22 11:34:13 -05:00
bool isBlockedOn(void const * const obj
= static_cast<void const *>(0)) const
{
return reinterpret_cast<void const *>(m_temp.obj) == obj;
2017-12-12 17:03:36 -05:00
}
2015-12-31 14:56:37 -05:00
private:
void block_(void) const;
void unblock_(void) const;
2017-08-21 18:21:15 -04:00
void teArm_(enum_t const sig, uint_fast16_t const nTicks);
2015-12-31 14:56:37 -05:00
bool teDisarm_(void);
// 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
};
//****************************************************************************
//! Counting Semaphore of the QXK preemptive kernel
2017-09-29 15:35:30 -04:00
///
/// @description
2019-03-28 11:59:31 -04:00
/// 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
2017-09-29 15:35:30 -04:00
/// counting semaphore when the maximum count is > 1.
///
/// @usage
2019-01-16 19:31:40 -05:00
/// The following example illustrates how to instantiate and use the semaphore
2017-09-29 15:35:30 -04:00
/// in your application.
2019-01-16 19:31:40 -05:00
/// @include qxk_sema.cpp
2017-09-29 15:35:30 -04:00
///
2015-12-31 14:56:37 -05:00
class QXSemaphore {
public:
//! initialize the counting semaphore
2017-07-28 22:54:35 -04:00
void init(uint_fast16_t const count,
2017-08-04 16:26:43 -04:00
uint_fast16_t const max_count
= static_cast<uint_fast16_t>(0xFFFF));
2015-12-31 14:56:37 -05:00
2017-08-21 18:21:15 -04:00
//! wait (block) on the semaphore
bool wait(uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT);
//! try wait on the semaphore (non-blocking)
bool tryWait(void);
2015-12-31 14:56:37 -05:00
//! signal (unblock) the semaphore
2017-07-28 22:54:35 -04:00
bool signal(void);
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
2019-01-16 19:31:40 -05:00
uint16_t volatile m_count; //!< semaphore up-down counter
uint16_t m_max_count; //!< maximum value of the semaphore counter
2015-12-31 14:56:37 -05:00
};
2017-08-21 18:21:15 -04:00
//****************************************************************************
2017-09-29 15:35:30 -04:00
//! Priority Ceiling Mutex the QXK preemptive kernel
2017-08-21 18:21:15 -04:00
///
/// @description
2019-03-28 11:59:31 -04:00
/// QP::QXMutex is a blocking mutual exclusion mechanism that can also apply
2017-09-29 15:35:30 -04:00
/// the **priority ceiling protocol** to avoid unbounded priority inversion
2019-03-28 11:59:31 -04:00
/// (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.
2017-08-21 18:21:15 -04:00
///
/// @note
2019-03-28 11:59:31 -04:00
/// QP::QXMutex should be used in situations when at least one of the extended
2017-08-21 18:21:15 -04:00
/// threads contending for the mutex blocks while holding the mutex (between
2019-03-28 11:59:31 -04:00
/// 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.
2017-08-21 18:21:15 -04:00
///
/// @usage
2019-01-16 19:31:40 -05:00
/// The following example illustrates how to instantiate and use the mutex
2017-08-21 18:21:15 -04:00
/// in your application.
2019-01-16 19:31:40 -05:00
/// @include qxk_mutex.cpp
2017-08-21 18:21:15 -04:00
///
class QXMutex {
public:
//! initialize the QXK priority-ceiling mutex QP::QXMutex
2017-11-12 21:35:04 -05:00
void init(uint_fast8_t const ceiling);
2017-08-21 18:21:15 -04:00
//! lock the QXK priority-ceiling mutex QP::QXMutex
bool lock(uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT);
//! try to lock the QXK priority-ceiling mutex QP::QXMutex
bool tryLock(void);
//! unlock the QXK priority-ceiling mutex QP::QXMutex
void unlock(void);
private:
QPSet m_waitSet; //!< set of extended-threads waiting on this mutex
2019-01-16 19:31:40 -05:00
uint8_t volatile m_lockNest; //!< lock-nesting up-down counter
uint8_t volatile m_holderPrio; //!< priority of the lock holder thread
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
2015-12-31 14:56:37 -05:00