249 lines
7.8 KiB
C++
Raw Normal View History

2019-04-16 22:11:29 -04:00
//$file${.::philo.cpp} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2018-02-12 18:51:22 -05:00
//
// Model: dpp.qm
2018-02-12 18:51:22 -05:00
// File: ${.::philo.cpp}
//
2019-10-27 12:26:31 -04:00
// This code has been generated by QM 4.5.1 (https://www.state-machine.com/qm).
2013-10-10 20:01:51 -04:00
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
//
2013-10-10 20:01:51 -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.
//
// 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-02-12 18:51:22 -05:00
//
2019-04-16 22:11:29 -04:00
//$endhead${.::philo.cpp} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2019-10-27 12:26:31 -04:00
#include "qpcpp.hpp"
#include "dpp.hpp"
#include "bsp.hpp"
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
2019-04-16 22:11:29 -04:00
//$declare${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2013-12-30 17:41:15 -05:00
namespace DPP {
2014-04-13 21:35:34 -04:00
//${AOs::Philo} ..............................................................
2016-12-01 10:31:49 -05:00
class Philo : public QP::QActive {
private:
QP::QTimeEvt m_timeEvt;
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);
};
2013-12-30 17:41:15 -05:00
} // namespace DPP
2019-04-16 22:11:29 -04:00
//$enddecl${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013-12-30 17:41:15 -05:00
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() {
2015-12-31 14:56:37 -05:00
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() {
2015-12-31 14:56:37 -05:00
return static_cast<QP::QTimeEvtCtr>((BSP::random() % BSP::TICKS_PER_SEC)
+ BSP::TICKS_PER_SEC);
}
2019-04-16 22:11:29 -04:00
// helper function to provide the ID of Philo
inline uint8_t PHILO_ID(Philo const * const philo) {
return static_cast<uint8_t>(philo - l_philo);
}
enum InternalSignals { // internal signals
TIMEOUT_SIG = MAX_SIG
};
// Global objects ------------------------------------------------------------
2016-12-01 10:31:49 -05:00
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]
};
2013-12-30 17:41:15 -05:00
} // namespace DPP
// Philo definition ----------------------------------------------------------
2019-04-16 22:11:29 -04:00
//$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2018-02-12 18:51:22 -05:00
// Check for the minimum required QP version
2019-04-16 22:11:29 -04:00
#if (QP_VERSION < 650U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
#error qpcpp version 6.5.0 or higher required
2018-02-12 18:51:22 -05:00
#endif
2019-04-16 22:11:29 -04:00
//$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//$define${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2013-12-30 17:41:15 -05:00
namespace DPP {
2014-04-13 21:35:34 -04:00
//${AOs::Philo} ..............................................................
//${AOs::Philo::Philo} .......................................................
2013-12-30 17:41:15 -05:00
Philo::Philo()
2016-12-01 10:31:49 -05:00
: QActive(Q_STATE_CAST(&Philo::initial)),
2013-12-30 17:41:15 -05:00
m_timeEvt(this, TIMEOUT_SIG, 0U)
{}
2018-12-12 17:54:19 -05:00
2014-04-13 21:35:34 -04:00
//${AOs::Philo::SM} ..........................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, initial) {
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::initial}
static bool registered = false; // starts off with 0, per C-standard
(void)e; // suppress the compiler warning about unused parameter
2018-06-25 16:44:34 -04:00
2019-04-16 22:11:29 -04:00
subscribe(EAT_SIG);
subscribe(TEST_SIG);
2018-06-25 16:44:34 -04:00
if (!registered) {
registered = true;
QS_OBJ_DICTIONARY(&l_philo[0].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[1].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[2].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[3].m_timeEvt);
QS_OBJ_DICTIONARY(&l_philo[4].m_timeEvt);
2018-06-25 16:44:34 -04:00
QS_FUN_DICTIONARY(&initial);
QS_FUN_DICTIONARY(&thinking);
QS_FUN_DICTIONARY(&hungry);
QS_FUN_DICTIONARY(&eating);
}
2018-06-25 16:44:34 -04:00
2019-04-16 22:11:29 -04:00
QS_SIG_DICTIONARY(HUNGRY_SIG, this); // signal for each Philos
QS_SIG_DICTIONARY(TIMEOUT_SIG, this); // signal for each Philos
return tran(&thinking);
}
2014-04-13 21:35:34 -04:00
//${AOs::Philo::SM::thinking} ................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, thinking) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::thinking}
2016-12-01 10:31:49 -05:00
case Q_ENTRY_SIG: {
2019-04-16 22:11:29 -04:00
m_timeEvt.armX(think_time(), 0U);
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::thinking}
2016-12-01 10:31:49 -05:00
case Q_EXIT_SIG: {
2019-04-16 22:11:29 -04:00
(void)m_timeEvt.disarm();
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::thinking::TIMEOUT}
case TIMEOUT_SIG: {
2019-04-16 22:11:29 -04:00
status_ = tran(&hungry);
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::thinking::EAT, DONE}
2015-05-22 20:38:16 -04:00
case EAT_SIG: // intentionally fall through
case DONE_SIG: {
2019-04-16 22:11:29 -04:00
// 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;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::thinking::TEST}
2016-09-29 19:54:50 -04:00
case TEST_SIG: {
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2016-09-29 19:54:50 -04:00
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&top);
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
2014-04-13 21:35:34 -04:00
//${AOs::Philo::SM::hungry} ..................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, hungry) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::hungry}
2016-12-01 10:31:49 -05:00
case Q_ENTRY_SIG: {
TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
2019-04-16 22:11:29 -04:00
pe->philoNum = PHILO_ID(this);
AO_Table->POST(pe, this);
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::hungry::EAT}
case EAT_SIG: {
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::hungry::EAT::[Q_EVT_CAST(TableEvt)->philoNum=~}
2019-04-16 22:11:29 -04:00
if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(this)) {
status_ = tran(&eating);
}
else {
2019-04-16 22:11:29 -04:00
status_ = Q_RET_UNHANDLED;
}
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::hungry::DONE}
case DONE_SIG: {
/* DONE must be for other Philos than this one */
2019-04-16 22:11:29 -04:00
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this));
status_ = Q_RET_HANDLED;
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&top);
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
2014-04-13 21:35:34 -04:00
//${AOs::Philo::SM::eating} ..................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Philo, eating) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::eating}
2016-12-01 10:31:49 -05:00
case Q_ENTRY_SIG: {
2019-04-16 22:11:29 -04:00
m_timeEvt.armX(eat_time(), 0U);
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::eating}
2016-12-01 10:31:49 -05:00
case Q_EXIT_SIG: {
TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
2019-04-16 22:11:29 -04:00
pe->philoNum = PHILO_ID(this);
QP::QF::PUBLISH(pe, this);
(void)m_timeEvt.disarm();
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::eating::TIMEOUT}
case TIMEOUT_SIG: {
2019-04-16 22:11:29 -04:00
status_ = tran(&thinking);
break;
}
2018-02-12 18:51:22 -05:00
//${AOs::Philo::SM::eating::EAT, DONE}
2015-05-22 20:38:16 -04:00
case EAT_SIG: // intentionally fall through
case DONE_SIG: {
2019-04-16 22:11:29 -04:00
// 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: {
2019-04-16 22:11:29 -04:00
status_ = super(&top);
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
2013-12-30 17:41:15 -05:00
} // namespace DPP
2019-04-16 22:11:29 -04:00
//$enddef${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^