Coordinator.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, class SchedulerHandle >
  46. struct done_start_message
  47. {
  48. explicit done_start_message(typename Time::type tn,
  49. common::Model < Time,
  50. SchedulerHandle >* child) :
  51. _tn(tn), _child(child)
  52. { }
  53. typename Time::type _tn;
  54. common::Model < Time, SchedulerHandle >* _child;
  55. };
  56. template < class Time, class SchedulerHandle >
  57. struct done_transition_message
  58. {
  59. explicit done_transition_message(typename Time::type tn,
  60. common::Model < Time,
  61. SchedulerHandle >* child) :
  62. _tn(tn), _child(child)
  63. { }
  64. typename Time::type _tn;
  65. common::Model < Time, SchedulerHandle >* _child;
  66. };
  67. template < class Time,
  68. class Scheduler,
  69. class SchedulerHandle,
  70. class GraphManager,
  71. class Parameters = common::NoParameters,
  72. class GraphParameters = common::NoParameters >
  73. class Coordinator : public pdevs::Coordinator < Time, Scheduler,
  74. SchedulerHandle, GraphManager,
  75. Parameters, GraphParameters >
  76. {
  77. typedef pdevs::Coordinator < Time, Scheduler, SchedulerHandle, GraphManager,
  78. Parameters, GraphParameters > parent_type;
  79. typedef Coordinator < Time, Scheduler, SchedulerHandle, GraphManager,
  80. Parameters, GraphParameters > type;
  81. typedef done_start_message < Time,
  82. SchedulerHandle > done_start_message_type;
  83. typedef start_message < Time > start_message_type;
  84. typedef done_transition_message < Time,
  85. SchedulerHandle > done_transition_message_type;
  86. typedef transition_message < Time > transition_message_type;
  87. public:
  88. Coordinator(const std::string& name,
  89. const Parameters& parameters,
  90. const GraphParameters& graph_parameters) :
  91. common::Model < Time, SchedulerHandle >(name),
  92. pdevs::Coordinator < Time, Scheduler, SchedulerHandle, GraphManager,
  93. Parameters, GraphParameters >(name, parameters,
  94. graph_parameters),
  95. _thread(std::thread([&]{ loop(); }))
  96. { type::_graph_manager.init(); }
  97. virtual ~Coordinator()
  98. {
  99. done();
  100. _thread.join();
  101. }
  102. void done()
  103. { get_sender().send(paradevs::common::Close()); }
  104. paradevs::common::Sender get_sender()
  105. { return _incoming; }
  106. void set_sender(common::Sender sender)
  107. { _sender = sender; }
  108. void loop()
  109. {
  110. try
  111. {
  112. for(;;) {
  113. _incoming.wait()
  114. .template handle < start_message_type >(
  115. [&](start_message_type const& msg)
  116. {
  117. typename Time::type tn = start(msg._t);
  118. _sender.send(done_start_message_type(tn, this));
  119. })
  120. .template handle < done_start_message_type >(
  121. [&](done_start_message_type const& msg)
  122. {
  123. type::_event_table.init(msg._tn, msg._child);
  124. --_received;
  125. if (_received == 0) {
  126. _received_mutex.unlock();
  127. }
  128. })
  129. .template handle < transition_message_type >(
  130. [&](transition_message_type const& msg)
  131. {
  132. typename Time::type tn = transition(msg._t);
  133. _sender.send(done_transition_message_type(tn,
  134. this));
  135. })
  136. .template handle < done_transition_message_type >(
  137. [&](done_transition_message_type const& msg)
  138. {
  139. type::_event_table.put(msg._tn, msg._child);
  140. --_received;
  141. if (_received == 0) {
  142. _received_mutex.unlock();
  143. }
  144. });
  145. }
  146. }
  147. catch(paradevs::common::Close const&)
  148. { }
  149. }
  150. typename Time::type start(typename Time::type t)
  151. {
  152. _received = 0;
  153. for (auto & child : parent_type::_graph_manager.children()) {
  154. if (child->is_atomic()) {
  155. type::_event_table.init(child->start(type::_tn), child);
  156. } else {
  157. ++_received;
  158. }
  159. }
  160. if (_received > 0) {
  161. type::_graph_manager.start(t);
  162. _received_mutex.lock();
  163. std::lock_guard < std::mutex > lock(_received_mutex);
  164. }
  165. type::_tl = t;
  166. type::_tn = type::_event_table.get_current_time();
  167. return type::_tn;
  168. }
  169. typename Time::type transition(typename Time::type t)
  170. {
  171. assert(t >= type::_tl and t <= type::_tn);
  172. common::Models < Time, SchedulerHandle > receivers =
  173. type::_event_table.get_current_models(t);
  174. type::add_models_with_inputs(receivers);
  175. _received = 0;
  176. for (auto & model : receivers) {
  177. if (model->is_atomic()) {
  178. type::_event_table.put(model->transition(t), model);
  179. } else {
  180. ++_received;
  181. }
  182. }
  183. if (_received > 0) {
  184. type::_graph_manager.transition(receivers, t);
  185. _received_mutex.lock();
  186. std::lock_guard < std::mutex > lock(_received_mutex);
  187. }
  188. parent_type::update_event_table(t);
  189. type::_tl = t;
  190. type::_tn = type::_event_table.get_current_time();
  191. type::clear_bag();
  192. return type::_tn;
  193. }
  194. private:
  195. std::thread _thread;
  196. paradevs::common::Receiver _incoming;
  197. paradevs::common::Sender _sender;
  198. unsigned int _received;
  199. std::mutex _received_mutex;
  200. };
  201. } } } // namespace paradevs pdevs multithreading
  202. #endif