262 lines
8.7 KiB
C++
Raw Normal View History

2013-12-30 17:41:15 -05:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
// Model: dpp.qm
2013-10-10 20:01:51 -04:00
// File: ./philo.cpp
2012-08-14 18:00:48 -04:00
//
2013-10-10 20:01:51 -04:00
// This code has been generated by QM tool (see state-machine.com/qm).
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
2012-08-14 18:00:48 -04:00
//
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.
2013-12-30 17:41:15 -05:00
//****************************************************************************
2013-10-10 20:01:51 -04:00
// @(/4/1) ...................................................................
2012-08-14 18:00:48 -04:00
#include "qp_port.h"
#include "dpp.h"
#include "bsp.h"
namespace DPP {
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
2013-12-30 17:41:15 -05:00
namespace DPP {
2013-10-10 20:01:51 -04:00
// @(/3/0) ...................................................................
2013-12-30 17:41:15 -05:00
class Philo : public QP::QMActive {
2012-08-14 18:00:48 -04:00
private:
QP::QTimeEvt m_timeEvt;
public:
Philo();
protected:
static QP::QState initial(Philo * const me, QP::QEvt const * const e);
2013-12-30 17:41:15 -05:00
static QP::QState thinking (Philo * const me, QP::QEvt const * const e);
static QP::QState thinking_e(Philo * const me);
static QP::QState thinking_x(Philo * const me);
static QP::QMState const thinking_s;
static QP::QState hungry (Philo * const me, QP::QEvt const * const e);
static QP::QState hungry_e(Philo * const me);
static QP::QMState const hungry_s;
static QP::QState eating (Philo * const me, QP::QEvt const * const e);
static QP::QState eating_e(Philo * const me);
static QP::QState eating_x(Philo * const me);
static QP::QMState const eating_s;
2012-08-14 18:00:48 -04:00
};
2013-12-30 17:41:15 -05:00
} // namespace DPP
2013-10-10 20:01:51 -04:00
2012-08-14 18:00:48 -04:00
// 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 "me"
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]
};
// Philo definition ----------------------------------------------------------
2013-12-30 17:41:15 -05:00
namespace DPP {
2013-10-10 20:01:51 -04:00
// @(/3/0) ...................................................................
// @(/3/0/1) .................................................................
2013-12-30 17:41:15 -05:00
Philo::Philo()
: QMActive(Q_STATE_CAST(&Philo::initial)),
m_timeEvt(this, TIMEOUT_SIG, 0U)
{}
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
// @(/3/0/2) .................................................................
// @(/3/0/2/0)
2012-08-14 18:00:48 -04:00
QP::QState Philo::initial(Philo * const me, QP::QEvt const * const e) {
2013-12-30 17:41:15 -05:00
static QP::QActionHandler const act_[] = {
Q_ACTION_CAST(&Philo::thinking_e),
Q_ACTION_CAST(0)
};
2012-08-14 18:00:48 -04:00
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, me); // signal for each Philos
QS_SIG_DICTIONARY(TIMEOUT_SIG, me); // signal for each Philos
me->subscribe(EAT_SIG);
2013-12-30 17:41:15 -05:00
return QM_INITIAL(&Philo::thinking_s, act_);
2012-08-14 18:00:48 -04:00
}
2013-10-10 20:01:51 -04:00
// @(/3/0/2/1) ...............................................................
2013-12-30 17:41:15 -05:00
QP::QMState const Philo::thinking_s = {
static_cast<QP::QMState const *>(0),
Q_STATE_CAST(&Philo::thinking),
Q_ACTION_CAST(&Philo::thinking_x)
};
QP::QState Philo::thinking_e(Philo * const me) {
me->m_timeEvt.armX(think_time(), 0U);
return QM_ENTRY(&thinking_s);
}
QP::QState Philo::thinking_x(Philo * const me) {
(void)me->m_timeEvt.disarm();
return QM_EXIT(&thinking_s);
}
2012-08-14 18:00:48 -04:00
QP::QState Philo::thinking(Philo * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
2012-08-14 18:00:48 -04:00
switch (e->sig) {
2013-10-10 20:01:51 -04:00
// @(/3/0/2/1/0)
2012-08-14 18:00:48 -04:00
case TIMEOUT_SIG: {
2013-12-30 17:41:15 -05:00
static QP::QActionHandler const act_[] = {
Q_ACTION_CAST(&Philo::thinking_x),
Q_ACTION_CAST(&Philo::hungry_e),
Q_ACTION_CAST(0)
};
status_ = QM_TRAN(&hungry_s, act_);
2012-08-14 18:00:48 -04:00
break;
}
2013-10-10 20:01:51 -04:00
// @(/3/0/2/1/1)
2013-12-30 17:41:15 -05:00
case EAT_SIG: /* intentionally fall through */
2012-08-14 18:00:48 -04:00
case DONE_SIG: {
/* EAT or DONE must be for other Philos than this one */
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
2012-08-14 18:00:48 -04:00
break;
}
default: {
2013-12-30 17:41:15 -05:00
status_ = QM_SUPER();
2012-08-14 18:00:48 -04:00
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
2012-08-14 18:00:48 -04:00
}
2013-10-10 20:01:51 -04:00
// @(/3/0/2/2) ...............................................................
2013-12-30 17:41:15 -05:00
QP::QMState const Philo::hungry_s = {
static_cast<QP::QMState const *>(0),
Q_STATE_CAST(&Philo::hungry),
Q_ACTION_CAST(0)
};
QP::QState Philo::hungry_e(Philo * const me) {
AO_Table->POST(Q_NEW(TableEvt, HUNGRY_SIG, PHILO_ID(me)), me);
return QM_ENTRY(&hungry_s);
}
2012-08-14 18:00:48 -04:00
QP::QState Philo::hungry(Philo * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
2012-08-14 18:00:48 -04:00
switch (e->sig) {
2013-10-10 20:01:51 -04:00
// @(/3/0/2/2/0)
2012-08-14 18:00:48 -04:00
case EAT_SIG: {
2013-10-10 20:01:51 -04:00
// @(/3/0/2/2/0/0)
2012-08-14 18:00:48 -04:00
if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(me)) {
2013-12-30 17:41:15 -05:00
static QP::QActionHandler const act_[] = {
Q_ACTION_CAST(&Philo::eating_e),
Q_ACTION_CAST(0)
};
status_ = QM_TRAN(&eating_s, act_);
2012-08-14 18:00:48 -04:00
}
else {
2013-12-30 17:41:15 -05:00
status_ = QM_UNHANDLED();
2012-08-14 18:00:48 -04:00
}
break;
}
2013-10-10 20:01:51 -04:00
// @(/3/0/2/2/1)
2012-08-14 18:00:48 -04:00
case DONE_SIG: {
/* DONE must be for other Philos than this one */
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
2012-08-14 18:00:48 -04:00
break;
}
default: {
2013-12-30 17:41:15 -05:00
status_ = QM_SUPER();
2012-08-14 18:00:48 -04:00
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
2012-08-14 18:00:48 -04:00
}
2013-10-10 20:01:51 -04:00
// @(/3/0/2/3) ...............................................................
2013-12-30 17:41:15 -05:00
QP::QMState const Philo::eating_s = {
static_cast<QP::QMState const *>(0),
Q_STATE_CAST(&Philo::eating),
Q_ACTION_CAST(&Philo::eating_x)
};
QP::QState Philo::eating_e(Philo * const me) {
me->m_timeEvt.armX(think_time(), 0U);
return QM_ENTRY(&eating_s);
}
QP::QState Philo::eating_x(Philo * const me) {
QP::QF::PUBLISH(Q_NEW(TableEvt, DONE_SIG, PHILO_ID(me)), me);
(void)me->m_timeEvt.disarm();
return QM_EXIT(&eating_s);
}
2012-08-14 18:00:48 -04:00
QP::QState Philo::eating(Philo * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
2012-08-14 18:00:48 -04:00
switch (e->sig) {
2013-10-10 20:01:51 -04:00
// @(/3/0/2/3/0)
2012-08-14 18:00:48 -04:00
case TIMEOUT_SIG: {
2013-12-30 17:41:15 -05:00
static QP::QActionHandler const act_[] = {
Q_ACTION_CAST(&Philo::eating_x),
Q_ACTION_CAST(&Philo::thinking_e),
Q_ACTION_CAST(0)
};
status_ = QM_TRAN(&thinking_s, act_);
2012-08-14 18:00:48 -04:00
break;
}
2013-10-10 20:01:51 -04:00
// @(/3/0/2/3/1)
2013-12-30 17:41:15 -05:00
case EAT_SIG: /* intentionally fall through */
2012-08-14 18:00:48 -04:00
case DONE_SIG: {
/* EAT or DONE must be for other Philos than this one */
Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
2012-08-14 18:00:48 -04:00
break;
}
default: {
2013-12-30 17:41:15 -05:00
status_ = QM_SUPER();
2012-08-14 18:00:48 -04:00
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
2012-08-14 18:00:48 -04:00
}
2013-12-30 17:41:15 -05:00
} // namespace DPP
2012-08-14 18:00:48 -04:00
} // namespace DPP