Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlrich Germann <Ulrich.Germann@gmail.com>2015-03-28 17:43:21 +0300
committerUlrich Germann <Ulrich.Germann@gmail.com>2015-03-28 17:44:40 +0300
commita706569844ea38154d02c2607908842244f7a6b2 (patch)
tree1b1375b3ddd79d3ae088b42f84bf9393348821fe /moses/ContextScope.h
parent1b23edf62f4f390fc0d7c155d2b27158362f8f3d (diff)
Thread-safe classes for storing context-specific information.
Diffstat (limited to 'moses/ContextScope.h')
-rw-r--r--moses/ContextScope.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/moses/ContextScope.h b/moses/ContextScope.h
new file mode 100644
index 000000000..78940e575
--- /dev/null
+++ b/moses/ContextScope.h
@@ -0,0 +1,57 @@
+// -*- 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.
+
+#ifdef WITH_THREADS
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/thread/locks.hpp>
+#include <boost/foreach.hpp>
+#endif
+
+#include "thread_safe_container.h"
+
+namespace Moses
+{
+ class ContextScope
+ {
+ protected:
+ typedef ThreadSafeContainer<void*,boost::shared_ptr<void> >scratchpad_t;
+ scratchpad_t m_scratchpad;
+ boost::shared_mutex m_lock;
+ public:
+
+ template<typename T>
+ boost::shared_ptr<T>
+ get(T* key, bool CreateNewIfNecessary) const
+ {
+ 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;
+ ret.reset(new T);
+ x = m_scratchpad.get(key, ret);
+ return static_cast< boost::shared_pointer< T > >(*x);
+ }
+
+ 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);
+ }
+ }
+
+ };
+
+};