philo.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.4 <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_FILE
  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. QActive * const AO_Philo[N_PHILO] = { /* "opaque" pointers to Philo AO */
  60. &l_philo[0].super,
  61. &l_philo[1].super,
  62. //&l_philo[2].super,
  63. //&l_philo[3].super,
  64. //&l_philo[4].super
  65. };
  66. /* Philo definition --------------------------------------------------------*/
  67. /*$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  68. /* Check for the minimum required QP version */
  69. #if (QP_VERSION < 700U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
  70. #error qpc version 7.0.0 or higher required
  71. #endif
  72. /*$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  73. /*$define${AOs::Philo_ctor} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  74. /*${AOs::Philo_ctor} .......................................................*/
  75. void Philo_ctor(void) {
  76. uint8_t n;
  77. Philo *me;
  78. for (n = 0U; n < N_PHILO; ++n) {
  79. me = &l_philo[n];
  80. QActive_ctor(&me->super, Q_STATE_CAST(&Philo_initial));
  81. QTimeEvt_ctorX(&me->timeEvt, &me->super, TIMEOUT_SIG, 0U);
  82. }
  83. }
  84. /*$enddef${AOs::Philo_ctor} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  85. /*$define${AOs::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  86. /*${AOs::Philo} ............................................................*/
  87. /*${AOs::Philo::SM} ........................................................*/
  88. static QState Philo_initial(Philo * const me, void const * const par) {
  89. /*${AOs::Philo::SM::initial} */
  90. static uint8_t registered = (uint8_t)0; /* starts off with 0, per C-standard */
  91. (void)par; /* unused parameter */
  92. if (registered == (uint8_t)0) {
  93. registered = (uint8_t)1;
  94. QS_OBJ_DICTIONARY(&l_philo[0]);
  95. QS_OBJ_DICTIONARY(&l_philo[0].timeEvt);
  96. QS_OBJ_DICTIONARY(&l_philo[1]);
  97. QS_OBJ_DICTIONARY(&l_philo[1].timeEvt);
  98. //QS_OBJ_DICTIONARY(&l_philo[2]);
  99. //QS_OBJ_DICTIONARY(&l_philo[2].timeEvt);
  100. //QS_OBJ_DICTIONARY(&l_philo[3]);
  101. //QS_OBJ_DICTIONARY(&l_philo[3].timeEvt);
  102. //QS_OBJ_DICTIONARY(&l_philo[4]);
  103. //QS_OBJ_DICTIONARY(&l_philo[4].timeEvt);
  104. QS_FUN_DICTIONARY(&Philo_initial);
  105. QS_FUN_DICTIONARY(&Philo_thinking);
  106. QS_FUN_DICTIONARY(&Philo_hungry);
  107. QS_FUN_DICTIONARY(&Philo_eating);
  108. }
  109. QS_SIG_DICTIONARY(HUNGRY_SIG, me); /* signal for each Philos */
  110. QS_SIG_DICTIONARY(TIMEOUT_SIG, me); /* signal for each Philos */
  111. QActive_subscribe(&me->super, EAT_SIG);
  112. return Q_TRAN(&Philo_thinking);
  113. }
  114. /*${AOs::Philo::SM::thinking} ..............................................*/
  115. static QState Philo_thinking(Philo * const me, QEvt const * const e) {
  116. QState status_;
  117. switch (e->sig) {
  118. /*${AOs::Philo::SM::thinking} */
  119. case Q_ENTRY_SIG: {
  120. QTimeEvt_armX(&me->timeEvt, THINK_TIME, 0U);
  121. status_ = Q_HANDLED();
  122. break;
  123. }
  124. /*${AOs::Philo::SM::thinking::TIMEOUT} */
  125. case TIMEOUT_SIG: {
  126. status_ = Q_TRAN(&Philo_hungry);
  127. break;
  128. }
  129. /*${AOs::Philo::SM::thinking::EAT, DONE} */
  130. case EAT_SIG: /* intentionally fall through */
  131. case DONE_SIG: {
  132. /* EAT or DONE must be for other Philos than this one */
  133. Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  134. status_ = Q_HANDLED();
  135. break;
  136. }
  137. default: {
  138. status_ = Q_SUPER(&QHsm_top);
  139. break;
  140. }
  141. }
  142. return status_;
  143. }
  144. /*${AOs::Philo::SM::hungry} ................................................*/
  145. static QState Philo_hungry(Philo * const me, QEvt const * const e) {
  146. QState status_;
  147. switch (e->sig) {
  148. /*${AOs::Philo::SM::hungry} */
  149. case Q_ENTRY_SIG: {
  150. TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
  151. pe->philoNum = PHILO_ID(me);
  152. QACTIVE_POST(AO_Table, &pe->super, me);
  153. status_ = Q_HANDLED();
  154. break;
  155. }
  156. /*${AOs::Philo::SM::hungry::EAT} */
  157. case EAT_SIG: {
  158. /*${AOs::Philo::SM::hungry::EAT::[Q_EVT_CAST(TableEvt)->philoNum=~} */
  159. if (Q_EVT_CAST(TableEvt)->philoNum == PHILO_ID(me)) {
  160. status_ = Q_TRAN(&Philo_eating);
  161. }
  162. else {
  163. status_ = Q_UNHANDLED();
  164. }
  165. break;
  166. }
  167. /*${AOs::Philo::SM::hungry::DONE} */
  168. case DONE_SIG: {
  169. /* DONE must be for other Philos than this one */
  170. Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  171. status_ = Q_HANDLED();
  172. break;
  173. }
  174. default: {
  175. status_ = Q_SUPER(&QHsm_top);
  176. break;
  177. }
  178. }
  179. return status_;
  180. }
  181. /*${AOs::Philo::SM::eating} ................................................*/
  182. static QState Philo_eating(Philo * const me, QEvt const * const e) {
  183. QState status_;
  184. switch (e->sig) {
  185. /*${AOs::Philo::SM::eating} */
  186. case Q_ENTRY_SIG: {
  187. QTimeEvt_armX(&me->timeEvt, EAT_TIME, 0U);
  188. status_ = Q_HANDLED();
  189. break;
  190. }
  191. /*${AOs::Philo::SM::eating} */
  192. case Q_EXIT_SIG: {
  193. TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
  194. pe->philoNum = PHILO_ID(me);
  195. QACTIVE_PUBLISH(&pe->super, &me->super);
  196. status_ = Q_HANDLED();
  197. break;
  198. }
  199. /*${AOs::Philo::SM::eating::TIMEOUT} */
  200. case TIMEOUT_SIG: {
  201. status_ = Q_TRAN(&Philo_thinking);
  202. break;
  203. }
  204. /*${AOs::Philo::SM::eating::EAT, DONE} */
  205. case EAT_SIG: /* intentionally fall through */
  206. case DONE_SIG: {
  207. /* EAT or DONE must be for other Philos than this one */
  208. Q_ASSERT(Q_EVT_CAST(TableEvt)->philoNum != PHILO_ID(me));
  209. status_ = Q_HANDLED();
  210. break;
  211. }
  212. default: {
  213. status_ = Q_SUPER(&QHsm_top);
  214. break;
  215. }
  216. }
  217. return status_;
  218. }
  219. /*$enddef${AOs::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/