Permalink
Browse files

changed actor_proxy_cache to provide get_or_put member funciton as we…

…ll as get
  • Loading branch information...
1 parent 608676f commit 4368dde2b63880e60817d83ef7c137c02c0fd910 @Neverlord Neverlord committed Aug 22, 2012
Showing with 36 additions and 27 deletions.
  1. +8 −2 cppa/detail/actor_proxy_cache.hpp
  2. +21 −19 src/actor_proxy_cache.cpp
  3. +3 −3 src/unicast_network.cpp
  4. +4 −3 src/uniform_type_info.cpp
@@ -49,7 +49,13 @@ class actor_proxy_cache {
public:
- actor_proxy_ptr get(actor_id aid, std::uint32_t process_id,
+ // returns existing instance if available or crates a new one
+ actor_proxy_ptr get_or_put(actor_id aid,
+ std::uint32_t process_id,
+ const process_information::node_id_type& node_id);
+
+ actor_proxy_ptr get(actor_id aid,
+ std::uint32_t process_id,
const process_information::node_id_type& node_id);
// @returns true if pptr was successfully removed, false otherwise
@@ -89,7 +95,7 @@ class actor_proxy_cache {
util::shared_spinlock m_lock;
std::map<key_tuple, actor_proxy_ptr, key_tuple_less> m_entries;
- actor_proxy_ptr get_impl(const key_tuple& key);
+ actor_proxy_ptr get_impl(const key_tuple& key, bool do_put);
};
@@ -37,49 +37,46 @@
#include "cppa/util/shared_lock_guard.hpp"
#include "cppa/util/upgrade_lock_guard.hpp"
+#include "cppa/detail/middleman.hpp"
#include "cppa/detail/network_manager.hpp"
#include "cppa/detail/actor_proxy_cache.hpp"
#include "cppa/detail/singleton_manager.hpp"
// thread_specific_ptr
//#include <boost/thread/tss.hpp>
-namespace {
-
-//boost::thread_specific_ptr<cppa::detail::actor_proxy_cache> s_proxy_cache;
-
-cppa::detail::actor_proxy_cache s_proxy_cache;
+namespace cppa { namespace detail {
-} // namespace <anonmyous>
+namespace { actor_proxy_cache s_proxy_cache; }
-namespace cppa { namespace detail {
+actor_proxy_cache& get_actor_proxy_cache() { return s_proxy_cache; }
-actor_proxy_cache& get_actor_proxy_cache() {
- /*
- if (s_proxy_cache.get() == nullptr) {
- s_proxy_cache.reset(new actor_proxy_cache);
- }
- return *s_proxy_cache;
- */
- return s_proxy_cache;
+actor_proxy_ptr actor_proxy_cache::get_or_put(actor_id aid,
+ std::uint32_t process_id,
+ const process_information::node_id_type& node_id) {
+ key_tuple k{node_id, process_id, aid};
+ return get_impl(k, true);
}
actor_proxy_ptr actor_proxy_cache::get(actor_id aid,
std::uint32_t process_id,
const process_information::node_id_type& node_id) {
key_tuple k{node_id, process_id, aid};
- return get_impl(k);
+ return get_impl(k, false);
}
-actor_proxy_ptr actor_proxy_cache::get_impl(const key_tuple& key) {
+actor_proxy_ptr actor_proxy_cache::get_impl(const key_tuple& key, bool do_put) {
{ // lifetime scope of shared guard
util::shared_lock_guard<util::shared_spinlock> guard{m_lock};
auto i = m_entries.find(key);
if (i != m_entries.end()) {
return i->second;
}
}
- actor_proxy_ptr result{new actor_proxy(std::get<2>(key), new process_information(std::get<1>(key), std::get<0>(key)))};
+ if (!do_put) { return nullptr; }
+ process_information_ptr pip(new process_information(std::get<1>(key),
+ std::get<0>(key)));
+ actor_proxy_ptr result(new actor_proxy(std::get<2>(key), pip));
{ // lifetime scope of exclusive guard
std::lock_guard<util::shared_spinlock> guard{m_lock};
auto i = m_entries.find(key);
@@ -91,7 +88,12 @@ actor_proxy_ptr actor_proxy_cache::get_impl(const key_tuple& key) {
result->attach_functor([result](std::uint32_t) {
get_actor_proxy_cache().erase(result);
});
- result->enqueue(nullptr, make_any_tuple(atom("MONITOR")));
+ middleman_enqueue(pip,
+ nullptr,
+ nullptr,
+ make_any_tuple(atom("MONITOR"),
+ pip,
+ std::get<2>(key)));
return result;
}
@@ -84,9 +84,9 @@ actor_ptr remote_actor(util::io_stream_ptr_pair peer) {
process_information_ptr pinfptr(new process_information(peer_pid, peer_node_id));
//auto key = std::make_tuple(remote_actor_id, pinfptr->process_id(), pinfptr->node_id());
detail::middleman_add_peer(peer, pinfptr);
- return detail::get_actor_proxy_cache().get(remote_actor_id,
- pinfptr->process_id(),
- pinfptr->node_id());
+ return detail::get_actor_proxy_cache().get_or_put(remote_actor_id,
+ pinfptr->process_id(),
+ pinfptr->node_id());
}
void publish(actor_ptr whom, std::uint16_t port) {
@@ -232,9 +232,10 @@ class actor_ptr_tinfo : public util::abstract_uniform_type_info<actor_ptr> {
*/
process_information::node_id_type nid;
node_id_from_string(nstr, nid);
- ptrref = detail::get_actor_proxy_cache().get(get<std::uint32_t>(ptup[0]),
- get<std::uint32_t>(ptup[1]),
- nid);
+ auto& cache = detail::get_actor_proxy_cache();
+ ptrref = cache.get_or_put(get<std::uint32_t>(ptup[0]),
+ get<std::uint32_t>(ptup[1]),
+ nid);
}
}
}

0 comments on commit 4368dde

Please sign in to comment.