Multithreading.hpp 5.5 KB

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