philo.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*$file${.::philo.c} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  2. /*
  3. * Model: dpp.qm
  4. * File: ${.::philo.c}
  5. *
  6. * This code has been generated by QM 5.2.5 <www.state-machine.com/qm>.
  7. * DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
  8. *
  9. * SPDX-License-Identifier: GPL-3.0-or-later
  10. *
  11. * This generated code is open source software: you can redistribute it under
  12. * the terms of the GNU General Public License as published by the Free
  13. * Software Foundation.
  14. *
  15. * This code is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * NOTE:
  21. * Alternatively, this generated code may be distributed under the terms
  22. * of Quantum Leaps commercial licenses, which expressly supersede the GNU
  23. * General Public License and are specifically designed for licensees
  24. * interested in retaining the proprietary status of their code.
  25. *
  26. * Contact information:
  27. * <www.state-machine.com/licensing>
  28. * <info@state-machine.com>
  29. */
  30. /*$endhead${.::philo.c} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  31. #include "qpc.h"
  32. #include "dpp.h"
  33. #include "bsp.h"
  34. Q_DEFINE_THIS_MODULE("philo")
  35. /* Active object class -----------------------------------------------------*/
  36. /*$declare${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  37. /*${AOs::Philo} ............................................................*/
  38. typedef struct {
  39. /* protected: */
  40. QActive super;
  41. /* private: */
  42. QTimeEvt timeEvt;
  43. } Philo;
  44. /* protected: */
  45. static QState Philo_initial(Philo * const me, void const * const par);
  46. static QState Philo_thinking(Philo * const me, QEvt const * const e);
  47. static QState Philo_hungry(Philo * const me, QEvt const * const e);
  48. static QState Philo_eating(Philo * const me, QEvt const * const e);
  49. /*$enddecl${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  50. /* Local objects -----------------------------------------------------------*/
  51. static Philo l_philo[N_PHILO]; /* storage for all Philos */
  52. #define THINK_TIME \
  53. (QTimeEvtCtr)((BSP_random() % BSP_TICKS_PER_SEC) + (BSP_TICKS_PER_SEC/2U))
  54. #define EAT_TIME \
  55. (QTimeEvtCtr)((BSP_random() % BSP_TICKS_PER_SEC) + BSP_TICKS_PER_SEC)
  56. /* helper macro to provide the ID of Philo "me_" */
  57. #define PHILO_ID(me_) ((uint8_t)((me_) - l_philo))
  58. /* Global objects ----------------------------------------------------------*/
  59. /*$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  60. /* Check for the minimum required QP version */
  61. #if (QP_VERSION < 700U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
  62. #error qpc version 7.0.0 or higher required
  63. #endif
  64. /*$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  65. /*$define${AOs::AO_Philo[N_PHILO]} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  66. /*${AOs::AO_Philo[N_PHILO]} ................................................*/
  67. QActive * const AO_Philo[N_PHILO] = {
  68. &l_philo[0].super,
  69. &l_philo[1].super,
  70. &l_philo[2].super,
  71. &l_philo[3].super,
  72. &l_philo[4].super
  73. };
  74. /*$enddef${AOs::AO_Philo[N_PHILO]} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  75. /* Philo definition --------------------------------------------------------*/
  76. /*$define${AOs::Philo_ctor} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  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. QActive_ctor(&me->super, Q_STATE_CAST(&Philo_initial));
  84. QTimeEvt_ctorX(&me->timeEvt, &me->super, TIMEOUT_SIG, 0U);
  85. }
  86. }
  87. /*$enddef${AOs::Philo_ctor} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  88. /*$define${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  89. /*${AOs::Philo} ............................................................*/
  90. /*${AOs::Philo::SM} ........................................................*/
  91. static QState Philo_initial(Philo * const me, void const * const par) {
  92. /*${AOs::Philo::SM::initial} */
  93. static uint8_t registered = (uint8_t)0; /* starts off with 0, per C-standard */
  94. (void)par; /* unused parameter */
  95. if (registered == (uint8_t)0) {
  96. registered = (uint8_t)1;
  97. QS_OBJ_DICTIONARY(&l_philo[0].timeEvt);
  98. QS_OBJ_DICTIONARY(&l_philo[1].timeEvt);
  99. QS_OBJ_DICTIONARY(&l_philo[2].timeEvt);
  100. QS_OBJ_DICTIONARY(&l_philo[3].timeEvt);
  101. QS_OBJ_DICTIONARY(&l_philo[4].timeEvt);
  102. QS_FUN_DICTIONARY(&Philo_initial);
  103. QS_FUN_DICTIONARY(&Philo_thinking);
  104. QS_FUN_DICTIONARY(&Philo_hungry);
  105. QS_FUN_DICTIONARY(&Philo_eating);
  106. }
  107. QS_SIG_DICTIONARY(HUNGRY_SIG, me); /* signal for each Philos */
  108. QS_SIG_DICTIONARY(TIMEOUT_SIG, me); /* signal for each Philos */
  109. QActive_subscribe(&me->super, EAT_SIG);
  110. QActive_subscribe(&me->super, TEST_SIG);
  111. return Q_TRAN(&Philo_thinking);
  112. }
  113. /*${AOs::Philo::SM::thinking} ..............................................*/
  114. static QState Philo_thinking(Philo * const me, QEvt const * const e) {
  115. QState status_;
  116. switch (e->sig) {
  117. /*${AOs::Philo::SM::thinking} */
  118. case Q_ENTRY_SIG: {
  119. QTimeEvt_armX(&me->timeEvt, THINK_TIME, 0U);
  120. status_ = Q_HANDLED();
  121. break;
  122. }
  123. /*${AOs::Philo::SM::thinking} */
  124. case Q_EXIT_SIG: {
  125. QTimeEvt_disarm(&me->timeEvt);
  126. status_ = Q_HANDLED();
  127. break;
  128. }
  129. /*${AOs::Philo::SM::thinking::TIMEOUT} */
  130. case TIMEOUT_SIG: {
  131. status_ = Q_TRAN(&Philo_hungry);
  132. break;
  133. }
  134. /*${AOs::Philo::SM::thinking::EAT, DONE} */
  135. case EAT_SIG: /* intentionally fall through */
  136. case DONE_SIG: {
  137. /* EAT or DONE must be for other Philos than this one */
  138. Q_ASSERT_ID(10, Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  139. status_ = Q_HANDLED();
  140. break;
  141. }
  142. default: {
  143. status_ = Q_SUPER(&QHsm_top);
  144. break;
  145. }
  146. }
  147. return status_;
  148. }
  149. /*${AOs::Philo::SM::hungry} ................................................*/
  150. static QState Philo_hungry(Philo * const me, QEvt const * const e) {
  151. QState status_;
  152. switch (e->sig) {
  153. /*${AOs::Philo::SM::hungry} */
  154. case Q_ENTRY_SIG: {
  155. TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
  156. pe->philoNum = PHILO_ID(me);
  157. QACTIVE_POST(AO_Table, &pe->super, me);
  158. status_ = Q_HANDLED();
  159. break;
  160. }
  161. /*${AOs::Philo::SM::hungry::EAT} */
  162. case EAT_SIG: {
  163. /*${AOs::Philo::SM::hungry::EAT::[Q_EVT_CAST(TableEvt)->philoNum=~} */
  164. if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(me)) {
  165. status_ = Q_TRAN(&Philo_eating);
  166. }
  167. else {
  168. status_ = Q_UNHANDLED();
  169. }
  170. break;
  171. }
  172. /*${AOs::Philo::SM::hungry::DONE} */
  173. case DONE_SIG: {
  174. /* DONE must be for other Philos than this one */
  175. Q_ASSERT_ID(20, Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  176. status_ = Q_HANDLED();
  177. break;
  178. }
  179. default: {
  180. status_ = Q_SUPER(&QHsm_top);
  181. break;
  182. }
  183. }
  184. return status_;
  185. }
  186. /*${AOs::Philo::SM::eating} ................................................*/
  187. static QState Philo_eating(Philo * const me, QEvt const * const e) {
  188. QState status_;
  189. switch (e->sig) {
  190. /*${AOs::Philo::SM::eating} */
  191. case Q_ENTRY_SIG: {
  192. QTimeEvt_armX(&me->timeEvt, EAT_TIME, 0U);
  193. status_ = Q_HANDLED();
  194. break;
  195. }
  196. /*${AOs::Philo::SM::eating} */
  197. case Q_EXIT_SIG: {
  198. TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
  199. pe->philoNum = PHILO_ID(me);
  200. QACTIVE_PUBLISH(&pe->super, &me->super);
  201. QTimeEvt_disarm(&me->timeEvt);
  202. status_ = Q_HANDLED();
  203. break;
  204. }
  205. /*${AOs::Philo::SM::eating::TIMEOUT} */
  206. case TIMEOUT_SIG: {
  207. status_ = Q_TRAN(&Philo_thinking);
  208. break;
  209. }
  210. /*${AOs::Philo::SM::eating::EAT, DONE} */
  211. case EAT_SIG: /* intentionally fall through */
  212. case DONE_SIG: {
  213. /* EAT or DONE must be for other Philos than this one */
  214. Q_ASSERT_ID(30, Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  215. status_ = Q_HANDLED();
  216. break;
  217. }
  218. default: {
  219. status_ = Q_SUPER(&QHsm_top);
  220. break;
  221. }
  222. }
  223. return status_;
  224. }
  225. /*$enddef${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/