Skip to content

Commit

Permalink
[Core] Split frozen<T> and message_queue<T>.
Browse files Browse the repository at this point in the history
Signals don't use queues, but use frozen events. Split these two entities to
reduce include burden.
  • Loading branch information
Andrey Sibiryov committed Mar 25, 2015
1 parent 5a90c4a commit 54ea0c9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 48 deletions.
4 changes: 2 additions & 2 deletions include/cocaine/context/signal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "cocaine/locked_ptr.hpp"

#include "cocaine/rpc/dispatch.hpp"
#include "cocaine/rpc/queue.hpp"
#include "cocaine/rpc/frozen.hpp"

#include <algorithm>
#include <list>
Expand Down Expand Up @@ -94,7 +94,7 @@ struct event_visitor:

template<class Tag>
class retroactive_signal {
typedef typename io::frozen_over<Tag>::type variant_type;
typedef typename io::make_frozen_over<Tag>::type variant_type;

struct subscriber_t {
std::weak_ptr<dispatch<Tag>> slot;
Expand Down
4 changes: 3 additions & 1 deletion include/cocaine/rpc/dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ class dispatch:

typedef typename mpl::transform<
typename io::messages<Tag>::type,
typename mpl::lambda<std::shared_ptr<io::basic_slot<mpl::_1>>>::type
typename mpl::lambda<
std::shared_ptr<io::basic_slot<mpl::_1>>
>::type
>::type slot_types;

typedef std::map<
Expand Down
73 changes: 73 additions & 0 deletions include/cocaine/rpc/frozen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright (c) 2011-2015 Andrey Sibiryov <[email protected]>
Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file.
This file is part of Cocaine.
Cocaine is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Cocaine 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef COCAINE_IO_FROZEN_EVENTS_HPP
#define COCAINE_IO_FROZEN_EVENTS_HPP

#include "cocaine/rpc/protocol.hpp"
#include "cocaine/rpc/slot.hpp"

#include <boost/mpl/lambda.hpp>
#include <boost/mpl/transform.hpp>

#include <boost/variant/variant.hpp>

namespace cocaine { namespace io {

namespace mpl = boost::mpl;

// Frozen events

template<class Event>
struct frozen {
typedef Event event_type;
typedef typename basic_slot<event_type>::tuple_type tuple_type;

frozen() = default;

template<typename... Args>
frozen(event_type, Args&&... args):
tuple(std::forward<Args>(args)...)
{ }

// NOTE: If the message cannot be sent right away, then the message arguments are placed into a
// temporary storage until the upstream is attached.
tuple_type tuple;
};

template<class Event, typename... Args>
frozen<Event>
make_frozen(Args&&... args) {
return frozen<Event>(Event(), std::forward<Args>(args)...);
}

template<class Tag>
struct make_frozen_over {
typedef typename mpl::transform<
typename messages<Tag>::type,
typename mpl::lambda<frozen<mpl::_1>>
>::type frozen_types;

typedef typename boost::make_variant_over<frozen_types>::type type;
};

}} // namespace cocaine::io

#endif
47 changes: 3 additions & 44 deletions include/cocaine/rpc/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,19 @@
#ifndef COCAINE_IO_MESSAGE_QUEUE_HPP
#define COCAINE_IO_MESSAGE_QUEUE_HPP

#include "cocaine/locked_ptr.hpp"

#include "cocaine/rpc/protocol.hpp"
#include "cocaine/rpc/slot.hpp"
#include "cocaine/rpc/frozen.hpp"
#include "cocaine/rpc/tags.hpp"
#include "cocaine/rpc/upstream.hpp"

#include <boost/mpl/lambda.hpp>
#include <boost/mpl/transform.hpp>

#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant.hpp>

namespace cocaine { namespace io {

template<class Tag, class Upstream = basic_upstream_t> class message_queue;

namespace mpl = boost::mpl;

// Frozen events

template<class Event>
struct frozen {
typedef Event event_type;
typedef typename basic_slot<event_type>::tuple_type tuple_type;

frozen() = default;

template<typename... Args>
frozen(event_type, Args&&... args):
tuple(std::forward<Args>(args)...)
{ }

// NOTE: If the message cannot be sent right away, then the message arguments are placed into a
// temporary storage until the upstream is attached.
tuple_type tuple;
};

template<class Event, typename... Args>
frozen<Event>
make_frozen(Args&&... args) {
return frozen<Event>(Event(), std::forward<Args>(args)...);
}

template<class Tag>
struct frozen_over {
typedef typename mpl::transform<
typename messages<Tag>::type,
typename mpl::lambda<frozen<mpl::_1>>
>::type frozen_types;

typedef typename boost::make_variant_over<frozen_types>::type type;
};

namespace aux {

template<class Upstream>
Expand Down Expand Up @@ -105,7 +64,7 @@ class message_queue {
typedef Upstream upstream_type;

// Operation log.
std::vector<typename frozen_over<Tag>::type> m_operations;
std::vector<typename make_frozen_over<Tag>::type> m_operations;

// The upstream might be attached during message invocation, so it has to be synchronized for
// thread safety - the atomicity guarantee of the shared_ptr<T> is not enough.
Expand Down
2 changes: 1 addition & 1 deletion include/cocaine/traits/frozen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "cocaine/traits.hpp"

#include "cocaine/rpc/queue.hpp"
#include "cocaine/rpc/frozen.hpp"

namespace cocaine { namespace io {

Expand Down

0 comments on commit 54ea0c9

Please sign in to comment.