Coordinator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 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 <kernel/pdevs/Coordinator.hpp>
  28. #include <future>
  29. namespace paradevs { namespace pdevs { namespace multithreading {
  30. class Barrier
  31. {
  32. struct SubBarrier
  33. {
  34. std::condition_variable _cv;
  35. std::mutex _lck;
  36. int _runners;
  37. };
  38. public:
  39. Barrier(int count) : _max(count)
  40. {
  41. _current = &_sub_barriers[0];
  42. for (int i = 0; i < 2; ++i) {
  43. _sub_barriers[i]._runners = count;
  44. }
  45. }
  46. virtual ~Barrier()
  47. { }
  48. int wait()
  49. {
  50. SubBarrier *sub_barrier = _current;
  51. std::unique_lock < std::mutex > lck(sub_barrier->_lck);
  52. if (sub_barrier->_runners == 1) {
  53. if (_max != 1) {
  54. sub_barrier->_runners = _max;
  55. _current = (_current == &_sub_barriers[0]) ? &_sub_barriers[1] :
  56. &_sub_barriers[0];
  57. sub_barrier->_cv.notify_all();
  58. }
  59. } else {
  60. sub_barrier->_runners--;
  61. while (sub_barrier->_runners != _max)
  62. sub_barrier->_cv.wait(lck);
  63. }
  64. return 0;
  65. }
  66. private:
  67. int _max;
  68. SubBarrier _sub_barriers[2];
  69. SubBarrier* _current;
  70. };
  71. template < class Time,
  72. class Scheduler,
  73. class SchedulerHandle,
  74. class GraphManager,
  75. class Parameters = common::NoParameters,
  76. class GraphParameters = common::NoParameters >
  77. class Coordinator : public pdevs::Coordinator < Time, Scheduler,
  78. SchedulerHandle, GraphManager,
  79. Parameters, GraphParameters >
  80. {
  81. typedef pdevs::Coordinator < Time, Scheduler, SchedulerHandle, GraphManager,
  82. Parameters, GraphParameters > parent_type;
  83. typedef Coordinator < Time, Scheduler, SchedulerHandle, GraphManager,
  84. Parameters, GraphParameters > type;
  85. public:
  86. Coordinator(const std::string& name,
  87. const Parameters& parameters,
  88. const GraphParameters& graph_parameters) :
  89. pdevs::Coordinator < Time, Scheduler, SchedulerHandle, GraphManager,
  90. Parameters, GraphParameters >(name, parameters,
  91. graph_parameters)
  92. {
  93. for (auto & child : parent_type::_graph_manager.children()) {
  94. if (not child->is_atomic()) {
  95. type* coordinator = dynamic_cast < type* >(child);
  96. _threads.push_back(std::thread([&]{ coordinator->loop(); }));
  97. }
  98. }
  99. }
  100. virtual ~Coordinator()
  101. { }
  102. void loop()
  103. {
  104. bool stop = false;
  105. while (not stop) {
  106. }
  107. }
  108. typename Time::type start(typename Time::type t)
  109. {
  110. for (auto & child : parent_type::_graph_manager.children()) {
  111. if (child->is_atomic()) {
  112. type::_event_table.init(child->start(type::_tn), child);
  113. } else {
  114. }
  115. }
  116. type::_tl = t;
  117. type::_tn = type::_event_table.get_current_time();
  118. return type::_tn;
  119. }
  120. void output(typename Time::type t)
  121. {
  122. parent_type::output(t);
  123. }
  124. typename Time::type transition(typename Time::type t)
  125. {
  126. return parent_type::transition(t);
  127. }
  128. void post_event(typename Time::type t,
  129. const common::ExternalEvent < Time,
  130. SchedulerHandle >& event)
  131. {
  132. parent_type::post_event(t, event);
  133. }
  134. typename Time::type dispatch_events(
  135. common::Bag < Time, SchedulerHandle > bag, typename Time::type t)
  136. {
  137. return parent_type::dispatch_events(bag, t);
  138. }
  139. private:
  140. typedef std::vector < std::thread > Threads;
  141. Threads _threads;
  142. };
  143. } } } // namespace paradevs pdevs multithreading
  144. #endif