2015-05-14 16:05:04 -04:00
|
|
|
/// @file
|
|
|
|
/// @brief QF/C++ port to POSIX/P-threads
|
|
|
|
/// @cond
|
|
|
|
///***************************************************************************
|
2020-10-01 12:50:17 -04:00
|
|
|
/// Last updated for version 6.9.1
|
|
|
|
/// Last updated on 2020-09-21
|
2015-05-14 16:05:04 -04:00
|
|
|
///
|
2018-10-25 11:13:01 -04:00
|
|
|
/// Q u a n t u m L e a P s
|
|
|
|
/// ------------------------
|
|
|
|
/// Modern Embedded Software
|
2015-05-14 16:05:04 -04:00
|
|
|
///
|
2020-03-17 21:33:58 -04:00
|
|
|
/// Copyright (C) 2005-2020 Quantum Leaps. All rights reserved.
|
2015-05-14 16:05:04 -04: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-05-14 16:05:04 -04:00
|
|
|
///
|
|
|
|
/// Contact information:
|
2019-12-31 15:56:23 -05:00
|
|
|
/// <www.state-machine.com/licensing>
|
2019-10-27 12:26:31 -04:00
|
|
|
/// <info@state-machine.com>
|
2015-05-14 16:05:04 -04:00
|
|
|
///***************************************************************************
|
|
|
|
/// @endcond
|
2019-02-10 21:01:38 -05:00
|
|
|
///
|
|
|
|
|
|
|
|
// expose features from the 2008 POSIX standard (IEEE Standard 1003.1-2008)
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2014-04-13 21:35:34 -04:00
|
|
|
|
2019-10-27 12:26:31 -04:00
|
|
|
#define QP_IMPL // this is QP implementation
|
|
|
|
#include "qf_port.hpp" // QF port
|
|
|
|
#include "qf_pkg.hpp" // QF package-scope interface
|
|
|
|
#include "qassert.h" // QP embedded systems-friendly assertions
|
|
|
|
#ifdef Q_SPY // QS software tracing enabled?
|
2020-03-17 21:33:58 -04:00
|
|
|
#include "qs_port.hpp" // QS port
|
|
|
|
#include "qs_pkg.hpp" // QS package-scope internal interface
|
2014-04-13 21:35:34 -04:00
|
|
|
#else
|
2019-10-27 12:26:31 -04:00
|
|
|
#include "qs_dummy.hpp" // disable the QS software tracing
|
2014-04-13 21:35:34 -04:00
|
|
|
#endif // Q_SPY
|
|
|
|
|
2019-10-27 12:26:31 -04:00
|
|
|
#include <limits.h> // for PTHREAD_STACK_MIN
|
|
|
|
#include <sys/mman.h> // for mlockall()
|
2018-10-25 11:13:01 -04:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/ioctl.h>
|
2019-10-27 12:26:31 -04:00
|
|
|
#include <string.h> // for memcpy() and memset()
|
2018-10-25 11:13:01 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2013-10-10 20:01:51 -04:00
|
|
|
namespace QP {
|
2012-08-14 18:00:48 -04:00
|
|
|
|
|
|
|
Q_DEFINE_THIS_MODULE("qf_port")
|
|
|
|
|
2018-04-05 17:04:31 -04:00
|
|
|
/* Global objects ==========================================================*/
|
2018-11-22 11:34:13 -05:00
|
|
|
pthread_mutex_t QF_pThreadMutex_; // mutex for QF critical section
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2018-04-05 17:04:31 -04:00
|
|
|
// Local objects *************************************************************
|
2017-08-29 16:01:46 -04:00
|
|
|
static pthread_mutex_t l_startupMutex;
|
2018-10-25 11:13:01 -04:00
|
|
|
static bool l_isRunning; // flag indicating when QF is running
|
|
|
|
static struct termios l_tsav; // structure with saved terminal attributes
|
2016-04-29 08:52:53 -04:00
|
|
|
static struct timespec l_tick;
|
2018-06-16 17:21:53 -04:00
|
|
|
static int_t l_tickPrio;
|
2016-04-29 08:52:53 -04:00
|
|
|
enum { NANOSLEEP_NSEC_PER_SEC = 1000000000 }; // see NOTE05
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
static void sigIntHandler(int /* dummy */);
|
2015-05-14 16:05:04 -04:00
|
|
|
static void *ao_thread(void *arg); // thread routine for all AOs
|
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
// QF functions ==============================================================
|
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);
|
|
|
|
|
2017-08-29 16:01:46 -04:00
|
|
|
// init the startup mutex with the default non-recursive initializer
|
|
|
|
pthread_mutex_init(&l_startupMutex, NULL);
|
|
|
|
|
|
|
|
// lock the startup mutex to block any active objects started before
|
|
|
|
// calling QF::run()
|
|
|
|
pthread_mutex_lock(&l_startupMutex);
|
|
|
|
|
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
|
2018-06-16 17:21:53 -04:00
|
|
|
l_tickPrio = sched_get_priority_min(SCHED_FIFO); // default tick prio
|
2018-10-25 11:13:01 -04:00
|
|
|
|
|
|
|
// install the SIGINT (Ctrl-C) signal handler
|
|
|
|
struct sigaction sig_act;
|
|
|
|
sig_act.sa_handler = &sigIntHandler;
|
|
|
|
sigaction(SIGINT, &sig_act, NULL);
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
2018-04-05 17:04:31 -04:00
|
|
|
|
2019-11-04 12:29:48 -05:00
|
|
|
//****************************************************************************
|
2018-11-22 11:34:13 -05:00
|
|
|
void QF_enterCriticalSection_(void) {
|
|
|
|
pthread_mutex_lock(&QF_pThreadMutex_);
|
|
|
|
}
|
2019-11-04 12:29:48 -05:00
|
|
|
//****************************************************************************
|
2018-11-22 11:34:13 -05:00
|
|
|
void QF_leaveCriticalSection_(void) {
|
|
|
|
pthread_mutex_unlock(&QF_pThreadMutex_);
|
|
|
|
}
|
|
|
|
|
2018-04-05 17:04:31 -04:00
|
|
|
//****************************************************************************
|
2013-12-30 17:41:15 -05:00
|
|
|
int_t QF::run(void) {
|
2019-11-04 12:29:48 -05:00
|
|
|
onStartup(); // application-specific startup callback
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2020-08-24 16:42:48 -04:00
|
|
|
// produce the QS_QF_RUN trace record
|
2020-10-01 12:50:17 -04:00
|
|
|
QS_BEGIN_NOCRIT_PRE_(QS_QF_RUN, 0U)
|
2020-08-24 16:42:48 -04:00
|
|
|
QS_END_NOCRIT_PRE_()
|
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
// try to set the priority of the ticker thread, see NOTE01
|
2015-05-14 16:05:04 -04:00
|
|
|
struct sched_param sparam;
|
2018-06-16 17:21:53 -04:00
|
|
|
sparam.sched_priority = l_tickPrio;
|
2012-08-14 18:00:48 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-29 16:01:46 -04:00
|
|
|
// unlock the startup mutex to unblock any active objects started before
|
|
|
|
// calling QF::run()
|
|
|
|
pthread_mutex_unlock(&l_startupMutex);
|
|
|
|
|
|
|
|
l_isRunning = true;
|
|
|
|
while (l_isRunning) { // 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
|
|
|
}
|
2019-11-04 12:29:48 -05:00
|
|
|
onCleanup(); // cleanup callback
|
2017-08-29 16:01:46 -04:00
|
|
|
pthread_mutex_destroy(&l_startupMutex);
|
2012-08-14 18:00:48 -04:00
|
|
|
pthread_mutex_destroy(&QF_pThreadMutex_);
|
2019-11-04 12:29:48 -05:00
|
|
|
|
2020-03-17 21:33:58 -04:00
|
|
|
return 0; // return success
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
2018-04-05 17:04:31 -04:00
|
|
|
//****************************************************************************
|
2020-03-17 21:33:58 -04:00
|
|
|
void QF_setTickRate(std::uint32_t ticksPerSec, int_t tickPrio) {
|
|
|
|
Q_REQUIRE_ID(300, ticksPerSec != 0U);
|
2016-04-29 08:52:53 -04:00
|
|
|
l_tick.tv_nsec = NANOSLEEP_NSEC_PER_SEC / ticksPerSec;
|
2018-06-16 17:21:53 -04:00
|
|
|
l_tickPrio = tickPrio;
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
2019-11-04 12:29:48 -05:00
|
|
|
//............................................................................
|
2012-08-14 18:00:48 -04:00
|
|
|
void QF::stop(void) {
|
2017-08-29 16:01:46 -04:00
|
|
|
l_isRunning = 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) {
|
2017-08-29 16:01:46 -04:00
|
|
|
// block this thread until the startup mutex is unlocked from QF::run()
|
|
|
|
pthread_mutex_lock(&l_startupMutex);
|
|
|
|
pthread_mutex_unlock(&l_startupMutex);
|
|
|
|
|
2020-07-18 17:58:58 -04:00
|
|
|
#ifdef QF_ACTIVE_STOP
|
|
|
|
act->m_thread = true;
|
|
|
|
while (act->m_thread)
|
|
|
|
#else
|
|
|
|
for (;;) // for-ever
|
|
|
|
#endif
|
|
|
|
{
|
2014-04-13 21:35:34 -04:00
|
|
|
QEvt const *e = act->get_(); // wait for event
|
2020-10-01 12:50:17 -04:00
|
|
|
act->dispatch(e, act->m_prio); // dispatch to the AO's state machine
|
2014-04-13 21:35:34 -04:00
|
|
|
gc(e); // check if the event is garbage, and collect it if so
|
2019-02-10 21:01:38 -05:00
|
|
|
}
|
2020-07-18 17:58:58 -04:00
|
|
|
#ifdef QF_ACTIVE_STOP
|
|
|
|
remove_(act); // remove this object from QF
|
|
|
|
#endif
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
2018-10-25 11:13:01 -04:00
|
|
|
|
|
|
|
//............................................................................
|
|
|
|
void QF_consoleSetup(void) {
|
|
|
|
struct termios tio; // modified terminal attributes
|
|
|
|
|
|
|
|
tcgetattr(0, &l_tsav); // save the current terminal attributes
|
|
|
|
tcgetattr(0, &tio); // obtain the current terminal attributes
|
|
|
|
tio.c_lflag &= ~(ICANON | ECHO); // disable the canonical mode & echo
|
|
|
|
tcsetattr(0, TCSANOW, &tio); // set the new attributes
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void QF_consoleCleanup(void) {
|
|
|
|
tcsetattr(0, TCSANOW, &l_tsav); // restore the saved attributes
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
int QF_consoleGetKey(void) {
|
|
|
|
int byteswaiting;
|
|
|
|
ioctl(0, FIONREAD, &byteswaiting);
|
|
|
|
if (byteswaiting > 0) {
|
|
|
|
char ch;
|
2020-08-05 12:33:18 -04:00
|
|
|
read(0, &ch, 1);
|
2018-10-25 11:13:01 -04:00
|
|
|
return (int)ch;
|
|
|
|
}
|
|
|
|
return 0; // no input at this time
|
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
int QF_consoleWaitForKey(void) {
|
|
|
|
return getchar();
|
|
|
|
}
|
|
|
|
|
2019-11-04 12:29:48 -05:00
|
|
|
//****************************************************************************
|
2020-03-17 21:33:58 -04:00
|
|
|
void QActive::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,
|
2019-10-27 12:26:31 -04:00
|
|
|
void const * const par)
|
2012-08-14 18:00:48 -04:00
|
|
|
{
|
2015-05-14 16:05:04 -04:00
|
|
|
// p-threads allocate stack internally
|
2020-03-17 21:33:58 -04:00
|
|
|
Q_REQUIRE_ID(600, stkSto == nullptr);
|
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);
|
2020-03-17 21:33:58 -04:00
|
|
|
m_prio = static_cast<std::uint8_t>(prio); // set the QF prio of this AO
|
2014-04-13 21:35:34 -04:00
|
|
|
QF::add_(this); // make QF aware of this AO
|
2019-10-27 12:26:31 -04:00
|
|
|
|
2020-10-01 12:50:17 -04:00
|
|
|
this->init(par, m_prio); // execute initial transition (virtual call)
|
2019-10-27 12:26:31 -04:00
|
|
|
QS_FLUSH(); // flush the QS trace buffer to the host
|
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
|
2020-08-05 12:33:18 -04:00
|
|
|
pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
|
|
|
|
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
|
|
|
|
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
|
2014-04-13 21:35:34 -04:00
|
|
|
|
2020-08-05 12:33:18 -04:00
|
|
|
// priority of the p-thread, see NOTE04
|
2012-08-14 18:00:48 -04:00
|
|
|
struct sched_param param;
|
2020-08-05 12:33:18 -04:00
|
|
|
param.sched_priority = prio
|
|
|
|
+ (sched_get_priority_max(SCHED_FIFO)
|
|
|
|
- QF_MAX_ACTIVE - 3U);
|
2012-08-14 18:00:48 -04:00
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
2020-08-05 12:33:18 -04:00
|
|
|
|
2020-01-26 20:09:37 -05:00
|
|
|
pthread_attr_setstacksize(&attr, (stkSize < PTHREAD_STACK_MIN
|
|
|
|
? PTHREAD_STACK_MIN
|
|
|
|
: stkSize));
|
2012-08-14 18:00:48 -04:00
|
|
|
pthread_t thread;
|
2020-08-05 12:33:18 -04:00
|
|
|
int err = pthread_create(&thread, &attr, &ao_thread, this);
|
|
|
|
if (err != 0) {
|
|
|
|
// Creating p-thread with the SCHED_FIFO policy failed. Most likely
|
|
|
|
// 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, ¶m);
|
2020-08-05 12:33:18 -04:00
|
|
|
err = pthread_create(&thread, &attr, &ao_thread, this);
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
2020-08-05 12:33:18 -04:00
|
|
|
Q_ASSERT_ID(610, err == 0); // AO thread must be created
|
|
|
|
|
|
|
|
//pthread_attr_getschedparam(&attr, ¶m);
|
|
|
|
//printf("param.sched_priority==%d\n", param.sched_priority);
|
|
|
|
|
2012-08-14 18:00:48 -04:00
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
}
|
2020-07-18 17:58:58 -04:00
|
|
|
//............................................................................
|
|
|
|
#ifdef QF_ACTIVE_STOP
|
|
|
|
void QActive::stop(void) {
|
|
|
|
unsubscribeAll(); // unsubscribe this AO from all events
|
|
|
|
m_thread = false; // stop the thread loop (see QF::thread_)
|
|
|
|
}
|
|
|
|
#endif
|
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));
|
2020-03-17 21:33:58 -04:00
|
|
|
return nullptr; // return success
|
2015-05-14 16:05:04 -04:00
|
|
|
}
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
//****************************************************************************
|
|
|
|
static void sigIntHandler(int /* dummy */) {
|
2019-11-04 12:29:48 -05:00
|
|
|
QF::onCleanup();
|
2018-10-25 11:13:01 -04:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
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
|
2020-08-05 12:33:18 -04:00
|
|
|
// three highest p-thread priorities for the ISR-like threads (e.g., I/O),
|
|
|
|
// and the rest highest-priorities for the active objects.
|
2012-08-14 18:00:48 -04:00
|
|
|
//
|
|
|
|
// 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,
|
2020-08-05 12:33:18 -04:00
|
|
|
// you would need to reduce the constant NANOSLEEP_NSEC_PER_SEC by factor 2.
|
2012-08-14 18:00:48 -04:00
|
|
|
//
|
2018-04-05 17:04:31 -04:00
|
|
|
|