250 lines
7.7 KiB
C++
Raw Normal View History

2019-12-31 15:56:23 -05:00
//.$file${.::philo.cpp} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2018-03-19 14:51:26 -04:00
//
2017-05-17 13:15:09 -04:00
// Model: dpp.qm
2018-03-19 14:51:26 -04:00
// File: ${.::philo.cpp}
2017-05-17 13:15:09 -04:00
//
// This code has been generated by QM 5.1.3 <www.state-machine.com/qm/>.
2017-05-17 13:15:09 -04:00
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
//
// 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.
//
// 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.
2018-03-19 14:51:26 -04:00
//
2019-12-31 15:56:23 -05:00
//.$endhead${.::philo.cpp} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2019-10-27 12:26:31 -04:00
#include "qpcpp.hpp"
#include "dpp.hpp"
#include "bsp.hpp"
2017-05-17 13:15:09 -04:00
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
2019-12-31 15:56:23 -05:00
//.$declare${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2017-05-17 13:15:09 -04:00
namespace DPP {
2019-12-31 15:56:23 -05:00
//.${AOs::Philo} .............................................................
2017-05-17 13:15:09 -04:00
class Philo : public QP::QActive {
2019-03-28 11:59:31 -04:00
public:
static Philo inst[N_PHILO];
2017-05-17 13:15:09 -04:00
private:
QP::QTimeEvt m_timeEvt;
public:
explicit Philo() noexcept;
2017-05-17 13:15:09 -04:00
protected:
2019-03-28 11:59:31 -04:00
Q_STATE_DECL(initial);
Q_STATE_DECL(thinking);
Q_STATE_DECL(hungry);
Q_STATE_DECL(eating);
2017-05-17 13:15:09 -04:00
};
} // namespace DPP
2019-12-31 15:56:23 -05:00
//.$enddecl${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2017-05-17 13:15:09 -04:00
namespace DPP {
// Local objects -------------------------------------------------------------
// 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 "me"
inline uint8_t PHILO_ID(Philo const * const me) {
2019-03-28 11:59:31 -04:00
return static_cast<uint8_t>(me - &Philo::inst[0]);
2017-05-17 13:15:09 -04:00
}
} // namespace DPP
2019-12-31 15:56:23 -05:00
//.$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//. Check for the minimum required QP version
#if (QP_VERSION < 690U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
#error qpcpp version 6.9.0 or higher required
2018-03-19 14:51:26 -04:00
#endif
2019-12-31 15:56:23 -05:00
//.$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//.$define${Shared::AO_Philo[N_PHILO]} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2019-03-28 11:59:31 -04:00
namespace DPP {
2018-06-25 16:44:34 -04:00
// opaque pointers to the Philo AOs
//.${Shared::AO_Philo[N_PHILO]} ..............................................
2019-03-28 11:59:31 -04:00
QP::QActive * const AO_Philo[N_PHILO] = { // "opaque" pointers to Philo AO
&Philo::inst[0],
&Philo::inst[1],
&Philo::inst[2],
&Philo::inst[3],
&Philo::inst[4]
};
} // namespace DPP
//.$enddef${Shared::AO_Philo[N_PHILO]} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2019-12-31 15:56:23 -05:00
//.$define${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2017-05-17 13:15:09 -04:00
namespace DPP {
2019-12-31 15:56:23 -05:00
//.${AOs::Philo} .............................................................
2019-03-28 11:59:31 -04:00
Philo Philo::inst[N_PHILO];
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::Philo} ......................................................
Philo::Philo() noexcept
2019-03-28 11:59:31 -04:00
: QActive(&initial),
2017-05-17 13:15:09 -04:00
m_timeEvt(this, TIMEOUT_SIG, 0U)
{}
2018-10-25 11:13:01 -04:00
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM} .........................................................
2019-03-28 11:59:31 -04:00
Q_STATE_DEF(Philo, initial) {
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::initial}
static_cast<void>(e); // unused parameter
#ifdef Q_SPY
std::uint8_t n = PHILO_ID(this);
QS_OBJ_ARR_DICTIONARY(&Philo::inst[n], n);
QS_OBJ_ARR_DICTIONARY(&Philo::inst[n].m_timeEvt, n);
#endif
subscribe(EAT_SIG);
subscribe(TEST_SIG);
2017-05-17 13:15:09 -04:00
static bool registered = false; // starts off with 0, per C-standard
if (!registered) {
registered = true;
2018-10-25 11:13:01 -04:00
QS_FUN_DICTIONARY(&Philo::initial);
QS_FUN_DICTIONARY(&Philo::thinking);
QS_FUN_DICTIONARY(&Philo::hungry);
QS_FUN_DICTIONARY(&Philo::eating);
2017-05-17 13:15:09 -04:00
}
2019-03-28 11:59:31 -04:00
return tran(&thinking);
2017-05-17 13:15:09 -04:00
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::thinking} ...............................................
2019-03-28 11:59:31 -04:00
Q_STATE_DEF(Philo, thinking) {
2017-05-17 13:15:09 -04:00
QP::QState status_;
switch (e->sig) {
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::thinking}
2017-05-17 13:15:09 -04:00
case Q_ENTRY_SIG: {
2019-03-28 11:59:31 -04:00
m_timeEvt.armX(think_time(), 0U);
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::thinking}
2017-05-17 13:15:09 -04:00
case Q_EXIT_SIG: {
2019-03-28 11:59:31 -04:00
(void)m_timeEvt.disarm();
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::thinking::TIMEOUT}
2017-05-17 13:15:09 -04:00
case TIMEOUT_SIG: {
2019-03-28 11:59:31 -04:00
status_ = tran(&hungry);
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::thinking::EAT, DONE}
2017-05-17 13:15:09 -04:00
case EAT_SIG: // intentionally fall through
case DONE_SIG: {
/* EAT or DONE must be for other Philos than this one */
2019-03-28 11:59:31 -04:00
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this));
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::thinking::TEST}
2017-05-17 13:15:09 -04:00
case TEST_SIG: {
2019-03-28 11:59:31 -04:00
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
default: {
2019-03-28 11:59:31 -04:00
status_ = super(&top);
2017-05-17 13:15:09 -04:00
break;
}
}
return status_;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::hungry} .................................................
2019-03-28 11:59:31 -04:00
Q_STATE_DEF(Philo, hungry) {
2017-05-17 13:15:09 -04:00
QP::QState status_;
switch (e->sig) {
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::hungry}
2017-05-17 13:15:09 -04:00
case Q_ENTRY_SIG: {
TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
2019-03-28 11:59:31 -04:00
pe->philoNum = PHILO_ID(this);
AO_Table->POST(pe, this);
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::hungry::EAT}
2017-05-17 13:15:09 -04:00
case EAT_SIG: {
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::hungry::EAT::[Q_EVT_CAST(TableEvt)->philoNum=~}
2019-03-28 11:59:31 -04:00
if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(this)) {
status_ = tran(&eating);
2017-05-17 13:15:09 -04:00
}
else {
2019-03-28 11:59:31 -04:00
status_ = Q_RET_UNHANDLED;
2017-05-17 13:15:09 -04:00
}
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::hungry::DONE}
2017-05-17 13:15:09 -04:00
case DONE_SIG: {
/* DONE must be for other Philos than this one */
2019-03-28 11:59:31 -04:00
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this));
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
default: {
2019-03-28 11:59:31 -04:00
status_ = super(&top);
2017-05-17 13:15:09 -04:00
break;
}
}
return status_;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::eating} .................................................
2019-03-28 11:59:31 -04:00
Q_STATE_DEF(Philo, eating) {
2017-05-17 13:15:09 -04:00
QP::QState status_;
switch (e->sig) {
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::eating}
2017-05-17 13:15:09 -04:00
case Q_ENTRY_SIG: {
2019-03-28 11:59:31 -04:00
m_timeEvt.armX(eat_time(), 0U);
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::eating}
2017-05-17 13:15:09 -04:00
case Q_EXIT_SIG: {
TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
2019-03-28 11:59:31 -04:00
pe->philoNum = PHILO_ID(this);
QP::QF::PUBLISH(pe, this);
(void)m_timeEvt.disarm();
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::eating::TIMEOUT}
2017-05-17 13:15:09 -04:00
case TIMEOUT_SIG: {
2019-03-28 11:59:31 -04:00
status_ = tran(&thinking);
2017-05-17 13:15:09 -04:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Philo::SM::eating::EAT, DONE}
2017-05-17 13:15:09 -04:00
case EAT_SIG: // intentionally fall through
case DONE_SIG: {
/* EAT or DONE must be for other Philos than this one */
2019-03-28 11:59:31 -04:00
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this));
status_ = Q_RET_HANDLED;
2017-05-17 13:15:09 -04:00
break;
}
default: {
2019-03-28 11:59:31 -04:00
status_ = super(&top);
2017-05-17 13:15:09 -04:00
break;
}
}
return status_;
}
} // namespace DPP
2019-12-31 15:56:23 -05:00
//.$enddef${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^