Multithreading.hpp 7.0 KB

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