Dining Philosopher Problem example with MSM state machines The array of static insts of the Philo class (Singleton pattern) : QActive(&initial), m_timeEvt(this, TIMEOUT_SIG, 0U) 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(&Philo::inst[0]); QS_OBJ_DICTIONARY(&Philo::inst[0].m_timeEvt); QS_OBJ_DICTIONARY(&Philo::inst[1]); QS_OBJ_DICTIONARY(&Philo::inst[1].m_timeEvt); QS_OBJ_DICTIONARY(&Philo::inst[2]); QS_OBJ_DICTIONARY(&Philo::inst[2].m_timeEvt); QS_OBJ_DICTIONARY(&Philo::inst[3]); QS_OBJ_DICTIONARY(&Philo::inst[3].m_timeEvt); QS_OBJ_DICTIONARY(&Philo::inst[4]); QS_OBJ_DICTIONARY(&Philo::inst[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(TIMEOUT_SIG, nullptr); } subscribe(EAT_SIG); subscribe(TEST_SIG); m_timeEvt.armX(think_time(), 0U); (void)m_timeEvt.disarm(); /* EAT or DONE must be for other Philos than this one */ Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this)); TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG); pe->philoNum = PHILO_ID(this); AO_Table->POST(pe, this); Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(this) /* DONE must be for other Philos than this one */ Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this)); m_timeEvt.armX(eat_time(), 0U); TableEvt *pe = Q_NEW(TableEvt, DONE_SIG); pe->philoNum = PHILO_ID(this); QP::QActive::PUBLISH(pe, this); (void)m_timeEvt.disarm(); /* EAT or DONE must be for other Philos than this one */ Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(this)); The only static inst of the Table class (Singleton pattern) : QActive(&initial) for (uint8_t n = 0U; n < N_PHILO; ++n) { m_fork[n] = FREE; m_isHungry[n] = false; } (void)e; // suppress the compiler warning about unused parameter QS_OBJ_DICTIONARY(&Table::inst); QS_SIG_DICTIONARY(DONE_SIG, nullptr); // global signals QS_SIG_DICTIONARY(EAT_SIG, nullptr); QS_SIG_DICTIONARY(PAUSE_SIG, nullptr); QS_SIG_DICTIONARY(SERVE_SIG, nullptr); QS_SIG_DICTIONARY(TEST_SIG, nullptr); QS_SIG_DICTIONARY(HUNGRY_SIG, this); // signal just for Table subscribe(DONE_SIG); subscribe(PAUSE_SIG); subscribe(SERVE_SIG); subscribe(TEST_SIG); for (uint8_t n = 0U; n < N_PHILO; ++n) { m_fork[n] = FREE; m_isHungry[n] = false; BSP::displayPhilStat(n, THINKING); } Q_ERROR(); for (uint8_t n = 0U; n < N_PHILO; ++n) { // give permissions to eat... if (m_isHungry[n] && (m_fork[LEFT(n)] == FREE) && (m_fork[n] == FREE)) { m_fork[LEFT(n)] = USED; m_fork[n] = USED; TableEvt *te = Q_NEW(TableEvt, EAT_SIG); te->philoNum = n; QP::QActive::PUBLISH(te, this); m_isHungry[n] = false; BSP::displayPhilStat(n, EATING); } } 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) && (!m_isHungry[n])); BSP::displayPhilStat(n, HUNGRY); uint8_t m = LEFT(n); (m_fork[m] == FREE) && (m_fork[n] == FREE) m_fork[m] = USED; m_fork[n] = USED; TableEvt *pe = Q_NEW(TableEvt, EAT_SIG); pe->philoNum = n; QP::QActive::PUBLISH(pe, this); BSP::displayPhilStat(n, EATING); else m_isHungry[n] = true; 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) && (!m_isHungry[n])); BSP::displayPhilStat(n, THINKING); uint8_t m = LEFT(n); // both forks of Phil[n] must be used Q_ASSERT((m_fork[n] == USED) && (m_fork[m] == USED)); m_fork[m] = FREE; m_fork[n] = FREE; m = RIGHT(n); // check the right neighbor if (m_isHungry[m] && (m_fork[m] == FREE)) { m_fork[n] = USED; m_fork[m] = USED; m_isHungry[m] = false; TableEvt *pe = Q_NEW(TableEvt, EAT_SIG); pe->philoNum = m; QP::QActive::PUBLISH(pe, this); BSP::displayPhilStat(m, EATING); } m = LEFT(n); // check the left neighbor n = LEFT(m); // left fork of the left neighbor if (m_isHungry[m] && (m_fork[n] == FREE)) { m_fork[m] = USED; m_fork[n] = USED; m_isHungry[m] = false; TableEvt *pe = Q_NEW(TableEvt, EAT_SIG); pe->philoNum = m; QP::QActive::PUBLISH(pe, this); BSP::displayPhilStat(m, EATING); } Q_ERROR(); BSP::displayPaused(1U); BSP::displayPaused(0U); 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) && (!m_isHungry[n])); m_isHungry[n] = true; BSP::displayPhilStat(n, HUNGRY); 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) && (!m_isHungry[n])); BSP::displayPhilStat(n, THINKING); uint8_t m = LEFT(n); /* both forks of Phil[n] must be used */ Q_ASSERT((m_fork[n] == USED) && (m_fork[m] == USED)); m_fork[m] = FREE; m_fork[n] = FREE; = { // "opaque" pointers to Philo AO &Philo::inst[0], &Philo::inst[1], &Philo::inst[2], &Philo::inst[3], &Philo::inst[4] }; = &Table::inst; // "opaque" pointer to Table AO #ifndef DPP_HPP #define DPP_HPP namespace DPP { enum DPPSignals { TIMEOUT_SIG = QP::Q_USER_SIG, // time event timeout EAT_SIG, // published by Table to let a philosopher eat DONE_SIG, // published by Philosopher when done eating PAUSE_SIG, // published by BSP to pause the application SERVE_SIG, // published by BSP to serve re-start serving forks TEST_SIG, // published by BSP to test the application MAX_PUB_SIG, // the last published signal HUNGRY_SIG, // posted direclty to Table from hungry Philo MAX_SIG // the last signal }; } // namespace DPP $declare${Events::TableEvt} // number of philosophers #define N_PHILO ((uint8_t)5) $declare${AOs::AO_Philo[N_PHILO]} $declare${AOs::AO_Table} #ifdef QXK_HPP $declare${AOs::XT_Test1} $declare${AOs::XT_Test2} #endif // QXK_HPP #endif // DPP_HPP #include "qpcpp.hpp" #include "dpp.hpp" #include "bsp.hpp" Q_DEFINE_THIS_FILE // Active object class ------------------------------------------------------- $declare${AOs::Philo} 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) { return static_cast<uint8_t>(me - &Philo::inst[0]); } } // namespace DPP $define${AOs::AO_Philo[N_PHILO]} $define${AOs::Philo} #include "qpcpp.hpp" #include "dpp.hpp" #include "bsp.hpp" Q_DEFINE_THIS_FILE // Active object class ------------------------------------------------------- $declare(AOs::Table) 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); } constexpr uint8_t FREE = static_cast<uint8_t>(0); constexpr uint8_t USED = static_cast<uint8_t>(1); constexpr char const * const THINKING = &"thinking"[0]; constexpr char const * const HUNGRY = &"hungry "[0]; constexpr char const * const EATING = &"eating "[0]; } // namespace DPP $define${AOs::AO_Table} $define(AOs::Table)