qpcpp/ports/posix/qf_port.cpp

236 lines
9.5 KiB
C++
Raw Normal View History

2015-05-14 16:05:04 -04:00
/// @file
/// @brief QF/C++ port to POSIX/P-threads
/// @cond
///***************************************************************************
2017-02-07 19:55:33 -05:00
/// Last updated for version 5.8.2
/// Last updated on 2016-12-22
2015-05-14 16:05:04 -04:00
///
/// Q u a n t u m L e a P s
/// ---------------------------
/// innovating embedded systems
///
/// Copyright (C) Quantum Leaps, www.state-machine.com.
///
/// 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
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// Contact information:
2017-05-17 13:15:09 -04:00
/// https://state-machine.com
2016-02-10 16:27:39 -05:00
/// mailto:info@state-machine.com
2015-05-14 16:05:04 -04:00
///***************************************************************************
/// @endcond
2014-04-13 21:35:34 -04:00
#define QP_IMPL // this is QP implementation
#include "qf_port.h" // QF port
2012-08-14 18:00:48 -04:00
#include "qf_pkg.h"
#include "qassert.h"
2014-04-13 21:35:34 -04:00
#ifdef Q_SPY // QS software tracing enabled?
#include "qs_port.h" // include QS port
#else
#include "qs_dummy.h" // disable the QS software tracing
#endif // Q_SPY
#include <limits.h> // for PTHREAD_STACK_MIN
#include <sys/mman.h> // for mlockall()
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
namespace QP {
2012-08-14 18:00:48 -04:00
Q_DEFINE_THIS_MODULE("qf_port")
2015-05-14 16:05:04 -04:00
// Global-scope objects ------------------------------------------------------
2016-06-10 21:51:18 -04:00
pthread_mutex_t QF_pThreadMutex_;
2012-08-14 18:00:48 -04:00
// Local-scope objects -------------------------------------------------------
static bool l_running;
2016-04-29 08:52:53 -04:00
static struct timespec l_tick;
enum { NANOSLEEP_NSEC_PER_SEC = 1000000000 }; // see NOTE05
2012-08-14 18:00:48 -04:00
2015-05-14 16:05:04 -04:00
static void *ao_thread(void *arg); // thread routine for all AOs
2012-08-14 18:00:48 -04:00
//............................................................................
void QF::init(void) {
2014-04-13 21:35:34 -04:00
// lock memory so we're never swapped out to disk
//mlockall(MCL_CURRENT | MCL_FUTURE); // uncomment when supported
2016-02-10 16:27:39 -05:00
2016-06-10 21:51:18 -04:00
// init the global mutex with the default non-recursive initializer
pthread_mutex_init(&QF_pThreadMutex_, NULL);
2016-02-10 16:27:39 -05:00
// clear the internal QF variables, so that the framework can (re)start
// correctly even if the startup code is not called to clear the
// uninitialized data (as is required by the C++ Standard).
extern uint_fast8_t QF_maxPool_;
QF_maxPool_ = static_cast<uint_fast8_t>(0);
bzero(&QF::timeEvtHead_[0],
static_cast<uint_fast16_t>(sizeof(QF::timeEvtHead_)));
bzero(&active_[0], static_cast<uint_fast16_t>(sizeof(active_)));
2016-04-29 08:52:53 -04:00
l_tick.tv_sec = 0;
l_tick.tv_nsec = NANOSLEEP_NSEC_PER_SEC/100L; // default clock tick
2012-08-14 18:00:48 -04:00
}
//............................................................................
2013-12-30 17:41:15 -05:00
int_t QF::run(void) {
2014-04-13 21:35:34 -04:00
onStartup(); // invoke startup callback
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
// try to maximize the priority of this thread, see NOTE01
2015-05-14 16:05:04 -04:00
struct sched_param sparam;
2012-08-14 18:00:48 -04:00
sparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sparam) == 0) {
2014-04-13 21:35:34 -04:00
// success, this application has sufficient privileges
2012-08-14 18:00:48 -04:00
}
else {
2014-04-13 21:35:34 -04:00
// setting priority failed, probably due to insufficient privieges
2012-08-14 18:00:48 -04:00
}
l_running = true;
2016-04-29 08:52:53 -04:00
while (l_running) { // the clock tick loop...
2014-04-13 21:35:34 -04:00
QF_onClockTick(); // clock tick callback (must call QF_TICK_X())
2012-08-14 18:00:48 -04:00
2016-04-29 08:52:53 -04:00
nanosleep(&l_tick, NULL); // sleep for the number of ticks, NOTE05
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
onCleanup(); // invoke cleanup callback
2012-08-14 18:00:48 -04:00
pthread_mutex_destroy(&QF_pThreadMutex_);
2014-04-13 21:35:34 -04:00
return static_cast<int_t>(0); // return success
2012-08-14 18:00:48 -04:00
}
//............................................................................
void QF_setTickRate(uint32_t ticksPerSec) {
2016-04-29 08:52:53 -04:00
l_tick.tv_nsec = NANOSLEEP_NSEC_PER_SEC / ticksPerSec;
2012-08-14 18:00:48 -04:00
}
//............................................................................
void QF::stop(void) {
2014-04-13 21:35:34 -04:00
l_running = false; // stop the loop in QF::run()
2012-08-14 18:00:48 -04:00
}
//............................................................................
2016-12-01 10:31:49 -05:00
void QF::thread_(QActive *act) {
2014-04-13 21:35:34 -04:00
// loop until m_thread is cleared in QActive::stop()
do {
QEvt const *e = act->get_(); // wait for event
act->dispatch(e); // dispatch to the active object's state machine
gc(e); // check if the event is garbage, and collect it if so
2012-08-14 18:00:48 -04:00
} while (act->m_thread != static_cast<uint8_t>(0));
2014-04-13 21:35:34 -04:00
2017-02-07 19:55:33 -05:00
QF::remove_(act); // remove this object from the framework
2014-04-13 21:35:34 -04:00
pthread_cond_destroy(&act->m_osObject); // cleanup the condition variable
2012-08-14 18:00:48 -04:00
}
//............................................................................
2016-12-01 10:31:49 -05:00
void QActive::start(uint_fast8_t prio,
2015-05-14 16:05:04 -04:00
QEvt const *qSto[], uint_fast16_t qLen,
void *stkSto, uint_fast16_t stkSize,
QEvt const *ie)
2012-08-14 18:00:48 -04:00
{
2015-05-14 16:05:04 -04:00
// p-threads allocate stack internally
Q_REQUIRE_ID(600, stkSto == static_cast<void *>(0));
2012-08-14 18:00:48 -04:00
pthread_cond_init(&m_osObject, 0);
2013-12-30 17:41:15 -05:00
m_eQueue.init(qSto, qLen);
2014-04-13 21:35:34 -04:00
m_prio = static_cast<uint8_t>(prio); // set the QF priority of this AO
QF::add_(this); // make QF aware of this AO
this->init(ie); // execute initial transition (virtual call)
2012-08-14 18:00:48 -04:00
pthread_attr_t attr;
pthread_attr_init(&attr);
// SCHED_FIFO corresponds to real-time preemptive priority-based scheduler
// NOTE: This scheduling policy requires the superuser privileges
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
2014-04-13 21:35:34 -04:00
// see NOTE04
2012-08-14 18:00:48 -04:00
struct sched_param param;
param.sched_priority = prio
+ (sched_get_priority_max(SCHED_FIFO)
- QF_MAX_ACTIVE - 3);
pthread_attr_setschedparam(&attr, &param);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2014-04-13 21:35:34 -04:00
// stack size not provided?
if (stkSize == 0U) {
stkSize = (uint_fast16_t)PTHREAD_STACK_MIN; // the minimum
2013-12-30 17:41:15 -05:00
}
2012-08-14 18:00:48 -04:00
pthread_t thread;
2015-05-14 16:05:04 -04:00
if (pthread_create(&thread, &attr, &ao_thread, this) != 0) {
2014-04-13 21:35:34 -04:00
// Creating the p-thread with the SCHED_FIFO policy failed.
// Most probably this application has no superuser privileges,
// so we just fall back to the default SCHED_OTHER policy
// and priority 0.
2012-08-14 18:00:48 -04:00
pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
param.sched_priority = 0;
pthread_attr_setschedparam(&attr, &param);
2015-05-14 16:05:04 -04:00
Q_ALLEGE(pthread_create(&thread, &attr, &ao_thread, this)== 0);
2012-08-14 18:00:48 -04:00
}
pthread_attr_destroy(&attr);
m_thread = static_cast<uint8_t>(1);
}
//............................................................................
2016-12-01 10:31:49 -05:00
void QActive::stop(void) {
2014-04-13 21:35:34 -04:00
m_thread = static_cast<uint8_t>(0); // stop the QF::thread_() loop
2012-08-14 18:00:48 -04:00
}
2015-05-14 16:05:04 -04:00
//............................................................................
static void *ao_thread(void *arg) { // the expected POSIX signature
2016-12-01 10:31:49 -05:00
QF::thread_(static_cast<QActive *>(arg));
2015-05-14 16:05:04 -04:00
return static_cast<void *>(0); // return success
}
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
} // namespace QP
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
// NOTE01:
// In Linux, the scheduler policy closest to real-time is the SCHED_FIFO
// policy, available only with superuser privileges. QF::run() attempts to set
// this policy as well as to maximize its priority, so that the ticking
// occurrs in the most timely manner (as close to an interrupt as possible).
// However, setting the SCHED_FIFO policy might fail, most probably due to
// insufficient privileges.
//
// NOTE02:
// On some Linux systems nanosleep() might actually not deliver the finest
// time granularity. For example, on some Linux implementations, nanosleep()
// could not block for shorter intervals than 20ms, while the underlying
// clock tick period was only 10ms. Sometimes, the select() system call can
// provide a finer granularity.
//
// NOTE03:
// Any blocking system call, such as nanosleep() or select() system call can
// be interrupted by a signal, such as ^C from the keyboard. In this case this
// QF port breaks out of the event-loop and returns to main() that exits and
// terminates all spawned p-threads.
//
// NOTE04:
// According to the man pages (for pthread_attr_setschedpolicy) the only value
// supported in the Linux p-threads implementation is PTHREAD_SCOPE_SYSTEM,
// meaning that the threads contend for CPU time with all processes running on
// the machine. In particular, thread priorities are interpreted relative to
// the priorities of all other processes on the machine.
//
// This is good, because it seems that if we set the priorities high enough,
// no other process (or thread running within) can gain control over the CPU.
//
// However, QF limits the number of priority levels to QF_MAX_ACTIVE.
// Assuming that a QF application will be real-time, this port reserves the
// three highest Linux priorities for the ISR-like threads (e.g., the ticker,
// I/O), and the rest highest-priorities for the active objects.
//
// NOTE05:
2016-04-29 08:52:53 -04:00
// In some (older) Linux kernels, the POSIX nanosleep() system call might
// deliver only 2*actual-system-tick granularity. To compensate for this,
// you would need to reduce (by 2) the constant NANOSLEEP_NSEC_PER_SEC.
2012-08-14 18:00:48 -04:00
//