qpcpp/ports/posix-qv/qp_port.hpp

155 lines
5.8 KiB
C++
Raw Permalink Normal View History

//============================================================================
// QP/C++ Real-Time Embedded Framework (RTEF)
//
2024-10-15 13:15:28 -04:00
// Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
//
// Q u a n t u m L e a P s
// ------------------------
// Modern Embedded Software
//
2022-08-11 15:36:19 -04:00
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
//
2025-01-18 18:33:40 -05:00
// This software is dual-licensed under the terms of the open-source GNU
2024-10-15 13:15:28 -04:00
// General Public License (GPL) or under the terms of one of the closed-
// source Quantum Leaps commercial licenses.
2022-08-11 15:36:19 -04:00
//
// Redistributions in source code must retain this top-level comment block.
// Plagiarizing this software to sidestep the license obligations is illegal.
//
2024-10-15 13:15:28 -04:00
// NOTE:
// The GPL (see <www.gnu.org/licenses/gpl-3.0>) does NOT permit the
// incorporation of the QP/C software into proprietary programs. Please
// contact Quantum Leaps for commercial licensing options, which expressly
// supersede the GPL and are designed explicitly for licensees interested
// in using QP/C in closed-source proprietary applications.
//
// Quantum Leaps contact information:
2022-08-11 15:36:19 -04:00
// <www.state-machine.com/licensing>
// <info@state-machine.com>
//============================================================================
#ifndef QP_PORT_HPP_
#define QP_PORT_HPP_
2018-04-05 17:04:31 -04:00
2024-10-15 13:15:28 -04:00
#include <cstdint> // Exact-width types. C++11 Standard
#include "qp_config.hpp" // QP configuration from the application
2018-04-05 17:04:31 -04:00
// no-return function specifier (C++11 Standard)
#define Q_NORETURN [[ noreturn ]] void
2018-04-05 17:04:31 -04:00
2024-02-08 21:09:29 -05:00
// QActive event queue and thread types for POSIX-QV
#define QACTIVE_EQUEUE_TYPE QEQueue
//QACTIVE_OS_OBJ_TYPE not used in this port
//QACTIVE_THREAD_TYPE not used in this port
2020-07-18 17:58:58 -04:00
// QF critical section for POSIX-QV, see NOTE1
#define QF_CRIT_STAT
#define QF_CRIT_ENTRY() QP::QF::enterCriticalSection_()
#define QF_CRIT_EXIT() QP::QF::leaveCriticalSection_()
2018-04-05 17:04:31 -04:00
// QF_LOG2 not defined -- use the internal LOG2() implementation
namespace QP {
2022-08-11 15:36:19 -04:00
namespace QF {
2018-04-05 17:04:31 -04:00
// internal functions for critical section management
void enterCriticalSection_();
void leaveCriticalSection_();
2018-11-22 11:34:13 -05:00
2018-06-16 17:21:53 -04:00
// set clock tick rate and p-thread priority
// (NOTE ticksPerSec==0 disables the "ticker thread"
2024-02-08 21:09:29 -05:00
void setTickRate(std::uint32_t ticksPerSec, int tickPrio);
2018-04-05 17:04:31 -04:00
// clock tick callback (NOTE not called when "ticker thread" is not running)
void onClockTick();
2018-04-05 17:04:31 -04:00
2024-02-08 21:09:29 -05:00
#ifdef QF_CONSOLE
// abstractions for console access...
void consoleSetup();
void consoleCleanup();
int consoleGetKey();
int consoleWaitForKey();
#endif
2018-10-25 11:13:01 -04:00
2022-08-11 15:36:19 -04:00
} // namespace QF
2018-04-05 17:04:31 -04:00
} // namespace QP
// include files -------------------------------------------------------------
#include "qequeue.hpp" // POSIX-QV port needs the native event-queue
#include "qmpool.hpp" // POSIX-QV port needs the native memory-pool
#include "qp.hpp" // QP platform-independent public interface
//============================================================================
// interface used only inside QF implementation, but not in applications
2022-08-11 15:36:19 -04:00
2018-04-05 17:04:31 -04:00
#ifdef QP_IMPL
// QF scheduler locking for POSIX-QV (not needed in single-thread port)
2018-04-05 17:04:31 -04:00
#define QF_SCHED_STAT_
2022-08-11 15:36:19 -04:00
#define QF_SCHED_LOCK_(dummy) (static_cast<void>(0))
#define QF_SCHED_UNLOCK_() (static_cast<void>(0))
2018-04-05 17:04:31 -04:00
// QF event queue customization for POSIX-QV...
2024-10-15 13:15:28 -04:00
#define QACTIVE_EQUEUE_WAIT_(me_) (static_cast<void>(0))
2018-04-05 17:04:31 -04:00
#define QACTIVE_EQUEUE_SIGNAL_(me_) \
QF::readySet_.insert((me_)->m_prio); \
pthread_cond_signal(&QP::QF::condVar_)
2018-04-05 17:04:31 -04:00
2022-08-11 15:36:19 -04:00
// native QF event pool operations
2018-04-05 17:04:31 -04: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_).getBlockSize())
2024-01-17 07:50:09 -05:00
#define QF_EPOOL_GET_(p_, e_, m_, qsId_) \
((e_) = static_cast<QEvt *>((p_).get((m_), (qsId_))))
#define QF_EPOOL_PUT_(p_, e_, qsId_) ((p_).put((e_), (qsId_)))
2018-04-05 17:04:31 -04:00
#include <pthread.h> // POSIX-thread API
2018-11-22 11:34:13 -05:00
namespace QP {
namespace QF {
extern QPSet readySet_;
extern QPSet readySet_dis_;
extern pthread_cond_t condVar_; // Cond.var. to signal events
} // namespace QF
} // namespace QP
2018-04-05 17:04:31 -04:00
#endif // QP_IMPL
//============================================================================
2018-04-05 17:04:31 -04:00
// NOTE1:
// QP, like all real-time frameworks, needs to execute certain sections of
2018-11-22 11:34:13 -05:00
// code exclusively, meaning that only one thread can execute the code at
// the time. Such sections of code are called "critical sections".
2018-04-05 17:04:31 -04:00
//
2022-08-11 15:36:19 -04:00
// This port uses a pair of functions QF::enterCriticalSection_() /
2024-02-08 21:09:29 -05:00
// QF::leaveCriticalSection_() to enter/leave the critical section,
2018-11-22 11:34:13 -05:00
// respectively.
2018-04-05 17:04:31 -04:00
//
2022-08-11 15:36:19 -04:00
// These functions are implemented in the qf_port.cpp module, where they
// manipulate the file-scope POSIX mutex object QF::pThreadMutex_
2024-01-17 07:50:09 -05:00
// to protect all critical sections. Using the single mutex for all critical
2018-11-22 11:34:13 -05:00
// section guarantees that only one thread at a time can execute inside a
// critical section. This prevents race conditions and data corruption.
2018-04-05 17:04:31 -04:00
//
2018-11-22 11:34:13 -05:00
// Please note, however, that the POSIX mutex implementation behaves
// differently than interrupt disabling. A common POSIX mutex ensures
// that only one thread at a time can execute a critical section, but it
// does not guarantee that a context switch cannot occur within the
// critical section. In fact, such context switches probably will happen,
// but they should not cause concurrency hazards because the critical
// section eliminates all race conditionis.
2018-04-05 17:04:31 -04:00
//
// Unlinke simply disabling and enabling interrupts, the mutex approach is
// also subject to priority inversions. However, the p-thread mutex
2018-11-22 11:34:13 -05:00
// implementation, such as POSIX threads, should support the priority-
2018-04-05 17:04:31 -04:00
// inheritance protocol.
//
// NOTE2:
// Scheduler locking (used inside QActive::publish()) is not needed in the
2024-02-08 21:09:29 -05:00
// single-threaded port because event multicasting is already atomic.
//
2018-04-05 17:04:31 -04:00
#endif // QP_PORT_HPP_
2018-04-05 17:04:31 -04:00