Coordinator.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @file kernel/pdevs/multithreading/Coordinator.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef PDEVS_MULTITHREADING_COORDINATOR
  26. #define PDEVS_MULTITHREADING_COORDINATOR 1
  27. #include <paradevs/common/utils/Multithreading.hpp>
  28. #include <paradevs/kernel/pdevs/Coordinator.hpp>
  29. #include <thread>
  30. namespace paradevs { namespace pdevs { namespace multithreading {
  31. template < class Time>
  32. struct start_message
  33. {
  34. explicit start_message(typename Time::type t) : _t(t)
  35. { }
  36. typename Time::type _t;
  37. };
  38. template < class Time>
  39. struct transition_message
  40. {
  41. explicit transition_message(typename Time::type t) : _t(t)
  42. { }
  43. typename Time::type _t;
  44. };
  45. template < class Time >
  46. struct done_start_message
  47. {
  48. explicit done_start_message(typename Time::type tn,
  49. common::Model < Time >* child) :
  50. _tn(tn), _child(child)
  51. { }
  52. typename Time::type _tn;
  53. common::Model < Time >* _child;
  54. };
  55. template < class Time >
  56. struct done_transition_message
  57. {
  58. explicit done_transition_message(typename Time::type tn,
  59. common::Model < Time >* child) :
  60. _tn(tn), _child(child)
  61. { }
  62. typename Time::type _tn;
  63. common::Model < Time >* _child;
  64. };
  65. template < class Time,
  66. class GraphManager,
  67. class Parameters = common::NoParameters,
  68. class GraphParameters = common::NoParameters >
  69. class Coordinator : public pdevs::Coordinator < Time, GraphManager,
  70. Parameters, GraphParameters >
  71. {
  72. typedef pdevs::Coordinator < Time, GraphManager,
  73. Parameters, GraphParameters > parent_type;
  74. typedef Coordinator < Time, GraphManager,
  75. Parameters, GraphParameters > type;
  76. typedef done_start_message < Time > done_start_message_type;
  77. typedef start_message < Time > start_message_type;
  78. typedef done_transition_message < Time > done_transition_message_type;
  79. typedef transition_message < Time > transition_message_type;
  80. public:
  81. Coordinator(const std::string& name,
  82. const Parameters& parameters,
  83. const GraphParameters& graph_parameters) :
  84. common::Model < Time >(name),
  85. pdevs::Coordinator < Time, GraphManager,
  86. Parameters, GraphParameters >(name, parameters,
  87. graph_parameters),
  88. _thread(std::thread([&]{ loop(); }))
  89. { type::_graph_manager.init(); }
  90. virtual ~Coordinator()
  91. {
  92. done();
  93. _thread.join();
  94. }
  95. void done()
  96. { get_sender().send(paradevs::common::Close()); }
  97. paradevs::common::Sender get_sender()
  98. { return _incoming; }
  99. void set_sender(common::Sender sender)
  100. { _sender = sender; }
  101. void loop()
  102. {
  103. try
  104. {
  105. for(;;) {
  106. _incoming.wait()
  107. .template handle < start_message_type >(
  108. [&](start_message_type const& msg)
  109. {
  110. typename Time::type tn = start(msg._t);
  111. _sender.send(done_start_message_type(tn, this));
  112. })
  113. .template handle < done_start_message_type >(
  114. [&](done_start_message_type const& msg)
  115. {
  116. type::_event_table.init(msg._tn, msg._child);
  117. --_received;
  118. if (_received == 0) {
  119. _received_mutex.unlock();
  120. }
  121. })
  122. .template handle < transition_message_type >(
  123. [&](transition_message_type const& msg)
  124. {
  125. typename Time::type tn = transition(msg._t);
  126. _sender.send(done_transition_message_type(tn,
  127. this));
  128. })
  129. .template handle < done_transition_message_type >(
  130. [&](done_transition_message_type const& msg)
  131. {
  132. type::_event_table.put(msg._tn, msg._child);
  133. --_received;
  134. if (_received == 0) {
  135. _received_mutex.unlock();
  136. }
  137. });
  138. }
  139. }
  140. catch(paradevs::common::Close const&)
  141. { }
  142. }
  143. typename Time::type start(typename Time::type t)
  144. {
  145. _received = 0;
  146. for (auto & child : parent_type::_graph_manager.children()) {
  147. if (child->is_atomic()) {
  148. type::_event_table.init(child->start(type::_tn), child);
  149. } else {
  150. ++_received;
  151. }
  152. }
  153. if (_received > 0) {
  154. _received_mutex.lock();
  155. type::_graph_manager.start(t);
  156. std::lock_guard < std::mutex > lock(_received_mutex);
  157. }
  158. type::_tl = t;
  159. type::_tn = type::_event_table.get_current_time();
  160. return type::_tn;
  161. }
  162. typename Time::type transition(typename Time::type t)
  163. {
  164. assert(t >= type::_tl and t <= type::_tn);
  165. common::Models < Time > receivers =
  166. type::_event_table.get_current_models(t);
  167. type::add_models_with_inputs(receivers);
  168. _received = 0;
  169. for (auto & model : receivers) {
  170. if (model->is_atomic()) {
  171. type::_event_table.put(model->transition(t), model);
  172. } else {
  173. ++_received;
  174. }
  175. }
  176. if (_received > 0) {
  177. _received_mutex.lock();
  178. type::_graph_manager.transition(receivers, t);
  179. std::lock_guard < std::mutex > lock(_received_mutex);
  180. }
  181. parent_type::update_event_table(t);
  182. type::_tl = t;
  183. type::_tn = type::_event_table.get_current_time();
  184. type::clear_bag();
  185. return type::_tn;
  186. }
  187. private:
  188. std::thread _thread;
  189. paradevs::common::Receiver _incoming;
  190. paradevs::common::Sender _sender;
  191. unsigned int _received;
  192. std::mutex _received_mutex;
  193. };
  194. } } } // namespace paradevs pdevs multithreading
  195. #endif