/*$file${.::table.c} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/ /* * Model: dpp-comp.qm * File: ${.::table.c} * * This code has been generated by QM 5.2.4 . * DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost. * * SPDX-License-Identifier: GPL-3.0-or-later * * This generated code is open source software: you can redistribute it under * the terms of the GNU General Public License as published by the Free * Software Foundation. * * This code 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. * * NOTE: * Alternatively, this generated code may be distributed under the terms * of Quantum Leaps commercial licenses, which expressly supersede the GNU * General Public License and are specifically designed for licensees * interested in retaining the proprietary status of their code. * * Contact information: * * */ /*$endhead${.::table.c} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/ #include "qpc.h" #include "dpp.h" #include "bsp.h" Q_DEFINE_THIS_FILE /*$declare${Cont::Table} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/ /*${Cont::Table} ...........................................................*/ typedef struct Table { /* protected: */ QActive super; /* public: */ /* private: */ uint8_t fork[N_PHILO]; uint8_t isHungry[N_PHILO]; } Table; extern Table Table_inst; /* protected: */ static QState Table_initial(Table * const me, void const * const par); static QState Table_active(Table * const me, QEvt const * const e); static QState Table_serving(Table * const me, QEvt const * const e); static QState Table_paused(Table * const me, QEvt const * const e); /*$enddecl${Cont::Table} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/ #define RIGHT(n_) ((uint8_t)(((n_) + (N_PHILO - 1U)) % N_PHILO)) #define LEFT(n_) ((uint8_t)(((n_) + 1U) % N_PHILO)) #define FREE ((uint8_t)0) #define USED ((uint8_t)1) /* definition of the whole "Cont" package */ /*$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/ /* Check for the minimum required QP version */ #if (QP_VERSION < 700U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U)) #error qpc version 7.0.0 or higher required #endif /*$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/ /*$define${Cont} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/ /*${Cont::Table} ...........................................................*/ Table Table_inst; /*${Cont::Table::SM} .......................................................*/ static QState Table_initial(Table * const me, void const * const par) { /*${Cont::Table::SM::initial} */ (void)par; /* unused parameter */ QS_OBJ_DICTIONARY(&Table_inst); QActive_subscribe(&me->super, PAUSE_SIG); QActive_subscribe(&me->super, SERVE_SIG); QActive_subscribe(&me->super, TEST_SIG); for (uint8_t n = 0U; n < N_PHILO; ++n) { me->fork[n] = FREE; me->isHungry[n] = 0U; QHSM_INIT(SM_Philo[n], (void *)0, QS_AP_ID + n); BSP_displayPhilStat(n, "thinking"); } QS_FUN_DICTIONARY(&Table_active); QS_FUN_DICTIONARY(&Table_serving); QS_FUN_DICTIONARY(&Table_paused); return Q_TRAN(&Table_serving); } /*${Cont::Table::SM::active} ...............................................*/ static QState Table_active(Table * const me, QEvt const * const e) { QState status_; switch (e->sig) { /*${Cont::Table::SM::active::TIMEOUT} */ case TIMEOUT_SIG: { uint16_t n = Q_EVT_CAST(CompTimeEvt)->compNum; QHSM_DISPATCH(SM_Philo[n], e, QS_AP_ID + n); status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::EAT} */ case EAT_SIG: { Q_ERROR(); status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::TEST} */ case TEST_SIG: { status_ = Q_HANDLED(); break; } default: { status_ = Q_SUPER(&QHsm_top); break; } } return status_; } /*${Cont::Table::SM::active::serving} ......................................*/ static QState Table_serving(Table * const me, QEvt const * const e) { QState status_; switch (e->sig) { /*${Cont::Table::SM::active::serving} */ case Q_ENTRY_SIG: { for (uint8_t n = 0U; n < N_PHILO; ++n) { /* give permissions to eat... */ if ((me->isHungry[n] != 0U) && (me->fork[LEFT(n)] == FREE) && (me->fork[n] == FREE)) { me->fork[LEFT(n)] = USED; me->fork[n] = USED; /* synchronoulsy dispatch EAT event to the Philo component */ TableEvt evt; evt.super.sig = EAT_SIG; evt.philoNum = n; QHSM_DISPATCH(SM_Philo[n], &evt.super, QS_AP_ID + n); me->isHungry[n] = 0U; BSP_displayPhilStat(n, "eating "); } } status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::serving::HUNGRY} */ case HUNGRY_SIG: { /* find the index of the Philo from the event */ 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->isHungry[n])); BSP_displayPhilStat(n, "hungry "); uint8_t m = LEFT(n); /*${Cont::Table::SM::active::serving::HUNGRY::[bothfree]} */ if ((me->fork[m] == FREE) && (me->fork[n] == FREE)) { me->fork[m] = USED; me->fork[n] = USED; /* synchronoulsy dispatch EAT event to the Philo component */ TableEvt evt; evt.super.sig = EAT_SIG; evt.philoNum = n; QHSM_DISPATCH(SM_Philo[n], &evt.super, QS_AP_ID + n); BSP_displayPhilStat(n, "eating "); status_ = Q_HANDLED(); } /*${Cont::Table::SM::active::serving::HUNGRY::[else]} */ else { me->isHungry[n] = 1U; status_ = Q_HANDLED(); } break; } /*${Cont::Table::SM::active::serving::DONE} */ case DONE_SIG: { /* find the index of the Philo from the event */ 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->isHungry[n])); BSP_displayPhilStat(n, "thinking"); uint8_t m = LEFT(n); /* both forks of Phil[n] must be used */ Q_ASSERT((me->fork[n] == USED) && (me->fork[m] == USED)); me->fork[m] = FREE; me->fork[n] = FREE; m = RIGHT(n); /* check the right neighbor */ TableEvt evt; if ((me->isHungry[m] != 0U) && (me->fork[m] == FREE)) { me->fork[n] = USED; me->fork[m] = USED; me->isHungry[m] = 0U; /* synchronoulsy dispatch EAT event to the Philo component */ evt.super.sig = EAT_SIG; evt.philoNum = m; QHSM_DISPATCH(SM_Philo[m], &evt.super, QS_AP_ID + m); BSP_displayPhilStat(m, "eating "); } m = LEFT(n); /* check the left neighbor */ n = LEFT(m); /* left fork of the left neighbor */ if ((me->isHungry[m] != 0U) && (me->fork[n] == FREE)) { me->fork[m] = USED; me->fork[n] = USED; me->isHungry[m] = 0U; /* synchronoulsy dispatch EAT event to the Philo component */ evt.super.sig = EAT_SIG; evt.philoNum = m; QHSM_DISPATCH(SM_Philo[m], &evt.super, QS_AP_ID + m); BSP_displayPhilStat(m, "eating "); } status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::serving::EAT} */ case EAT_SIG: { Q_ERROR(); status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::serving::PAUSE} */ case PAUSE_SIG: { status_ = Q_TRAN(&Table_paused); break; } default: { status_ = Q_SUPER(&Table_active); break; } } return status_; } /*${Cont::Table::SM::active::paused} .......................................*/ static QState Table_paused(Table * const me, QEvt const * const e) { QState status_; switch (e->sig) { /*${Cont::Table::SM::active::paused} */ case Q_ENTRY_SIG: { BSP_displayPaused(1U); status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::paused} */ case Q_EXIT_SIG: { BSP_displayPaused(0U); status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::paused::SERVE} */ case SERVE_SIG: { status_ = Q_TRAN(&Table_serving); break; } /*${Cont::Table::SM::active::paused::HUNGRY} */ case HUNGRY_SIG: { /* find the index of the Philo from the event */ 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->isHungry[n])); me->isHungry[n] = 1U; BSP_displayPhilStat(n, "hungry "); status_ = Q_HANDLED(); break; } /*${Cont::Table::SM::active::paused::DONE} */ case DONE_SIG: { /* find the index of the Philo from the event */ 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->isHungry[n])); BSP_displayPhilStat(n, "thinking"); uint8_t m = LEFT(n); /* both forks of Phil[n] must be used */ Q_ASSERT((me->fork[n] == USED) && (me->fork[m] == USED)); me->fork[m] = FREE; me->fork[n] = FREE; status_ = Q_HANDLED(); break; } default: { status_ = Q_SUPER(&Table_active); break; } } return status_; } /*${Cont::AO_Table} ........................................................*/ QActive * const AO_Table = &Table_inst.super; /* opaque pointer */ /*${Cont::Table_ctor} ......................................................*/ void Table_ctor(void) { Table *me = &Table_inst; QActive_ctor(&me->super, Q_STATE_CAST(&Table_initial)); for (uint8_t n = 0U; n < N_PHILO; ++n) { me->fork[n] = FREE; me->isHungry[n] = 0U; Philo_ctor(n); /* create the Philo component */ } } /*$enddef${Cont} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/