philo.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*$file${.::philo.c} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  2. /*
  3. * Model: dpp-comp.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. /*$declare${Comp::Philo} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  36. /*${Comp::Philo} ...........................................................*/
  37. typedef struct Philo {
  38. /* protected: */
  39. QHsm super;
  40. /* public: */
  41. /* private: */
  42. CompTimeEvt timeEvt;
  43. } Philo;
  44. extern Philo Philo_inst[N_PHILO];
  45. /* protected: */
  46. static QState Philo_initial(Philo * const me, void const * const par);
  47. static QState Philo_thinking(Philo * const me, QEvt const * const e);
  48. static QState Philo_hungry(Philo * const me, QEvt const * const e);
  49. static QState Philo_eating(Philo * const me, QEvt const * const e);
  50. /*$enddecl${Comp::Philo} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  51. /* helper macros to provide a randomized think time for 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. /* definition of the whole "Comp" package */
  57. /*$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  58. /* Check for the minimum required QP version */
  59. #if (QP_VERSION < 700U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
  60. #error qpc version 7.0.0 or higher required
  61. #endif
  62. /*$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  63. /*$define${Comp} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  64. /*${Comp::CompTimeEvt} .....................................................*/
  65. /*${Comp::CompTimeEvt::ctor} ...............................................*/
  66. void CompTimeEvt_ctor(CompTimeEvt * const me,
  67. QActive * act,
  68. uint16_t num,
  69. enum_t const sig,
  70. uint_fast8_t const tickRate)
  71. {
  72. QTimeEvt_ctorX(&me->super, act, sig, tickRate);
  73. me->compNum = num;
  74. }
  75. /*${Comp::Philo} ...........................................................*/
  76. Philo Philo_inst[N_PHILO];
  77. /*${Comp::Philo::SM} .......................................................*/
  78. static QState Philo_initial(Philo * const me, void const * const par) {
  79. /*${Comp::Philo::SM::initial} */
  80. (void)par; /* unused parameter */
  81. #ifdef Q_SPY
  82. uint8_t n = (uint8_t)(me - &Philo_inst[0]);
  83. QS_OBJ_ARR_DICTIONARY(&Philo_inst[n], n);
  84. QS_OBJ_ARR_DICTIONARY(&Philo_inst[n].timeEvt, n);
  85. #endif
  86. /* the following code outputs the "fun-dictionaries" only once for
  87. * all Philo instances, as opposed to repeat them for every instance.
  88. */
  89. static bool registered = false;
  90. if (!registered) {
  91. registered = true;
  92. QS_FUN_DICTIONARY(&Philo_initial);
  93. QS_FUN_DICTIONARY(&Philo_thinking);
  94. QS_FUN_DICTIONARY(&Philo_hungry);
  95. QS_FUN_DICTIONARY(&Philo_eating);
  96. }
  97. return Q_TRAN(&Philo_thinking);
  98. }
  99. /*${Comp::Philo::SM::thinking} .............................................*/
  100. static QState Philo_thinking(Philo * const me, QEvt const * const e) {
  101. QState status_;
  102. switch (e->sig) {
  103. /*${Comp::Philo::SM::thinking} */
  104. case Q_ENTRY_SIG: {
  105. QTimeEvt_armX(&me->timeEvt.super, THINK_TIME, 0U);
  106. status_ = Q_HANDLED();
  107. break;
  108. }
  109. /*${Comp::Philo::SM::thinking} */
  110. case Q_EXIT_SIG: {
  111. QTimeEvt_disarm(&me->timeEvt.super);
  112. status_ = Q_HANDLED();
  113. break;
  114. }
  115. /*${Comp::Philo::SM::thinking::TIMEOUT} */
  116. case TIMEOUT_SIG: {
  117. status_ = Q_TRAN(&Philo_hungry);
  118. break;
  119. }
  120. /*${Comp::Philo::SM::thinking::TEST} */
  121. case TEST_SIG: {
  122. status_ = Q_HANDLED();
  123. break;
  124. }
  125. default: {
  126. status_ = Q_SUPER(&QHsm_top);
  127. break;
  128. }
  129. }
  130. return status_;
  131. }
  132. /*${Comp::Philo::SM::hungry} ...............................................*/
  133. static QState Philo_hungry(Philo * const me, QEvt const * const e) {
  134. QState status_;
  135. switch (e->sig) {
  136. /*${Comp::Philo::SM::hungry} */
  137. case Q_ENTRY_SIG: {
  138. /* asynchronously post event to the Container */
  139. TableEvt *pe = Q_NEW(TableEvt, HUNGRY_SIG);
  140. pe->philoNum = (uint8_t)(me - &Philo_inst[0]);
  141. QACTIVE_POST_LIFO(AO_Table, &pe->super);
  142. status_ = Q_HANDLED();
  143. break;
  144. }
  145. /*${Comp::Philo::SM::hungry::EAT} */
  146. case EAT_SIG: {
  147. status_ = Q_TRAN(&Philo_eating);
  148. break;
  149. }
  150. default: {
  151. status_ = Q_SUPER(&QHsm_top);
  152. break;
  153. }
  154. }
  155. return status_;
  156. }
  157. /*${Comp::Philo::SM::eating} ...............................................*/
  158. static QState Philo_eating(Philo * const me, QEvt const * const e) {
  159. QState status_;
  160. switch (e->sig) {
  161. /*${Comp::Philo::SM::eating} */
  162. case Q_ENTRY_SIG: {
  163. QTimeEvt_armX(&me->timeEvt.super, EAT_TIME, 0U);
  164. status_ = Q_HANDLED();
  165. break;
  166. }
  167. /*${Comp::Philo::SM::eating} */
  168. case Q_EXIT_SIG: {
  169. QTimeEvt_disarm(&me->timeEvt.super);
  170. /* asynchronously post event to the Container */
  171. TableEvt *pe = Q_NEW(TableEvt, DONE_SIG);
  172. pe->philoNum = (uint8_t)(me - &Philo_inst[0]);
  173. QACTIVE_POST_LIFO(AO_Table, &pe->super);
  174. status_ = Q_HANDLED();
  175. break;
  176. }
  177. /*${Comp::Philo::SM::eating::TIMEOUT} */
  178. case TIMEOUT_SIG: {
  179. status_ = Q_TRAN(&Philo_thinking);
  180. break;
  181. }
  182. default: {
  183. status_ = Q_SUPER(&QHsm_top);
  184. break;
  185. }
  186. }
  187. return status_;
  188. }
  189. /*${Comp::SM_Philo[N_PHILO]} ...............................................*/
  190. QHsm * const SM_Philo[N_PHILO] = { /* opaque pointers to Philo instances */
  191. &Philo_inst[0].super,
  192. &Philo_inst[1].super,
  193. &Philo_inst[2].super,
  194. &Philo_inst[3].super,
  195. &Philo_inst[4].super
  196. };
  197. /*${Comp::Philo_ctor} ......................................................*/
  198. void Philo_ctor(uint8_t n) {
  199. Philo *me = &Philo_inst[n];
  200. QHsm_ctor(&me->super, Q_STATE_CAST(&Philo_initial));
  201. CompTimeEvt_ctor(&me->timeEvt, AO_Table, n, TIMEOUT_SIG, 0U);
  202. }
  203. /*$enddef${Comp} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/