327 lines
11 KiB
C++
Raw Normal View History

2013-12-30 17:41:15 -05:00
//****************************************************************************
// Model: dpp.qm
// File: ./table.cpp
//
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.
//
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
// @(/3/2) ...................................................................
#include "qp_port.h"
#include "dpp.h"
#include "bsp.h"
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
2013-12-30 17:41:15 -05:00
namespace DPP {
// @(/2/1) ...................................................................
2013-12-30 17:41:15 -05:00
class Table : public QP::QMActive {
private:
uint8_t m_fork[N_PHILO];
bool m_isHungry[N_PHILO];
public:
Table();
protected:
static QP::QState initial(Table * const me, QP::QEvt const * const e);
2013-12-30 17:41:15 -05:00
static QP::QState active (Table * const me, QP::QEvt const * const e);
static QP::QMState const active_s;
static QP::QState serving (Table * const me, QP::QEvt const * const e);
static QP::QState serving_e(Table * const me);
static QP::QMState const serving_s;
static QP::QState paused (Table * const me, QP::QEvt const * const e);
static QP::QState paused_e(Table * const me);
static QP::QState paused_x(Table * const me);
static QP::QMState const paused_s;
};
2013-12-30 17:41:15 -05:00
} // namespace DPP
namespace DPP {
// helper function to provide the RIGHT neighbour of a Philo[n]
inline uint8_t RIGHT(uint8_t const n) {
return static_cast<uint8_t>((n + (N_PHILO - 1U)) % N_PHILO);
}
// helper function to provide the LEFT neighbour of a Philo[n]
inline uint8_t LEFT(uint8_t const n) {
return static_cast<uint8_t>((n + 1U) % N_PHILO);
}
static uint8_t const FREE = static_cast<uint8_t>(0);
static uint8_t const USED = static_cast<uint8_t>(1);
static char_t const * const THINKING = &"thinking"[0];
static char_t const * const HUNGRY = &"hungry "[0];
static char_t const * const EATING = &"eating "[0];
// Local objects -------------------------------------------------------------
static Table l_table; // the single instance of the Table active object
// Global-scope objects ------------------------------------------------------
QP::QActive * const AO_Table = &l_table; // "opaque" AO pointer
2013-12-30 17:41:15 -05:00
} // namespace DPP
//............................................................................
2013-12-30 17:41:15 -05:00
namespace DPP {
// @(/2/1) ...................................................................
// @(/2/1/2) .................................................................
2013-12-30 17:41:15 -05:00
Table::Table()
: QMActive(Q_STATE_CAST(&Table::initial))
{
for (uint8_t n = 0U; n < N_PHILO; ++n) {
m_fork[n] = FREE;
m_isHungry[n] = false;
}
}
// @(/2/1/3) .................................................................
// @(/2/1/3/0)
QP::QState Table::initial(Table * const me, QP::QEvt const * const e) {
2013-12-30 17:41:15 -05:00
static QP::QActionHandler const act_[] = {
Q_ACTION_CAST(&Table::serving_e),
Q_ACTION_CAST(0)
};
(void)e; // suppress the compiler warning about unused parameter
QS_OBJ_DICTIONARY(&l_table);
QS_FUN_DICTIONARY(&QP::QHsm::top);
QS_FUN_DICTIONARY(&Table::initial);
QS_FUN_DICTIONARY(&Table::active);
QS_FUN_DICTIONARY(&Table::serving);
QS_FUN_DICTIONARY(&Table::paused);
QS_SIG_DICTIONARY(DONE_SIG, (void *)0); // global signals
QS_SIG_DICTIONARY(EAT_SIG, (void *)0);
QS_SIG_DICTIONARY(PAUSE_SIG, (void *)0);
QS_SIG_DICTIONARY(TERMINATE_SIG, (void *)0);
QS_SIG_DICTIONARY(HUNGRY_SIG, me); // signal just for Table
me->subscribe(DONE_SIG);
me->subscribe(PAUSE_SIG);
me->subscribe(TERMINATE_SIG);
for (uint8_t n = 0U; n < N_PHILO; ++n) {
me->m_fork[n] = FREE;
me->m_isHungry[n] = false;
BSP_displayPhilStat(n, THINKING);
}
2013-12-30 17:41:15 -05:00
return QM_INITIAL(&Table::serving_s, &act_[0]);
}
// @(/2/1/3/1) ...............................................................
2013-12-30 17:41:15 -05:00
QP::QMState const Table::active_s = {
static_cast<QP::QMState const *>(0),
Q_STATE_CAST(&Table::active),
Q_ACTION_CAST(0)
};
QP::QState Table::active(Table * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
// @(/2/1/3/1/0)
case TERMINATE_SIG: {
BSP_terminate(0);
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
break;
}
// @(/2/1/3/1/1)
case EAT_SIG: {
Q_ERROR();
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
break;
}
default: {
2013-12-30 17:41:15 -05:00
status_ = QM_SUPER();
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
// @(/2/1/3/1/2) .............................................................
2013-12-30 17:41:15 -05:00
QP::QMState const Table::serving_s = {
&Table::active_s,
Q_STATE_CAST(&Table::serving),
Q_ACTION_CAST(0)
};
QP::QState Table::serving_e(Table * const me) {
for (uint8_t n = 0U; n < N_PHILO; ++n) { // give permissions to eat...
if (me->m_isHungry[n]
&& (me->m_fork[LEFT(n)] == FREE)
&& (me->m_fork[n] == FREE))
{
me->m_fork[LEFT(n)] = USED;
me->m_fork[n] = USED;
TableEvt *te = Q_NEW(TableEvt, EAT_SIG);
te->philoNum = n;
QP::QF::PUBLISH(te, me);
me->m_isHungry[n] = false;
BSP_displayPhilStat(n, EATING);
}
}
return QM_ENTRY(&serving_s);
}
QP::QState Table::serving(Table * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
// @(/2/1/3/1/2/0)
case HUNGRY_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// phil ID must be in range and he must be not hungry
Q_ASSERT((n < N_PHILO) && (!me->m_isHungry[n]));
BSP_displayPhilStat(n, HUNGRY);
uint8_t m = LEFT(n);
// @(/2/1/3/1/2/0/0)
if ((me->m_fork[m] == FREE) && (me->m_fork[n] == FREE)) {
me->m_fork[m] = USED;
me->m_fork[n] = USED;
TableEvt *pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = n;
QP::QF::PUBLISH(pe, me);
BSP_displayPhilStat(n, EATING);
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
}
// @(/2/1/3/1/2/0/1)
else {
me->m_isHungry[n] = true;
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
}
break;
}
// @(/2/1/3/1/2/1)
case DONE_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// phil ID must be in range and he must be not hungry
Q_ASSERT((n < N_PHILO) && (!me->m_isHungry[n]));
BSP_displayPhilStat(n, THINKING);
uint8_t m = LEFT(n);
// both forks of Phil[n] must be used
Q_ASSERT((me->m_fork[n] == USED) && (me->m_fork[m] == USED));
me->m_fork[m] = FREE;
me->m_fork[n] = FREE;
m = RIGHT(n); // check the right neighbor
if (me->m_isHungry[m] && (me->m_fork[m] == FREE)) {
me->m_fork[n] = USED;
me->m_fork[m] = USED;
me->m_isHungry[m] = false;
TableEvt *pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = m;
QP::QF::PUBLISH(pe, me);
BSP_displayPhilStat(m, EATING);
}
m = LEFT(n); // check the left neighbor
n = LEFT(m); // left fork of the left neighbor
if (me->m_isHungry[m] && (me->m_fork[n] == FREE)) {
me->m_fork[m] = USED;
me->m_fork[n] = USED;
me->m_isHungry[m] = false;
TableEvt *pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = m;
QP::QF::PUBLISH(pe, me);
BSP_displayPhilStat(m, EATING);
}
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
break;
}
// @(/2/1/3/1/2/2)
case EAT_SIG: {
Q_ERROR();
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
break;
}
// @(/2/1/3/1/2/3)
case PAUSE_SIG: {
2013-12-30 17:41:15 -05:00
static QP::QActionHandler const act_[] = {
Q_ACTION_CAST(&Table::paused_e),
Q_ACTION_CAST(0)
};
status_ = QM_TRAN(&paused_s, &act_[0]);
break;
}
default: {
2013-12-30 17:41:15 -05:00
status_ = QM_SUPER();
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
// @(/2/1/3/1/3) .............................................................
2013-12-30 17:41:15 -05:00
QP::QMState const Table::paused_s = {
&Table::active_s,
Q_STATE_CAST(&Table::paused),
Q_ACTION_CAST(&Table::paused_x)
};
QP::QState Table::paused_e(Table * const me) {
BSP_displayPaused(1U);
return QM_ENTRY(&paused_s);
}
QP::QState Table::paused_x(Table * const me) {
BSP_displayPaused(0U);
return QM_EXIT(&paused_s);
}
QP::QState Table::paused(Table * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
// @(/2/1/3/1/3/0)
case PAUSE_SIG: {
2013-12-30 17:41:15 -05:00
static QP::QActionHandler const act_[] = {
Q_ACTION_CAST(&Table::paused_x),
Q_ACTION_CAST(&Table::serving_e),
Q_ACTION_CAST(0)
};
status_ = QM_TRAN(&serving_s, &act_[0]);
break;
}
// @(/2/1/3/1/3/1)
case HUNGRY_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// philo ID must be in range and he must be not hungry
Q_ASSERT((n < N_PHILO) && (!me->m_isHungry[n]));
me->m_isHungry[n] = true;
BSP_displayPhilStat(n, HUNGRY);
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
break;
}
// @(/2/1/3/1/3/2)
case DONE_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// phil ID must be in range and he must be not hungry
Q_ASSERT((n < N_PHILO) && (!me->m_isHungry[n]));
BSP_displayPhilStat(n, THINKING);
uint8_t m = LEFT(n);
/* both forks of Phil[n] must be used */
Q_ASSERT((me->m_fork[n] == USED) && (me->m_fork[m] == USED));
me->m_fork[m] = FREE;
me->m_fork[n] = FREE;
2013-12-30 17:41:15 -05:00
status_ = QM_HANDLED();
break;
}
default: {
2013-12-30 17:41:15 -05:00
status_ = QM_SUPER();
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
2013-12-30 17:41:15 -05:00
} // namespace DPP