| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- /*****************************************************************************
- * Model: dpp.qm
- * File: ./philo.c
- *
- * 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.
- *
- * 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.
- *****************************************************************************/
- /*${.::philo.c} ............................................................*/
- #include "qpc.h"
- #include "dpp.h"
- #include "bsp.h"
- Q_DEFINE_THIS_FILE
- /* Active object class -----------------------------------------------------*/
- /*${AOs::Philo} ............................................................*/
- typedef struct {
- /* protected: */
- QMActive super;
- /* private: */
- QTimeEvt timeEvt;
- } Philo;
- /* protected: */
- static QState Philo_initial(Philo * const me, QEvt const * const e);
- static QState Philo_thinking (Philo * const me, QEvt const * const e);
- static QState Philo_thinking_e(Philo * const me);
- static QMState const Philo_thinking_s = {
- (QMState const *)0, /* superstate (top) */
- Q_STATE_CAST(&Philo_thinking),
- Q_ACTION_CAST(&Philo_thinking_e),
- Q_ACTION_CAST(0), /* no exit action */
- Q_ACTION_CAST(0) /* no intitial tran. */
- };
- static QState Philo_hungry (Philo * const me, QEvt const * const e);
- static QState Philo_hungry_e(Philo * const me);
- static QMState const Philo_hungry_s = {
- (QMState const *)0, /* superstate (top) */
- Q_STATE_CAST(&Philo_hungry),
- Q_ACTION_CAST(&Philo_hungry_e),
- Q_ACTION_CAST(0), /* no exit action */
- Q_ACTION_CAST(0) /* no intitial tran. */
- };
- static QState Philo_eating (Philo * const me, QEvt const * const e);
- static QState Philo_eating_e(Philo * const me);
- static QState Philo_eating_x(Philo * const me);
- static QMState const Philo_eating_s = {
- (QMState const *)0, /* superstate (top) */
- Q_STATE_CAST(&Philo_eating),
- Q_ACTION_CAST(&Philo_eating_e),
- Q_ACTION_CAST(&Philo_eating_x),
- Q_ACTION_CAST(0) /* no intitial tran. */
- };
- /* Local objects -----------------------------------------------------------*/
- static Philo l_philo[N_PHILO]; /* storage for all Philos */
- #define THINK_TIME \
- (QTimeEvtCtr)((BSP_random() % BSP_TICKS_PER_SEC) + (BSP_TICKS_PER_SEC/2U))
- #define EAT_TIME \
- (QTimeEvtCtr)((BSP_random() % BSP_TICKS_PER_SEC) + BSP_TICKS_PER_SEC)
- /* helper macro to provide the ID of Philo "me_" */
- #define PHILO_ID(me_) ((uint8_t)((me_) - l_philo))
- /* Global objects ----------------------------------------------------------*/
- QMActive * const AO_Philo[N_PHILO] = { /* "opaque" pointers to Philo AO */
- &l_philo[0].super,
- // &l_philo[1].super,
- // &l_philo[2].super,
- // &l_philo[3].super,
- // &l_philo[4].super
- };
- /* Philo definition --------------------------------------------------------*/
- /*${AOs::Philo_ctor} .......................................................*/
- void Philo_ctor(void) {
- uint8_t n;
- Philo *me;
- for (n = 0U; n < N_PHILO; ++n) {
- me = &l_philo[n];
- QMActive_ctor(&me->super, Q_STATE_CAST(&Philo_initial));
- QTimeEvt_ctorX(&me->timeEvt, &me->super, TIMEOUT_SIG, 0U);
- }
- }
- /*${AOs::Philo} ............................................................*/
- /*${AOs::Philo::SM} ........................................................*/
- static QState Philo_initial(Philo * const me, QEvt const * const e) {
- static struct {
- QMState const *target;
- QActionHandler act[2];
- } const tatbl_ = { /* transition-action table */
- &Philo_thinking_s, /* target state */
- {
- Q_ACTION_CAST(&Philo_thinking_e), /* entry */
- Q_ACTION_CAST(0) /* zero terminator */
- }
- };
- /* ${AOs::Philo::SM::initial} */
- static uint8_t registered = (uint8_t)0; /* starts off with 0, per C-standard */
- (void)e; /* suppress the compiler warning about unused parameter */
- if (registered == (uint8_t)0) {
- registered = (uint8_t)1;
- QS_OBJ_DICTIONARY(&l_philo[0]);
- QS_OBJ_DICTIONARY(&l_philo[0].timeEvt);
- // QS_OBJ_DICTIONARY(&l_philo[1]);
- // QS_OBJ_DICTIONARY(&l_philo[1].timeEvt);
- // QS_OBJ_DICTIONARY(&l_philo[2]);
- // QS_OBJ_DICTIONARY(&l_philo[2].timeEvt);
- // QS_OBJ_DICTIONARY(&l_philo[3]);
- // QS_OBJ_DICTIONARY(&l_philo[3].timeEvt);
- // QS_OBJ_DICTIONARY(&l_philo[4]);
- // QS_OBJ_DICTIONARY(&l_philo[4].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 */
- QActive_subscribe(&me->super, EAT_SIG);
- return QM_TRAN_INIT(&tatbl_);
- }
- /*${AOs::Philo::SM::thinking} ..............................................*/
- /* ${AOs::Philo::SM::thinking} */
- static QState Philo_thinking_e(Philo * const me) {
- QTimeEvt_armX(&me->timeEvt, THINK_TIME, 0U);
- return QM_ENTRY(&Philo_thinking_s);
- }
- /* ${AOs::Philo::SM::thinking} */
- static QState Philo_thinking(Philo * const me, QEvt const * const e) {
- QState status_;
- switch (e->sig) {
- /* ${AOs::Philo::SM::thinking::TIMEOUT} */
- case TIMEOUT_SIG: {
- static struct {
- QMState const *target;
- QActionHandler act[2];
- } const tatbl_ = { /* transition-action table */
- &Philo_hungry_s, /* target state */
- {
- Q_ACTION_CAST(&Philo_hungry_e), /* entry */
- Q_ACTION_CAST(0) /* zero terminator */
- }
- };
- status_ = QM_TRAN(&tatbl_);
- break;
- }
- /* ${AOs::Philo::SM::thinking::EAT, DONE} */
- case EAT_SIG: /* intentionally fall through */
- case DONE_SIG: {
- /* EAT or DONE must be for other Philos than this one */
- Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
- status_ = QM_HANDLED();
- break;
- }
- default: {
- status_ = QM_SUPER();
- break;
- }
- }
- return status_;
- }
- /*${AOs::Philo::SM::hungry} ................................................*/
- /* ${AOs::Philo::SM::hungry} */
- static QState Philo_hungry_e(Philo * const me) {
- TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
- pe->philoNum = PHILO_ID(me);
- QACTIVE_POST(AO_Table, &pe->super, me);
- return QM_ENTRY(&Philo_hungry_s);
- }
- /* ${AOs::Philo::SM::hungry} */
- static QState Philo_hungry(Philo * const me, QEvt const * const e) {
- QState status_;
- switch (e->sig) {
- /* ${AOs::Philo::SM::hungry::EAT} */
- case EAT_SIG: {
- /* ${AOs::Philo::SM::hungry::EAT::[Q_EVT_CAST(TableEvt)->philoNum=~} */
- if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(me)) {
- static struct {
- QMState const *target;
- QActionHandler act[2];
- } const tatbl_ = { /* transition-action table */
- &Philo_eating_s, /* target state */
- {
- Q_ACTION_CAST(&Philo_eating_e), /* entry */
- Q_ACTION_CAST(0) /* zero terminator */
- }
- };
- status_ = QM_TRAN(&tatbl_);
- }
- else {
- status_ = QM_UNHANDLED();
- }
- break;
- }
- /* ${AOs::Philo::SM::hungry::DONE} */
- case DONE_SIG: {
- /* DONE must be for other Philos than this one */
- Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
- status_ = QM_HANDLED();
- break;
- }
- default: {
- status_ = QM_SUPER();
- break;
- }
- }
- return status_;
- }
- /*${AOs::Philo::SM::eating} ................................................*/
- /* ${AOs::Philo::SM::eating} */
- static QState Philo_eating_e(Philo * const me) {
- QTimeEvt_armX(&me->timeEvt, EAT_TIME, 0U);
- return QM_ENTRY(&Philo_eating_s);
- }
- /* ${AOs::Philo::SM::eating} */
- static QState Philo_eating_x(Philo * const me) {
- TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
- pe->philoNum = PHILO_ID(me);
- QF_PUBLISH(&pe->super, me);
- return QM_EXIT(&Philo_eating_s);
- }
- /* ${AOs::Philo::SM::eating} */
- static QState Philo_eating(Philo * const me, QEvt const * const e) {
- QState status_;
- switch (e->sig) {
- /* ${AOs::Philo::SM::eating::TIMEOUT} */
- case TIMEOUT_SIG: {
- static struct {
- QMState const *target;
- QActionHandler act[3];
- } const tatbl_ = { /* transition-action table */
- &Philo_thinking_s, /* target state */
- {
- Q_ACTION_CAST(&Philo_eating_x), /* exit */
- Q_ACTION_CAST(&Philo_thinking_e), /* entry */
- Q_ACTION_CAST(0) /* zero terminator */
- }
- };
- status_ = QM_TRAN(&tatbl_);
- break;
- }
- /* ${AOs::Philo::SM::eating::EAT, DONE} */
- case EAT_SIG: /* intentionally fall through */
- case DONE_SIG: {
- /* EAT or DONE must be for other Philos than this one */
- Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
- status_ = QM_HANDLED();
- break;
- }
- default: {
- status_ = QM_SUPER();
- break;
- }
- }
- return status_;
- }
|