Multithreading.hpp 6.1 KB

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