Multithreading.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @file common/utils/Multithreading.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.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 COMMON_UTILS_MULTITHREADING
  26. #define COMMON_UTILS_MULTITHREADING 1
  27. #include <condition_variable>
  28. #include <queue>
  29. #include <mutex>
  30. namespace artis {
  31. namespace common {
  32. struct BaseMessage {
  33. virtual ~BaseMessage() { }
  34. };
  35. template<typename Msg>
  36. struct Message : BaseMessage {
  37. explicit Message(Msg const& content)
  38. :_content(content) { }
  39. Msg _content;
  40. };
  41. class Close {
  42. };
  43. class MessageQueue {
  44. public:
  45. MessageQueue() = default;
  46. template<typename T>
  47. void push(T const& msg)
  48. {
  49. std::lock_guard<std::mutex> lock(_mutex);
  50. _queue.push(std::make_shared<Message<T> >(msg));
  51. _condition.notify_all();
  52. }
  53. std::shared_ptr<BaseMessage> wait_and_pop()
  54. {
  55. std::unique_lock<std::mutex> lock(_mutex);
  56. _condition.wait(lock, [&] { return not _queue.empty(); });
  57. auto res = _queue.front();
  58. _queue.pop();
  59. return res;
  60. }
  61. private:
  62. std::mutex _mutex;
  63. std::condition_variable _condition;
  64. std::queue<std::shared_ptr<BaseMessage> > _queue;
  65. };
  66. class Sender {
  67. public:
  68. Sender()
  69. :_queue(0) { }
  70. explicit Sender(MessageQueue* queue)
  71. :_queue(queue) { }
  72. template<typename Message>
  73. void send(Message const& msg)
  74. {
  75. if (_queue) {
  76. _queue->push(msg);
  77. }
  78. }
  79. private:
  80. MessageQueue* _queue;
  81. };
  82. template<typename PreviousDispatcher, typename Msg, typename Func>
  83. class TemplateDispatcher {
  84. template<typename Dispatcher, typename OtherMsg, typename OtherFunc>
  85. friend
  86. class TemplateDispatcher;
  87. public:
  88. TemplateDispatcher(TemplateDispatcher&& other)
  89. :_queue(other._queue),
  90. _previous(other._previous),
  91. _function(
  92. std::move(
  93. other._function)),
  94. _chained(other._chained) { other._chained = true; }
  95. TemplateDispatcher(MessageQueue* queue,
  96. PreviousDispatcher* previous,
  97. Func&& function)
  98. :
  99. _queue(queue), _previous(previous),
  100. _function(std::forward<Func>(function)),
  101. _chained(false) { previous->_chained = true; }
  102. bool dispatch(std::shared_ptr<BaseMessage> const& msg)
  103. {
  104. Message<Msg>* message =
  105. dynamic_cast < Message<Msg>* >(msg.get());
  106. if (message) {
  107. _function(message->_content);
  108. return true;
  109. } else {
  110. return _previous->dispatch(msg);
  111. }
  112. }
  113. template<typename OtherMsg, typename OtherFunc>
  114. TemplateDispatcher<TemplateDispatcher<PreviousDispatcher, Msg, Func>,
  115. OtherMsg, OtherFunc>
  116. handle(OtherFunc&& of)
  117. {
  118. return TemplateDispatcher<TemplateDispatcher<PreviousDispatcher,
  119. Msg, Func>,
  120. OtherMsg, OtherFunc>(
  121. _queue, this, std::forward<OtherFunc>(of));
  122. }
  123. ~TemplateDispatcher() noexcept(false)
  124. {
  125. if (not _chained) {
  126. wait_and_dispatch();
  127. }
  128. }
  129. private:
  130. TemplateDispatcher(TemplateDispatcher const&) = delete;
  131. TemplateDispatcher& operator=(TemplateDispatcher const&) = delete;
  132. void wait_and_dispatch()
  133. {
  134. for (;;) {
  135. auto msg = _queue->wait_and_pop();
  136. if (dispatch(msg)) {
  137. break;
  138. }
  139. }
  140. }
  141. MessageQueue* _queue;
  142. PreviousDispatcher* _previous;
  143. Func _function;
  144. bool _chained;
  145. };
  146. class Dispatcher {
  147. template<typename Dispatcher, typename Msg, typename Func>
  148. friend
  149. class TemplateDispatcher;
  150. public:
  151. Dispatcher(Dispatcher&& other)
  152. :_queue(other._queue),
  153. _chained(other._chained) { other._chained = true; }
  154. explicit Dispatcher(MessageQueue* queue)
  155. :_queue(queue), _chained(false) { }
  156. template<typename Message, typename Func>
  157. TemplateDispatcher<Dispatcher, Message, Func>
  158. handle(Func&& function)
  159. {
  160. return TemplateDispatcher<Dispatcher, Message, Func>(
  161. _queue, this, std::forward<Func>(function));
  162. }
  163. ~Dispatcher() noexcept(false)
  164. {
  165. if (not _chained) {
  166. wait_and_dispatch();
  167. }
  168. }
  169. private:
  170. Dispatcher(Dispatcher const&) = delete;
  171. Dispatcher& operator=(Dispatcher const&) = delete;
  172. void wait_and_dispatch()
  173. {
  174. for (;;) {
  175. auto msg = _queue->wait_and_pop();
  176. dispatch(msg);
  177. }
  178. }
  179. bool dispatch(std::shared_ptr<BaseMessage> const& msg)
  180. {
  181. if (dynamic_cast < Message<Close>* >(msg.get())) {
  182. throw Close();
  183. }
  184. return false;
  185. }
  186. MessageQueue* _queue;
  187. bool _chained;
  188. };
  189. class Receiver {
  190. public:
  191. Receiver() : _queue(new MessageQueue) { }
  192. ~Receiver() { delete _queue; }
  193. Sender get_sender() { return Sender(_queue); }
  194. Dispatcher wait() { return Dispatcher(_queue); }
  195. private:
  196. MessageQueue* _queue;
  197. };
  198. }
  199. } // namespace artis common
  200. #endif