ping-pong-throughput.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 Ivan Baidakou
  3. #include "board.h"
  4. #include "rotor-light.hpp"
  5. namespace rl = rotor_light;
  6. namespace message {
  7. struct Ping : rl::Message {
  8. using Message::Message;
  9. static constexpr auto type_id = __LINE__;
  10. };
  11. struct Pong : rl::Message {
  12. using Message::Message;
  13. static constexpr auto type_id = __LINE__;
  14. };
  15. } // namespace message
  16. struct Pinger : rl::Actor<2> {
  17. using Parent = Actor<2>;
  18. void initialize() override {
  19. subscribe(&Pinger::on_pong);
  20. Parent::initialize();
  21. }
  22. void advance_start() override {
  23. Parent::advance_start();
  24. ping();
  25. }
  26. void ping() {
  27. --left;
  28. send<rl::ctx::thread, message::Ping>(0, ponger_id);
  29. }
  30. void on_pong(message::Pong &) {
  31. if (left > 0) {
  32. ping();
  33. }
  34. }
  35. uint32_t left;
  36. rl::ActorId ponger_id;
  37. };
  38. struct Ponger : rl::Actor<2> {
  39. using Parent = Actor<2>;
  40. void initialize() override {
  41. subscribe(&Ponger::on_ping);
  42. Parent::initialize();
  43. }
  44. void on_ping(message::Ping &) {
  45. send<rl::ctx::thread, message::Pong>(0, pinger_id);
  46. }
  47. rl::ActorId pinger_id;
  48. };
  49. using Supervisor =
  50. rl::Supervisor<rl::SupervisorBase::min_handlers_amount, Pinger, Ponger>;
  51. using Storage = rl::traits::MessageStorage<rl::message::ChangeState,
  52. rl::message::ChangeStateAck,
  53. message::Ping, message::Pong>;
  54. using Queue = rl::Queue<Storage, 5>; /* upto 5 messages in 1 queue */
  55. Supervisor sup;
  56. int main(int, char **) {
  57. Board::init_start();
  58. Board::enable_timer();
  59. ROTOR_LIGHT_ENABLE_INTERRUPTS();
  60. while (true) {
  61. Queue queue;
  62. rl::Context context{&queue, nullptr, nullptr};
  63. /* setup */
  64. sup.bind(context);
  65. auto pinger = sup.get_child<0>();
  66. auto ponger = sup.get_child<1>();
  67. pinger->left = Board::samples;
  68. pinger->ponger_id = ponger->get_id();
  69. ponger->pinger_id = pinger->get_id();
  70. /* disable polling timer timer */
  71. sup.start(false);
  72. Board::toggle_led();
  73. sup.process();
  74. Board::toggle_led();
  75. sup.stop();
  76. sup.process();
  77. Board::delay();
  78. }
  79. }