diff options
author | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-03-30 03:20:17 +0300 |
---|---|---|
committer | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-03-30 03:20:17 +0300 |
commit | fcbfc5a535522328cd9c7abfc7f0680d93c3c9f4 (patch) | |
tree | 3c6c00c783fb098e1448c6c8ed4740c77431efae /moses/ContextScope.h | |
parent | 79cd40d2c487179565bdd05947cb11c996c3ce14 (diff) |
Feature functions and the constructors of TranslationOptionCollections
now have access to the current translation task.
This was done to allow context-sensitive processing (if provided by the FF).
Diffstat (limited to 'moses/ContextScope.h')
-rw-r--r-- | moses/ContextScope.h | 84 |
1 files changed, 62 insertions, 22 deletions
diff --git a/moses/ContextScope.h b/moses/ContextScope.h index 78940e575..8f8c398a0 100644 --- a/moses/ContextScope.h +++ b/moses/ContextScope.h @@ -4,6 +4,7 @@ // 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> @@ -11,45 +12,84 @@ #include <boost/foreach.hpp> #endif -#include "thread_safe_container.h" +#include <map> +#include <boost/shared_ptr.hpp> +// #include "thread_safe_container.h" namespace Moses { class ContextScope { protected: - typedef ThreadSafeContainer<void*,boost::shared_ptr<void> >scratchpad_t; + 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; - boost::shared_mutex m_lock; + mutable boost::shared_mutex m_lock; public: + // class write_access + // { + // boost::unique_lock<boost::shared_mutex> m_lock; + // public: + + // write_access(boost::shared_mutex& lock) + // : m_lock(lock) + // { } + + // write_access(write_access& other) + // { + // swap(m_lock, other.m_lock); + // } + // }; + + // write_access lock() const + // { + // return write_access(m_lock); + // } + + template<typename T> + boost::shared_ptr<void> const& + set(void const* const key, boost::shared_ptr<T> const& val) + { + boost::unique_lock<boost::shared_mutex> lock(m_lock); + return (m_scratchpad[key] = val); + } template<typename T> - boost::shared_ptr<T> - get(T* key, bool CreateNewIfNecessary) const + boost::shared_ptr<T> const + get(void const* key, bool CreateNewIfNecessary=false) { using boost::shared_mutex; - boost::shared_pointer<void>* x; - { - boost::shared_lock<shared_mutex> lock(m_lock); - x = m_scratchpad.get(key); - if (x) return static_cast< boost::shared_pointer< T > >(*x); - } - boost::unique_lock<shared_mutex> lock(m_lock); - boost::shared_ptr<T> ret; - if (!CrateNewIfNecessary) return ret; + using boost::upgrade_lock; + // T const* key = reinterpret_cast<T const*>(xkey); + upgrade_lock<shared_mutex> lock(m_lock); + iter_t m = m_scratchpad.find(key); + boost::shared_ptr< T > ret; + if (m != m_scratchpad.end()) + { + if (m->second == NULL && CreateNewIfNecessary) + { + boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); + m->second.reset(new T); + } + ret = boost::static_pointer_cast< T >(m->second); + return ret; + } + if (!CreateNewIfNecessary) return ret; + boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); ret.reset(new T); - x = m_scratchpad.get(key, ret); - return static_cast< boost::shared_pointer< T > >(*x); + m_scratchpad[key] = ret; + return ret; } + ContextScope() { } + ContextScope(ContextScope const& other) { - boost::unique_lock<boost::shared_mutex> lock2(this->m_lock); - scratchpad_t::locking iterator m; - for (m = other.begin(); m != other.end(); ++m) - { - m_scratchpad.set(m->first, m->second); - } + boost::unique_lock<boost::shared_mutex> lock1(this->m_lock); + boost::unique_lock<boost::shared_mutex> lock2(other.m_lock); + m_scratchpad = other.m_scratchpad; } }; |