QP/C++  7.0.1
Real-Time Embedded Framework
qxthread.hpp
Go to the documentation of this file.
1//============================================================================
2// QP/C++ Real-Time Embedded Framework (RTEF)
3// Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
4//
5// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
6//
7// This software is dual-licensed under the terms of the open source GNU
8// General Public License version 3 (or any later version), or alternatively,
9// under the terms of one of the closed source Quantum Leaps commercial
10// licenses.
11//
12// The terms of the open source GNU General Public License version 3
13// can be found at: <www.gnu.org/licenses/gpl-3.0>
14//
15// The terms of the closed source Quantum Leaps commercial licenses
16// can be found at: <www.state-machine.com/licensing>
17//
18// Redistributions in source code must retain this top-level comment block.
19// Plagiarizing this software to sidestep the license obligations is illegal.
20//
21// Contact information:
22// <www.state-machine.com>
23// <info@state-machine.com>
24//============================================================================
30
31#ifndef QXTHREAD_HPP
32#define QXTHREAD_HPP
33
34namespace QP {
35
37static constexpr std::uint_fast16_t QXTHREAD_NO_TIMEOUT = 0U;
38
39//============================================================================
58class QXThread : public QActive {
59public:
60
62 QXThread(QXThreadHandler const handler,
63 std::uint_fast8_t const tickRate = 0U) noexcept;
64
66 static bool delay(std::uint_fast16_t const nTicks) noexcept;
67
69 bool delayCancel(void) noexcept;
70
72 static QEvt const *queueGet(
73 std::uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT) noexcept;
74
75 // virtual function overrides...
77 void init(void const * const e,
78 std::uint_fast8_t const qs_id) noexcept override;
79 void init(std::uint_fast8_t const qs_id) noexcept override {
80 this->init(nullptr, qs_id);
81 }
82
84 void dispatch(QEvt const * const e,
85 std::uint_fast8_t const qs_id) noexcept override;
86
89 void start(std::uint_fast8_t const prio,
90 QEvt const * * const qSto, std::uint_fast16_t const qLen,
91 void * const stkSto, std::uint_fast16_t const stkSize,
92 void const * const par) override;
93
95 void start(std::uint_fast8_t const prio,
96 QEvt const * * const qSto, std::uint_fast16_t const qLen,
97 void * const stkSto, std::uint_fast16_t const stkSize) override
98 {
99 this->start(prio, qSto, qLen, stkSto, stkSize, nullptr);
100 }
101
102#ifndef Q_SPY
105 bool post_(QEvt const * const e,
106 std::uint_fast16_t const margin) noexcept override;
107#else
108 bool post_(QEvt const * const e, std::uint_fast16_t const margin,
109 void const * const sender) noexcept override;
110#endif
111
114 void postLIFO(QEvt const * const e) noexcept override;
115
116private:
117 void block_(void) const noexcept;
118 void unblock_(void) const noexcept;
119 void teArm_(enum_t const sig, std::uint_fast16_t const nTicks) noexcept;
120 bool teDisarm_(void) noexcept;
121
122 // attributes...
124
125 // friendships...
126 friend class QXSemaphore;
127 friend class QXMutex;
128};
129
130//============================================================================
146public:
148 void init(std::uint_fast16_t const count,
149 std::uint_fast16_t const max_count = 0xFFFFU) noexcept;
150
152 bool wait(std::uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT) noexcept;
153
155 bool tryWait(void) noexcept;
156
158 bool signal(void) noexcept;
159
160private:
162 std::uint16_t volatile m_count;
163 std::uint16_t m_max_count;
164};
165
166//============================================================================
202class QXMutex {
203public:
205 void init(std::uint_fast8_t const ceiling) noexcept;
206
208 bool lock(std::uint_fast16_t const nTicks = QXTHREAD_NO_TIMEOUT) noexcept;
209
211 bool tryLock(void) noexcept;
212
214 void unlock(void) noexcept;
215
216private:
218 std::uint8_t volatile m_lockNest;
219 std::uint8_t volatile m_holderPrio;
220 std::uint8_t m_ceiling;
221};
222
223} // namespace QP
224
225#endif // QXTHREAD_HPP
QActive active object (based on QP::QHsm implementation)
Definition: qf.hpp:136
friend class QXThread
Definition: qep.hpp:455
Time Event class.
Definition: qf.hpp:395
Priority Ceiling Mutex the QXK preemptive kernel.
Definition: qxthread.hpp:202
std::uint8_t volatile m_lockNest
lock-nesting up-down counter
Definition: qxthread.hpp:218
bool tryLock(void) noexcept
try to lock the QXK priority-ceiling mutex QP::QXMutex
Definition: qxk_mutex.cpp:254
std::uint8_t m_ceiling
prioirty ceiling of this mutex
Definition: qxthread.hpp:220
QPSet m_waitSet
set of extended-threads waiting on this mutex
Definition: qxthread.hpp:217
void init(std::uint_fast8_t const ceiling) noexcept
initialize the QXK priority-ceiling mutex QP::QXMutex
Definition: qxk_mutex.cpp:82
void unlock(void) noexcept
unlock the QXK priority-ceiling mutex QP::QXMutex
Definition: qxk_mutex.cpp:341
std::uint8_t volatile m_holderPrio
prio of the lock holder thread
Definition: qxthread.hpp:219
bool lock(std::uint_fast16_t const nTicks=QXTHREAD_NO_TIMEOUT) noexcept
lock the QXK priority-ceiling mutex QP::QXMutex
Definition: qxk_mutex.cpp:127
Counting Semaphore of the QXK preemptive kernel.
Definition: qxthread.hpp:145
bool wait(std::uint_fast16_t const nTicks=QXTHREAD_NO_TIMEOUT) noexcept
wait (block) on the semaphore
Definition: qxk_sema.cpp:108
bool signal(void) noexcept
signal (unblock) the semaphore
Definition: qxk_sema.cpp:231
QPSet m_waitSet
set of extended threads waiting on this semaphore
Definition: qxthread.hpp:161
std::uint16_t m_max_count
maximum value of the semaphore counter
Definition: qxthread.hpp:163
void init(std::uint_fast16_t const count, std::uint_fast16_t const max_count=0xFFFFU) noexcept
initialize the counting semaphore
Definition: qxk_sema.cpp:75
std::uint16_t volatile m_count
semaphore up-down counter
Definition: qxthread.hpp:162
bool tryWait(void) noexcept
try wait on the semaphore (non-blocking)
Definition: qxk_sema.cpp:192
Extended (blocking) thread of the QXK preemptive kernel.
Definition: qxthread.hpp:58
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
Starts execution of an extended thread and registers the thread with the framework.
Definition: qxk_xthr.cpp:132
void block_(void) const noexcept
Definition: qxk_xthr.cpp:470
void init(void const *const e, std::uint_fast8_t const qs_id) noexcept override
Executes the top-most initial transition in HSM.
Definition: qxk_xthr.cpp:94
void postLIFO(QEvt const *const e) noexcept override
Posts an event directly to the event queue of the active object using the Last-In-First-Out (LIFO) po...
Definition: qxk_xthr.cpp:352
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
Overloaded start function (no initialization event)
Definition: qxthread.hpp:95
static bool delay(std::uint_fast16_t const nTicks) noexcept
delay (block) the current extended thread for a specified # ticks
Definition: qxk_xthr.cpp:576
void init(std::uint_fast8_t const qs_id) noexcept override
overloaded init(qs_id)
Definition: qxthread.hpp:79
bool delayCancel(void) noexcept
cancel the delay
Definition: qxk_xthr.cpp:623
bool post_(QEvt const *const e, std::uint_fast16_t const margin, void const *const sender) noexcept override
Definition: qxk_xthr.cpp:210
void dispatch(QEvt const *const e, std::uint_fast8_t const qs_id) noexcept override
Dispatches an event to HSM.
Definition: qxk_xthr.cpp:103
QTimeEvt m_timeEvt
time event to handle blocking timeouts
Definition: qxthread.hpp:123
void unblock_(void) const noexcept
Definition: qxk_xthr.cpp:484
static QEvt const * queueGet(std::uint_fast16_t const nTicks=QXTHREAD_NO_TIMEOUT) noexcept
obtain a message from the private message queue (block if no messages)
Definition: qxk_xthr.cpp:377
void teArm_(enum_t const sig, std::uint_fast16_t const nTicks) noexcept
Definition: qxk_xthr.cpp:502
bool teDisarm_(void) noexcept
Definition: qxk_xthr.cpp:547
namespace associated with the QP/C++ framework
Definition: exa_native.dox:1
void(*)(QXThread *const me) QXThreadHandler
Pointer to a thread-handler function.
Definition: qep.hpp:214
static constexpr std::uint_fast16_t QXTHREAD_NO_TIMEOUT
no-timeout sepcification when blocking on queues or semaphores
Definition: qxthread.hpp:37
int enum_t
alias for enumerations used for event signals
Definition: qep.hpp:66
QEvt base class.
Definition: qep.hpp:191
Priority Set of up to 32 elements *‍/.
Definition: qpset.hpp:68