2019-12-31 15:56:23 -05:00

328 lines
10 KiB
C++

//.$file${.::philo.cpp} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//
// Model: dpp_qmsm.qm
// File: ${.::philo.cpp}
//
// This code has been generated by QM 4.6.0 <www.state-machine.com/qm/>.
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
//
// This code is covered by the following QP license:
// License # : QPCPP-EVAL
// Issued to : Institution or an individual evaluating the QP/C++ framework
// Framework(s): qpcpp
// Support ends: 2020-12-31
// Product(s) :
// This license is available only for evaluation purposes and
// the generated code is still licensed under the terms of GPL.
// Please submit request for extension of the evaluaion period at:
// <www.state-machine.com/licensing>/licensing/#RequestForm
//
//.$endhead${.::philo.cpp} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include "qpcpp.hpp"
#include "dpp.hpp"
#include "bsp.hpp"
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
//.$declare${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace DPP {
//.${AOs::Philo} .............................................................
class Philo : public QP::QMActive {
private:
QP::QTimeEvt m_timeEvt;
public:
Philo();
protected:
QM_STATE_DECL( initial);
QM_STATE_DECL( thinking);
QM_ACTION_DECL(thinking_e);
QM_ACTION_DECL(thinking_x);
QM_STATE_DECL( hungry);
QM_ACTION_DECL(hungry_e);
QM_STATE_DECL( eating);
QM_ACTION_DECL(eating_e);
QM_ACTION_DECL(eating_x);
};
} // namespace DPP
//.$enddecl${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
namespace DPP {
// Local objects -------------------------------------------------------------
static Philo l_philo[N_PHILO]; // storage for all Philos
// helper function to provide a randomized think time for Philos
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
inline QP::QTimeEvtCtr eat_time() {
return static_cast<QP::QTimeEvtCtr>((BSP::random() % BSP::TICKS_PER_SEC)
+ BSP::TICKS_PER_SEC);
}
// helper function to provide the ID of Philo "this"
inline uint8_t PHILO_ID(Philo const * const me) {
return static_cast<uint8_t>(me - l_philo);
}
enum InternalSignals { // internal signals
TIMEOUT_SIG = MAX_SIG
};
// Global objects ------------------------------------------------------------
QP::QActive * const AO_Philo[N_PHILO] = { // "opaque" pointers to Philo AO
&l_philo[0],
&l_philo[1],
&l_philo[2],
&l_philo[3],
&l_philo[4]
};
} // namespace DPP
// Philo definition ----------------------------------------------------------
//.$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//. Check for the minimum required QP version
#if (QP_VERSION < 650U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
#error qpcpp version 6.5.0 or higher required
#endif
//.$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//.$define${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace DPP {
//.${AOs::Philo} .............................................................
//.${AOs::Philo::Philo} ......................................................
Philo::Philo()
: QMActive(Q_STATE_CAST(&Philo::initial)),
m_timeEvt(this, TIMEOUT_SIG, 0U)
{}
//.${AOs::Philo::SM} .........................................................
QM_STATE_DEF(Philo, initial) {
//.${AOs::Philo::SM::initial}
static bool registered = false; // starts off with 0, per C-standard
(void)e; // suppress the compiler warning about unused parameter
if (!registered) {
registered = true;
QS_OBJ_DICTIONARY(&l_philo[0]);
QS_OBJ_DICTIONARY(&l_philo[0].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[1]);
QS_OBJ_DICTIONARY(&l_philo[1].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[2]);
QS_OBJ_DICTIONARY(&l_philo[2].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[3]);
QS_OBJ_DICTIONARY(&l_philo[3].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[4]);
QS_OBJ_DICTIONARY(&l_philo[4].m_timeEvt);
QS_FUN_DICTIONARY(&Philo::initial);
QS_FUN_DICTIONARY(&Philo::thinking);
QS_FUN_DICTIONARY(&Philo::hungry);
QS_FUN_DICTIONARY(&Philo::eating);
}
QS_SIG_DICTIONARY(HUNGRY_SIG, this); // signal for each Philos
QS_SIG_DICTIONARY(TIMEOUT_SIG, this); // signal for each Philos
subscribe(EAT_SIG);
subscribe(TEST_SIG);
QS_FUN_DICTIONARY(&thinking);
QS_FUN_DICTIONARY(&hungry);
QS_FUN_DICTIONARY(&eating);
static struct {
QP::QMState const *target;
QP::QActionHandler act[2];
} const tatbl_ = { // tran-action table
&thinking_s, // target state
{
&thinking_e, // entry
Q_ACTION_NULL // zero terminator
}
};
return qm_tran_init(&tatbl_);
}
//.${AOs::Philo::SM::thinking} ...............................................
QP::QMState const Philo::thinking_s = {
QM_STATE_NULL, // superstate (top)
&Philo::thinking,
&Philo::thinking_e,
&Philo::thinking_x,
Q_ACTION_NULL // no initial tran.
};
//.${AOs::Philo::SM::thinking}
QM_ACTION_DEF(Philo, thinking_e) {
m_timeEvt.armX(think_time(), 0U);
return qm_entry(&thinking_s);
}
//.${AOs::Philo::SM::thinking}
QM_ACTION_DEF(Philo, thinking_x) {
(void)m_timeEvt.disarm();
return qm_exit(&thinking_s);
}
//.${AOs::Philo::SM::thinking}
QM_STATE_DEF(Philo, thinking) {
QP::QState status_;
switch (e->sig) {
//.${AOs::Philo::SM::thinking::TIMEOUT}
case TIMEOUT_SIG: {
static struct {
QP::QMState const *target;
QP::QActionHandler act[3];
} const tatbl_ = { // tran-action table
&hungry_s, // target state
{
&thinking_x, // exit
&hungry_e, // entry
Q_ACTION_NULL // zero terminator
}
};
status_ = qm_tran(&tatbl_);
break;
}
//.${AOs::Philo::SM::thinking::EAT, DONE}
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)->philoNum != PHILO_ID(this));
status_ = Q_RET_HANDLED;
break;
}
//.${AOs::Philo::SM::thinking::TEST}
case TEST_SIG: {
status_ = Q_RET_HANDLED;
break;
}
default: {
status_ = Q_RET_SUPER;
break;
}
}
return status_;
}
//.${AOs::Philo::SM::hungry} .................................................
QP::QMState const Philo::hungry_s = {
QM_STATE_NULL, // superstate (top)
&Philo::hungry,
&Philo::hungry_e,
Q_ACTION_NULL, // no exit action
Q_ACTION_NULL // no initial tran.
};
//.${AOs::Philo::SM::hungry}
QM_ACTION_DEF(Philo, hungry_e) {
TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
pe->philoNum = PHILO_ID(this);
AO_Table->POST(pe, this);
return qm_entry(&hungry_s);
}
//.${AOs::Philo::SM::hungry}
QM_STATE_DEF(Philo, hungry) {
QP::QState status_;
switch (e->sig) {
//.${AOs::Philo::SM::hungry::EAT}
case EAT_SIG: {
//.${AOs::Philo::SM::hungry::EAT::[Q_EVT_CAST(TableEvt)->philoNum=~}
if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(this)) {
static struct {
QP::QMState const *target;
QP::QActionHandler act[2];
} const tatbl_ = { // tran-action table
&eating_s, // target state
{
&eating_e, // entry
Q_ACTION_NULL // zero terminator
}
};
status_ = qm_tran(&tatbl_);
}
else {
status_ = Q_RET_UNHANDLED;
}
break;
}
//.${AOs::Philo::SM::hungry::DONE}
case DONE_SIG: {
/* DONE must be for other Philos than this one */
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this));
status_ = Q_RET_HANDLED;
break;
}
default: {
status_ = Q_RET_SUPER;
break;
}
}
return status_;
}
//.${AOs::Philo::SM::eating} .................................................
QP::QMState const Philo::eating_s = {
QM_STATE_NULL, // superstate (top)
&Philo::eating,
&Philo::eating_e,
&Philo::eating_x,
Q_ACTION_NULL // no initial tran.
};
//.${AOs::Philo::SM::eating}
QM_ACTION_DEF(Philo, eating_e) {
m_timeEvt.armX(eat_time(), 0U);
return qm_entry(&eating_s);
}
//.${AOs::Philo::SM::eating}
QM_ACTION_DEF(Philo, eating_x) {
TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
pe->philoNum = PHILO_ID(this);
QP::QF::PUBLISH(pe, this);
(void)m_timeEvt.disarm();
return qm_exit(&eating_s);
}
//.${AOs::Philo::SM::eating}
QM_STATE_DEF(Philo, eating) {
QP::QState status_;
switch (e->sig) {
//.${AOs::Philo::SM::eating::TIMEOUT}
case TIMEOUT_SIG: {
static struct {
QP::QMState const *target;
QP::QActionHandler act[3];
} const tatbl_ = { // tran-action table
&thinking_s, // target state
{
&eating_x, // exit
&thinking_e, // entry
Q_ACTION_NULL // zero terminator
}
};
status_ = qm_tran(&tatbl_);
break;
}
//.${AOs::Philo::SM::eating::EAT, DONE}
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)->philoNum != PHILO_ID(this));
status_ = Q_RET_HANDLED;
break;
}
default: {
status_ = Q_RET_SUPER;
break;
}
}
return status_;
}
} // namespace DPP
//.$enddef${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^