philo.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*****************************************************************************
  2. * Model: dpp.qm
  3. * File: ./philo.c
  4. *
  5. * This code has been generated by QM tool (see state-machine.com/qm).
  6. * DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
  7. *
  8. * This program is open source software: you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as published
  10. * by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *****************************************************************************/
  17. /*${.::philo.c} ............................................................*/
  18. #include "qpc.h"
  19. #include "dpp.h"
  20. #include "bsp.h"
  21. Q_DEFINE_THIS_FILE
  22. /* Active object class -----------------------------------------------------*/
  23. /*${AOs::Philo} ............................................................*/
  24. typedef struct {
  25. /* protected: */
  26. QMActive super;
  27. /* private: */
  28. QTimeEvt timeEvt;
  29. } Philo;
  30. /* protected: */
  31. static QState Philo_initial(Philo * const me, QEvt const * const e);
  32. static QState Philo_thinking (Philo * const me, QEvt const * const e);
  33. static QState Philo_thinking_e(Philo * const me);
  34. static QMState const Philo_thinking_s = {
  35. (QMState const *)0, /* superstate (top) */
  36. Q_STATE_CAST(&Philo_thinking),
  37. Q_ACTION_CAST(&Philo_thinking_e),
  38. Q_ACTION_CAST(0), /* no exit action */
  39. Q_ACTION_CAST(0) /* no intitial tran. */
  40. };
  41. static QState Philo_hungry (Philo * const me, QEvt const * const e);
  42. static QState Philo_hungry_e(Philo * const me);
  43. static QMState const Philo_hungry_s = {
  44. (QMState const *)0, /* superstate (top) */
  45. Q_STATE_CAST(&Philo_hungry),
  46. Q_ACTION_CAST(&Philo_hungry_e),
  47. Q_ACTION_CAST(0), /* no exit action */
  48. Q_ACTION_CAST(0) /* no intitial tran. */
  49. };
  50. static QState Philo_eating (Philo * const me, QEvt const * const e);
  51. static QState Philo_eating_e(Philo * const me);
  52. static QState Philo_eating_x(Philo * const me);
  53. static QMState const Philo_eating_s = {
  54. (QMState const *)0, /* superstate (top) */
  55. Q_STATE_CAST(&Philo_eating),
  56. Q_ACTION_CAST(&Philo_eating_e),
  57. Q_ACTION_CAST(&Philo_eating_x),
  58. Q_ACTION_CAST(0) /* no intitial tran. */
  59. };
  60. /* Local objects -----------------------------------------------------------*/
  61. static Philo l_philo[N_PHILO]; /* storage for all Philos */
  62. #define THINK_TIME \
  63. (QTimeEvtCtr)((BSP_random() % BSP_TICKS_PER_SEC) + (BSP_TICKS_PER_SEC/2U))
  64. #define EAT_TIME \
  65. (QTimeEvtCtr)((BSP_random() % BSP_TICKS_PER_SEC) + BSP_TICKS_PER_SEC)
  66. /* helper macro to provide the ID of Philo "me_" */
  67. #define PHILO_ID(me_) ((uint8_t)((me_) - l_philo))
  68. /* Global objects ----------------------------------------------------------*/
  69. QMActive * const AO_Philo[N_PHILO] = { /* "opaque" pointers to Philo AO */
  70. &l_philo[0].super,
  71. // &l_philo[1].super,
  72. // &l_philo[2].super,
  73. // &l_philo[3].super,
  74. // &l_philo[4].super
  75. };
  76. /* Philo definition --------------------------------------------------------*/
  77. /*${AOs::Philo_ctor} .......................................................*/
  78. void Philo_ctor(void) {
  79. uint8_t n;
  80. Philo *me;
  81. for (n = 0U; n < N_PHILO; ++n) {
  82. me = &l_philo[n];
  83. QMActive_ctor(&me->super, Q_STATE_CAST(&Philo_initial));
  84. QTimeEvt_ctorX(&me->timeEvt, &me->super, TIMEOUT_SIG, 0U);
  85. }
  86. }
  87. /*${AOs::Philo} ............................................................*/
  88. /*${AOs::Philo::SM} ........................................................*/
  89. static QState Philo_initial(Philo * const me, QEvt const * const e) {
  90. static struct {
  91. QMState const *target;
  92. QActionHandler act[2];
  93. } const tatbl_ = { /* transition-action table */
  94. &Philo_thinking_s, /* target state */
  95. {
  96. Q_ACTION_CAST(&Philo_thinking_e), /* entry */
  97. Q_ACTION_CAST(0) /* zero terminator */
  98. }
  99. };
  100. /* ${AOs::Philo::SM::initial} */
  101. static uint8_t registered = (uint8_t)0; /* starts off with 0, per C-standard */
  102. (void)e; /* suppress the compiler warning about unused parameter */
  103. if (registered == (uint8_t)0) {
  104. registered = (uint8_t)1;
  105. QS_OBJ_DICTIONARY(&l_philo[0]);
  106. QS_OBJ_DICTIONARY(&l_philo[0].timeEvt);
  107. // QS_OBJ_DICTIONARY(&l_philo[1]);
  108. // QS_OBJ_DICTIONARY(&l_philo[1].timeEvt);
  109. // QS_OBJ_DICTIONARY(&l_philo[2]);
  110. // QS_OBJ_DICTIONARY(&l_philo[2].timeEvt);
  111. // QS_OBJ_DICTIONARY(&l_philo[3]);
  112. // QS_OBJ_DICTIONARY(&l_philo[3].timeEvt);
  113. // QS_OBJ_DICTIONARY(&l_philo[4]);
  114. // QS_OBJ_DICTIONARY(&l_philo[4].timeEvt);
  115. QS_FUN_DICTIONARY(&Philo_initial);
  116. QS_FUN_DICTIONARY(&Philo_thinking);
  117. QS_FUN_DICTIONARY(&Philo_hungry);
  118. QS_FUN_DICTIONARY(&Philo_eating);
  119. }
  120. QS_SIG_DICTIONARY(HUNGRY_SIG, me); /* signal for each Philos */
  121. QS_SIG_DICTIONARY(TIMEOUT_SIG, me); /* signal for each Philos */
  122. QActive_subscribe(&me->super, EAT_SIG);
  123. return QM_TRAN_INIT(&tatbl_);
  124. }
  125. /*${AOs::Philo::SM::thinking} ..............................................*/
  126. /* ${AOs::Philo::SM::thinking} */
  127. static QState Philo_thinking_e(Philo * const me) {
  128. QTimeEvt_armX(&me->timeEvt, THINK_TIME, 0U);
  129. return QM_ENTRY(&Philo_thinking_s);
  130. }
  131. /* ${AOs::Philo::SM::thinking} */
  132. static QState Philo_thinking(Philo * const me, QEvt const * const e) {
  133. QState status_;
  134. switch (e->sig) {
  135. /* ${AOs::Philo::SM::thinking::TIMEOUT} */
  136. case TIMEOUT_SIG: {
  137. static struct {
  138. QMState const *target;
  139. QActionHandler act[2];
  140. } const tatbl_ = { /* transition-action table */
  141. &Philo_hungry_s, /* target state */
  142. {
  143. Q_ACTION_CAST(&Philo_hungry_e), /* entry */
  144. Q_ACTION_CAST(0) /* zero terminator */
  145. }
  146. };
  147. status_ = QM_TRAN(&tatbl_);
  148. break;
  149. }
  150. /* ${AOs::Philo::SM::thinking::EAT, DONE} */
  151. case EAT_SIG: /* intentionally fall through */
  152. case DONE_SIG: {
  153. /* EAT or DONE must be for other Philos than this one */
  154. Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  155. status_ = QM_HANDLED();
  156. break;
  157. }
  158. default: {
  159. status_ = QM_SUPER();
  160. break;
  161. }
  162. }
  163. return status_;
  164. }
  165. /*${AOs::Philo::SM::hungry} ................................................*/
  166. /* ${AOs::Philo::SM::hungry} */
  167. static QState Philo_hungry_e(Philo * const me) {
  168. TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
  169. pe->philoNum = PHILO_ID(me);
  170. QACTIVE_POST(AO_Table, &pe->super, me);
  171. return QM_ENTRY(&Philo_hungry_s);
  172. }
  173. /* ${AOs::Philo::SM::hungry} */
  174. static QState Philo_hungry(Philo * const me, QEvt const * const e) {
  175. QState status_;
  176. switch (e->sig) {
  177. /* ${AOs::Philo::SM::hungry::EAT} */
  178. case EAT_SIG: {
  179. /* ${AOs::Philo::SM::hungry::EAT::[Q_EVT_CAST(TableEvt)->philoNum=~} */
  180. if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(me)) {
  181. static struct {
  182. QMState const *target;
  183. QActionHandler act[2];
  184. } const tatbl_ = { /* transition-action table */
  185. &Philo_eating_s, /* target state */
  186. {
  187. Q_ACTION_CAST(&Philo_eating_e), /* entry */
  188. Q_ACTION_CAST(0) /* zero terminator */
  189. }
  190. };
  191. status_ = QM_TRAN(&tatbl_);
  192. }
  193. else {
  194. status_ = QM_UNHANDLED();
  195. }
  196. break;
  197. }
  198. /* ${AOs::Philo::SM::hungry::DONE} */
  199. case DONE_SIG: {
  200. /* DONE must be for other Philos than this one */
  201. Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  202. status_ = QM_HANDLED();
  203. break;
  204. }
  205. default: {
  206. status_ = QM_SUPER();
  207. break;
  208. }
  209. }
  210. return status_;
  211. }
  212. /*${AOs::Philo::SM::eating} ................................................*/
  213. /* ${AOs::Philo::SM::eating} */
  214. static QState Philo_eating_e(Philo * const me) {
  215. QTimeEvt_armX(&me->timeEvt, EAT_TIME, 0U);
  216. return QM_ENTRY(&Philo_eating_s);
  217. }
  218. /* ${AOs::Philo::SM::eating} */
  219. static QState Philo_eating_x(Philo * const me) {
  220. TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
  221. pe->philoNum = PHILO_ID(me);
  222. QF_PUBLISH(&pe->super, me);
  223. return QM_EXIT(&Philo_eating_s);
  224. }
  225. /* ${AOs::Philo::SM::eating} */
  226. static QState Philo_eating(Philo * const me, QEvt const * const e) {
  227. QState status_;
  228. switch (e->sig) {
  229. /* ${AOs::Philo::SM::eating::TIMEOUT} */
  230. case TIMEOUT_SIG: {
  231. static struct {
  232. QMState const *target;
  233. QActionHandler act[3];
  234. } const tatbl_ = { /* transition-action table */
  235. &Philo_thinking_s, /* target state */
  236. {
  237. Q_ACTION_CAST(&Philo_eating_x), /* exit */
  238. Q_ACTION_CAST(&Philo_thinking_e), /* entry */
  239. Q_ACTION_CAST(0) /* zero terminator */
  240. }
  241. };
  242. status_ = QM_TRAN(&tatbl_);
  243. break;
  244. }
  245. /* ${AOs::Philo::SM::eating::EAT, DONE} */
  246. case EAT_SIG: /* intentionally fall through */
  247. case DONE_SIG: {
  248. /* EAT or DONE must be for other Philos than this one */
  249. Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  250. status_ = QM_HANDLED();
  251. break;
  252. }
  253. default: {
  254. status_ = QM_SUPER();
  255. break;
  256. }
  257. }
  258. return status_;
  259. }