123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /**
- * @file common/utils/Multithreading.hpp
- * @author The PARADEVS Development Team
- * See the AUTHORS or Authors.txt file
- */
- /*
- * PARADEVS - the multimodeling and simulation environment
- * This file is a part of the PARADEVS environment
- *
- * Copyright (C) 2013 ULCO http://www.univ-litoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef COMMON_UTILS_MULTITHREADING
- #define COMMON_UTILS_MULTITHREADING 1
- #include <condition_variable>
- #include <queue>
- #include <mutex>
- namespace paradevs { namespace common {
- struct BaseMessage
- {
- virtual ~BaseMessage()
- { }
- };
- template < typename Msg >
- struct Message : BaseMessage
- {
- explicit Message(Msg const& content) : _content(content)
- { }
- Msg _content;
- };
- class Close
- { };
- class MessageQueue
- {
- public:
- template < typename T >
- void push(T const& msg)
- {
- std::lock_guard < std::mutex > lock(_mutex);
- _queue.push(std::make_shared < Message < T > >(msg));
- _condition.notify_all();
- }
- std::shared_ptr < BaseMessage > wait_and_pop()
- {
- std::unique_lock < std::mutex > lock(_mutex);
- _condition.wait(lock, [&]{ return not _queue.empty(); });
- auto res = _queue.front();
- _queue.pop();
- return res;
- }
- private:
- std::mutex _mutex;
- std::condition_variable _condition;
- std::queue < std::shared_ptr < BaseMessage > > _queue;
- };
- class Sender
- {
- public:
- Sender() : _queue(0)
- { }
- explicit Sender(MessageQueue* queue) : _queue(queue)
- { }
- template < typename Message >
- void send(Message const& msg)
- {
- if (_queue) {
- _queue->push(msg);
- }
- }
- private:
- MessageQueue* _queue;
- };
- template < typename PreviousDispatcher, typename Msg, typename Func >
- class TemplateDispatcher
- {
- template < typename Dispatcher, typename OtherMsg, typename OtherFunc >
- friend class TemplateDispatcher;
- public:
- TemplateDispatcher(TemplateDispatcher&& other) : _queue(other._queue),
- _previous(other._previous),
- _function(
- std::move(
- other._function)),
- _chained(other._chained)
- { other._chained = true; }
- TemplateDispatcher(MessageQueue* queue,
- PreviousDispatcher* previous,
- Func&& function) :
- _queue(queue), _previous(previous),
- _function(std::forward < Func >(function)), _chained(false)
- { previous->_chained = true; }
- bool dispatch(std::shared_ptr < BaseMessage > const& msg)
- {
- Message < Msg >* message =
- dynamic_cast < Message < Msg >* >(msg.get());
- if (message) {
- _function(message->_content);
- return true;
- } else {
- return _previous->dispatch(msg);
- }
- }
- template < typename OtherMsg, typename OtherFunc >
- TemplateDispatcher < TemplateDispatcher < PreviousDispatcher, Msg, Func >,
- OtherMsg, OtherFunc >
- handle(OtherFunc&& of)
- {
- return TemplateDispatcher < TemplateDispatcher < PreviousDispatcher,
- Msg, Func >,
- OtherMsg, OtherFunc >(
- _queue, this, std::forward < OtherFunc >(of));
- }
- ~TemplateDispatcher() noexcept(false)
- {
- if (not _chained) {
- wait_and_dispatch();
- }
- }
- private:
- TemplateDispatcher(TemplateDispatcher const&)=delete;
- TemplateDispatcher& operator=(TemplateDispatcher const&)=delete;
- void wait_and_dispatch()
- {
- for(;;) {
- auto msg = _queue->wait_and_pop();
- if (dispatch(msg)) {
- break;
- }
- }
- }
- MessageQueue* _queue;
- PreviousDispatcher* _previous;
- Func _function;
- bool _chained;
- };
- class Dispatcher
- {
- template < typename Dispatcher, typename Msg, typename Func>
- friend class TemplateDispatcher;
- public:
- Dispatcher(Dispatcher&& other) : _queue(other._queue),
- _chained(other._chained)
- { other._chained = true; }
- explicit Dispatcher(MessageQueue* queue) : _queue(queue), _chained(false)
- { }
- template < typename Message, typename Func >
- TemplateDispatcher < Dispatcher, Message, Func >
- handle(Func&& function)
- {
- return TemplateDispatcher < Dispatcher, Message, Func >(
- _queue, this, std::forward < Func >(function));
- }
- ~Dispatcher() noexcept(false)
- {
- if (not _chained) {
- wait_and_dispatch();
- }
- }
- private:
- Dispatcher(Dispatcher const&)=delete;
- Dispatcher& operator=(Dispatcher const&)=delete;
- void wait_and_dispatch()
- {
- for(;;) {
- auto msg = _queue->wait_and_pop();
- dispatch(msg);
- }
- }
- bool dispatch(std::shared_ptr < BaseMessage > const& msg)
- {
- if (dynamic_cast < Message < Close >* >(msg.get())) {
- throw Close();
- }
- return false;
- }
- MessageQueue* _queue;
- bool _chained;
- };
- class Receiver
- {
- public:
- Receiver()
- { }
- operator Sender()
- { return Sender(&_queue); }
- Dispatcher wait()
- { return Dispatcher(&_queue); }
- private:
- MessageQueue _queue;
- };
- } } // namespace paradevs common
- #endif
|