265 lines
8.1 KiB
C++
Raw Normal View History

2022-08-11 15:36:19 -04:00
//$file${.::philo.cpp} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2018-01-10 18:26:05 -05:00
//
2017-02-07 19:55:33 -05:00
// Model: dpp.qm
2018-01-10 18:26:05 -05:00
// File: ${.::philo.cpp}
2017-02-07 19:55:33 -05:00
//
// This code has been generated by QM 5.3.0 <www.state-machine.com/qm>.
2017-02-07 19:55:33 -05:00
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
//
2022-08-11 15:36:19 -04:00
// SPDX-License-Identifier: GPL-3.0-or-later
2017-02-07 19:55:33 -05:00
//
2022-08-11 15:36:19 -04:00
// This generated code is open source software: you can redistribute it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation.
2018-01-10 18:26:05 -05:00
//
2022-08-11 15:36:19 -04:00
// This code 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.
//
// NOTE:
// Alternatively, this generated code may be distributed 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.
//
// Contact information:
// <www.state-machine.com/licensing>
// <info@state-machine.com>
//
//$endhead${.::philo.cpp} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include "qpcpp.hpp" // QP/C++ real-time embedded framework
#include "dpp.hpp" // DPP Application interface
#include "bsp.hpp" // Board Support Package
//----------------------------------------------------------------------------
namespace { // unnamed namespace for local definitions with internal linkage
2017-02-07 19:55:33 -05:00
Q_DEFINE_THIS_FILE
// helper function to provide a randomized think time for Philos
static inline QP::QTimeEvtCtr think_time() {
return static_cast<QP::QTimeEvtCtr>((BSP::random() % BSP::TICKS_PER_SEC)
+ (BSP::TICKS_PER_SEC/2U));
}
// helper function to provide a randomized eat time for Philos
static inline QP::QTimeEvtCtr eat_time() {
return static_cast<QP::QTimeEvtCtr>((BSP::random() % BSP::TICKS_PER_SEC)
+ BSP::TICKS_PER_SEC);
}
} // unnamed namespace
//----------------------------------------------------------------------------
2022-08-11 15:36:19 -04:00
//$declare${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace APP {
2017-02-07 19:55:33 -05:00
2022-08-11 15:36:19 -04:00
//${AOs::Philo} ..............................................................
2017-02-07 19:55:33 -05:00
class Philo : public QP::QActive {
private:
QP::QTimeEvt m_timeEvt;
std::uint8_t m_id;
public:
static Philo inst[N_PHILO];
2017-02-07 19:55:33 -05:00
public:
Philo();
protected:
2019-04-16 22:11:29 -04:00
Q_STATE_DECL(initial);
Q_STATE_DECL(thinking);
Q_STATE_DECL(hungry);
Q_STATE_DECL(eating);
2022-08-11 15:36:19 -04:00
}; // class Philo
2017-02-07 19:55:33 -05:00
} // namespace APP
2022-08-11 15:36:19 -04:00
//$enddecl${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2017-02-07 19:55:33 -05:00
2022-08-11 15:36:19 -04:00
//$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Check for the minimum required QP version
#if (QP_VERSION < 730U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
#error qpcpp version 7.3.0 or higher required
2018-01-10 18:26:05 -05:00
#endif
2022-08-11 15:36:19 -04:00
//$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//$define${Shared::AO_Philo[N_PHILO]} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace APP {
2020-08-24 16:42:48 -04:00
//${Shared::AO_Philo[N_PHILO]} ...............................................
QP::QActive * const AO_Philo[N_PHILO] = {
2020-08-24 16:42:48 -04:00
&Philo::inst[0],
&Philo::inst[1],
&Philo::inst[2],
&Philo::inst[3],
&Philo::inst[4]
};
} // namespace APP
//$enddef${Shared::AO_Philo[N_PHILO]} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2022-08-11 15:36:19 -04:00
//$define${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace APP {
2017-02-07 19:55:33 -05:00
2022-08-11 15:36:19 -04:00
//${AOs::Philo} ..............................................................
2020-08-24 16:42:48 -04:00
Philo Philo::inst[N_PHILO];
2022-08-11 15:36:19 -04:00
//${AOs::Philo::Philo} .......................................................
2017-02-07 19:55:33 -05:00
Philo::Philo()
: QActive(Q_STATE_CAST(&initial)),
m_timeEvt(this, TIMEOUT_SIG, 0U),
m_id(0xFFU)
2017-02-07 19:55:33 -05:00
{}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM} ..........................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, initial) {
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::initial}
Q_UNUSED_PAR(e);
2017-02-07 19:55:33 -05:00
m_id = static_cast<std::uint8_t>(this - &inst[0]);
2020-08-24 16:42:48 -04:00
QS_OBJ_ARR_DICTIONARY(&Philo::inst[m_id], m_id);
QS_OBJ_ARR_DICTIONARY(&Philo::inst[m_id].m_timeEvt, m_id);
2017-02-07 19:55:33 -05:00
2020-08-24 16:42:48 -04:00
subscribe(EAT_SIG);
subscribe(TEST_SIG);
QS_FUN_DICTIONARY(&Philo::thinking);
QS_FUN_DICTIONARY(&Philo::hungry);
QS_FUN_DICTIONARY(&Philo::eating);
2019-04-16 22:11:29 -04:00
return tran(&thinking);
2017-02-07 19:55:33 -05:00
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::thinking} ................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, thinking) {
2017-02-07 19:55:33 -05:00
QP::QState status_;
switch (e->sig) {
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::thinking}
2017-02-07 19:55:33 -05:00
case Q_ENTRY_SIG: {
2019-04-16 22:11:29 -04:00
m_timeEvt.armX(think_time(), 0U);
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::thinking}
2017-02-07 19:55:33 -05:00
case Q_EXIT_SIG: {
m_timeEvt.disarm();
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::thinking::TIMEOUT}
2017-02-07 19:55:33 -05:00
case TIMEOUT_SIG: {
2019-04-16 22:11:29 -04:00
status_ = tran(&hungry);
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::thinking::EAT, DONE}
2017-02-07 19:55:33 -05:00
case EAT_SIG: // intentionally fall through
case DONE_SIG: {
// EAT or DONE must be for other Philos than this one
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoId != m_id);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::thinking::TEST}
2017-02-07 19:55:33 -05:00
case TEST_SIG: {
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&top);
2017-02-07 19:55:33 -05:00
break;
}
}
return status_;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::hungry} ..................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, hungry) {
2017-02-07 19:55:33 -05:00
QP::QState status_;
switch (e->sig) {
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::hungry}
2017-02-07 19:55:33 -05:00
case Q_ENTRY_SIG: {
#ifdef QEVT_DYN_CTOR
TableEvt const *pe = Q_NEW(TableEvt, HUNGRY_SIG, m_id);
#else
2017-02-07 19:55:33 -05:00
TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
pe->philoId = m_id;
#endif
2019-04-16 22:11:29 -04:00
AO_Table->POST(pe, this);
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::hungry::EAT}
2017-02-07 19:55:33 -05:00
case EAT_SIG: {
//${AOs::Philo::SM::hungry::EAT::[e->philoId==m_id]}
if (Q_EVT_CAST(TableEvt)->philoId == m_id) {
2019-04-16 22:11:29 -04:00
status_ = tran(&eating);
2017-02-07 19:55:33 -05:00
}
else {
2019-04-16 22:11:29 -04:00
status_ = Q_RET_UNHANDLED;
2017-02-07 19:55:33 -05:00
}
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::hungry::DONE}
2017-02-07 19:55:33 -05:00
case DONE_SIG: {
// DONE must be for other Philos than this one
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoId != m_id);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&top);
2017-02-07 19:55:33 -05:00
break;
}
}
return status_;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::eating} ..................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, eating) {
2017-02-07 19:55:33 -05:00
QP::QState status_;
switch (e->sig) {
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::eating}
2017-02-07 19:55:33 -05:00
case Q_ENTRY_SIG: {
2019-04-16 22:11:29 -04:00
m_timeEvt.armX(eat_time(), 0U);
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::eating}
2017-02-07 19:55:33 -05:00
case Q_EXIT_SIG: {
m_timeEvt.disarm();
#ifdef QEVT_DYN_CTOR
TableEvt const *pe = Q_NEW(TableEvt, DONE_SIG, m_id);
#else
2017-02-07 19:55:33 -05:00
TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
pe->philoId = m_id;
#endif
2022-08-11 15:36:19 -04:00
QP::QActive::PUBLISH(pe, this);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::eating::TIMEOUT}
2017-02-07 19:55:33 -05:00
case TIMEOUT_SIG: {
2019-04-16 22:11:29 -04:00
status_ = tran(&thinking);
2017-02-07 19:55:33 -05:00
break;
}
2022-08-11 15:36:19 -04:00
//${AOs::Philo::SM::eating::EAT, DONE}
2017-02-07 19:55:33 -05:00
case EAT_SIG: // intentionally fall through
case DONE_SIG: {
// EAT or DONE must be for other Philos than this one
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoId != m_id);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2017-02-07 19:55:33 -05:00
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&top);
2017-02-07 19:55:33 -05:00
break;
}
}
return status_;
}
} // namespace APP
2022-08-11 15:36:19 -04:00
//$enddef${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^