/***************************************************************************** * Model: dpp.qm * File: ././table.c * * This file has been generated automatically by QP Modeler (QM). * DO NOT EDIT THIS FILE MANUALLY. * * Please visit www.state-machine.com/qm for more information. *****************************************************************************/ #include "qp_port.h" #include "dpp.h" #include "bsp.h" Q_DEFINE_THIS_FILE /* Active object class -----------------------------------------------------*/ /* @(/2/1) .................................................................*/ typedef struct TableTag { /* protected: */ QActive super; /* private: */ uint8_t fork[N_PHILO]; uint8_t isHungry[N_PHILO]; } Table; /* protected: */ static QState Table_initial(Table * const me, QEvt const * const e); 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); #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) /* Local objects -----------------------------------------------------------*/ static Table l_table; /* the single instance of the Table active object */ /* Global-scope objects ----------------------------------------------------*/ QActive * const AO_Table = &l_table.super; /* "opaque" AO pointer */ /*..........................................................................*/ /* @(/2/5) .................................................................*/ void Table_ctor(void) { uint8_t n; Table *me = &l_table; QActive_ctor(&me->super, Q_STATE_CAST(&Table_initial)); for (n = 0U; n < N_PHILO; ++n) { me->fork[n] = FREE; me->isHungry[n] = 0U; } } /* @(/2/1) .................................................................*/ /* @(/2/1/2) ...............................................................*/ /* @(/2/1/2/0) */ static QState Table_initial(Table * const me, QEvt const * const e) { uint8_t n; (void)e; /* suppress the compiler warning about unused parameter */ QS_OBJ_DICTIONARY(&l_table); QS_FUN_DICTIONARY(&QHsm_top); QS_FUN_DICTIONARY(&Table_initial); QS_FUN_DICTIONARY(&Table_serving); 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 */ QActive_subscribe(&me->super, DONE_SIG); QActive_subscribe(&me->super, PAUSE_SIG); QActive_subscribe(&me->super, TERMINATE_SIG); for (n = 0U; n < N_PHILO; ++n) { me->fork[n] = FREE; me->isHungry[n] = 0U; BSP_displayPhilStat(n, "thinking"); } return Q_TRAN(&Table_serving); } /* @(/2/1/2/1) .............................................................*/ static QState Table_active(Table * const me, QEvt const * const e) { QState status; switch (e->sig) { /* @(/2/1/2/1/0) */ case TERMINATE_SIG: { BSP_terminate(0); status = Q_HANDLED(); break; } /* @(/2/1/2/1/1) */ case EAT_SIG: { Q_ERROR(); status = Q_HANDLED(); break; } default: { status = Q_SUPER(&QHsm_top); break; } } return status; } /* @(/2/1/2/1/2) ...........................................................*/ static QState Table_serving(Table * const me, QEvt const * const e) { QState status; switch (e->sig) { /* @(/2/1/2/1/2) */ case Q_ENTRY_SIG: { uint8_t n; for (n = 0U; n < N_PHILO; ++n) { /* give permissions to eat... */ if ((me->isHungry[n] != 0U) && (me->fork[LEFT(n)] == FREE) && (me->fork[n] == FREE)) { TableEvt *te; me->fork[LEFT(n)] = USED; me->fork[n] = USED; te = Q_NEW(TableEvt, EAT_SIG); te->philoNum = n; QF_PUBLISH(&te->super, me); me->isHungry[n] = 0U; BSP_displayPhilStat(n, "eating "); } } status = Q_HANDLED(); break; } /* @(/2/1/2/1/2/0) */ case HUNGRY_SIG: { uint8_t n, m; n = Q_EVT_CAST(TableEvt)->philoNum; /* phil ID must be in range and he must be not hungry */ Q_ASSERT((n < N_PHILO) && (me->isHungry[n] == 0U)); BSP_displayPhilStat(n, "hungry "); m = LEFT(n); /* @(/2/1/2/1/2/0/0) */ if ((me->fork[m] == FREE) && (me->fork[n] == FREE)) { TableEvt *pe; me->fork[m] = USED; me->fork[n] = USED; pe = Q_NEW(TableEvt, EAT_SIG); pe->philoNum = n; QF_PUBLISH(&pe->super, me); BSP_displayPhilStat(n, "eating "); status = Q_HANDLED(); } /* @(/2/1/2/1/2/0/1) */ else { me->isHungry[n] = 1U; status = Q_HANDLED(); } break; } /* @(/2/1/2/1/2/1) */ case DONE_SIG: { uint8_t n, m; TableEvt *pe; n = Q_EVT_CAST(TableEvt)->philoNum; /* phil ID must be in range and he must be not hungry */ Q_ASSERT((n < N_PHILO) && (me->isHungry[n] == 0U)); BSP_displayPhilStat(n, "thinking"); 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 */ if ((me->isHungry[m] != 0U) && (me->fork[m] == FREE)) { me->fork[n] = USED; me->fork[m] = USED; me->isHungry[m] = 0U; pe = Q_NEW(TableEvt, EAT_SIG); pe->philoNum = m; QF_PUBLISH(&pe->super, me); 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; pe = Q_NEW(TableEvt, EAT_SIG); pe->philoNum = m; QF_PUBLISH(&pe->super, me); BSP_displayPhilStat(m, "eating "); } status = Q_HANDLED(); break; } /* @(/2/1/2/1/2/2) */ case EAT_SIG: { Q_ERROR(); status = Q_HANDLED(); break; } /* @(/2/1/2/1/2/3) */ case PAUSE_SIG: { status = Q_TRAN(&Table_paused); break; } default: { status = Q_SUPER(&Table_active); break; } } return status; } /* @(/2/1/2/1/3) ...........................................................*/ static QState Table_paused(Table * const me, QEvt const * const e) { QState status; switch (e->sig) { /* @(/2/1/2/1/3) */ case Q_ENTRY_SIG: { BSP_displayPaused(1U); status = Q_HANDLED(); break; } /* @(/2/1/2/1/3) */ case Q_EXIT_SIG: { BSP_displayPaused(0U); status = Q_HANDLED(); break; } /* @(/2/1/2/1/3/0) */ case PAUSE_SIG: { status = Q_TRAN(&Table_serving); break; } /* @(/2/1/2/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->isHungry[n] == 0U)); me->isHungry[n] = 1U; BSP_displayPhilStat(n, "hungry "); status = Q_HANDLED(); break; } /* @(/2/1/2/1/3/2) */ case DONE_SIG: { uint8_t n, m; n = Q_EVT_CAST(TableEvt)->philoNum; /* phil ID must be in range and he must be not hungry */ Q_ASSERT((n < N_PHILO) && (me->isHungry[n] == 0U)); BSP_displayPhilStat(n, "thinking"); 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; }