mosesdecoder/moses/ContextScope.h

105 lines
2.6 KiB
C
Raw Normal View History

// -*- c++ -*-
// A class to store "local" information (such as task-specific caches).
// The idea is for each translation task to have a scope, which stores
// shared pointers to task-specific objects such as caches and priors.
// Since these objects are referenced via shared pointers, sopes can
// share information.
#pragma once
#ifdef WITH_THREADS
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/locks.hpp>
#include <boost/foreach.hpp>
#endif
#include <map>
#include <boost/shared_ptr.hpp>
// #include "thread_safe_container.h"
namespace Moses
{
2015-05-02 13:45:24 +03:00
class ContextScope
{
2015-05-10 12:19:26 +03:00
protected:
typedef std::map<void const*, boost::shared_ptr<void> > scratchpad_t;
typedef scratchpad_t::iterator iter_t;
typedef scratchpad_t::value_type entry_t;
typedef scratchpad_t::const_iterator const_iter_t;
scratchpad_t m_scratchpad;
#ifdef WITH_THREADS
2015-05-10 12:19:26 +03:00
mutable boost::shared_mutex m_lock;
#endif
2015-05-10 12:19:26 +03:00
public:
// class write_access
// {
// boost::unique_lock<boost::shared_mutex> m_lock;
// public:
2015-05-02 13:45:24 +03:00
// write_access(boost::shared_mutex& lock)
// : m_lock(lock)
// { }
2015-05-02 13:45:24 +03:00
// write_access(write_access& other)
// {
// swap(m_lock, other.m_lock);
// }
// };
2015-05-02 13:45:24 +03:00
// write_access lock() const
// {
// return write_access(m_lock);
// }
2015-05-10 12:19:26 +03:00
template<typename T>
boost::shared_ptr<void> const&
set(void const* const key, boost::shared_ptr<T> const& val) {
#ifdef WITH_THREADS
2015-05-10 12:19:26 +03:00
boost::unique_lock<boost::shared_mutex> lock(m_lock);
#endif
2015-05-10 12:19:26 +03:00
return (m_scratchpad[key] = val);
}
2015-05-10 12:19:26 +03:00
template<typename T>
boost::shared_ptr<T> const
get(void const* key, bool CreateNewIfNecessary=false) {
#ifdef WITH_THREADS
2015-05-10 12:19:26 +03:00
using boost::shared_mutex;
using boost::upgrade_lock;
// T const* key = reinterpret_cast<T const*>(xkey);
upgrade_lock<shared_mutex> lock(m_lock);
#endif
2015-05-10 12:19:26 +03:00
iter_t m = m_scratchpad.find(key);
boost::shared_ptr< T > ret;
if (m != m_scratchpad.end()) {
if (m->second == NULL && CreateNewIfNecessary) {
#ifdef WITH_THREADS
2015-05-10 12:19:26 +03:00
boost::upgrade_to_unique_lock<shared_mutex> xlock(lock);
#endif
2015-05-10 12:19:26 +03:00
m->second.reset(new T);
}
ret = boost::static_pointer_cast< T >(m->second);
return ret;
}
2015-05-10 12:19:26 +03:00
if (!CreateNewIfNecessary) return ret;
#ifdef WITH_THREADS
boost::upgrade_to_unique_lock<shared_mutex> xlock(lock);
#endif
ret.reset(new T);
m_scratchpad[key] = ret;
return ret;
}
2015-05-02 13:45:24 +03:00
ContextScope() { }
2015-05-10 12:19:26 +03:00
ContextScope(ContextScope const& other) {
#ifdef WITH_THREADS
2015-05-02 13:45:24 +03:00
boost::unique_lock<boost::shared_mutex> lock1(this->m_lock);
boost::unique_lock<boost::shared_mutex> lock2(other.m_lock);
#endif
2015-05-02 13:45:24 +03:00
m_scratchpad = other.m_scratchpad;
}
2015-05-02 13:45:24 +03:00
};
};