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:
authorHieu Hoang <hieuhoang@gmail.com>2015-05-02 13:45:24 +0300
committerHieu Hoang <hieuhoang@gmail.com>2015-05-02 13:45:24 +0300
commitcc8c6b7b10abd8118014635609f7658f6a7a1857 (patch)
treecab374bcbf73ff97782e9131380e4f32dbeb1e67 /moses/thread_safe_container.h
parenta4a7c14593766ab188e1d6ae1c29e67ed201d412 (diff)
beautify
Diffstat (limited to 'moses/thread_safe_container.h')
-rw-r--r--moses/thread_safe_container.h176
1 files changed, 86 insertions, 90 deletions
diff --git a/moses/thread_safe_container.h b/moses/thread_safe_container.h
index 1983d7234..6a977185b 100644
--- a/moses/thread_safe_container.h
+++ b/moses/thread_safe_container.h
@@ -18,108 +18,104 @@
namespace Moses
{
- // todo: replace this with thread lock-free containers, if a stable library can
- // be found somewhere
+// todo: replace this with thread lock-free containers, if a stable library can
+// be found somewhere
- template<typename KEY, typename VAL, class CONTAINER = std::map<KEY,VAL> >
- class
+template<typename KEY, typename VAL, class CONTAINER = std::map<KEY,VAL> >
+class
ThreadSafeContainer
+{
+protected:
+ mutable boost::shared_mutex m_lock;
+ CONTAINER m_container;
+ typedef typename CONTAINER::iterator iter_t;
+ typedef typename CONTAINER::const_iterator const_iter_t;
+ typedef typename CONTAINER::value_type entry_t;
+public:
+
+ class locking_iterator
{
- protected:
- mutable boost::shared_mutex m_lock;
- CONTAINER m_container;
- typedef typename CONTAINER::iterator iter_t;
- typedef typename CONTAINER::const_iterator const_iter_t;
- typedef typename CONTAINER::value_type entry_t;
- public:
+ boost::unique_lock<boost::shared_mutex> m_lock;
+ CONTAINER const* m_container;
+ const_iter_t m_iter;
- class locking_iterator
- {
- boost::unique_lock<boost::shared_mutex> m_lock;
- CONTAINER const* m_container;
- const_iter_t m_iter;
-
- locking_iterator(locking_iterator const& other); // no copies!
- public:
- locking_iterator() : m_container(NULL) { }
-
- locking_iterator(boost::shared_mutex& lock,
- CONTAINER const* container,
- const_iter_t const& iter)
- : m_lock(lock), m_container(container), m_iter(iter)
- { }
-
- entry_t const& operator->()
- {
- UTIL_THROW_IF2(m_container == NULL, "This locking iterator is invalid "
- << "or has not been assigned.");
- return m_iter.operator->();
- }
-
- // locking operators transfer the lock upon assignment and become invalid
- locking_iterator const&
- operator=(locking_iterator& other)
- {
- m_lock.swap(other.m_lock);
- m_iter = other.m_iter;
- other.m_iter = other.m_container.end();
- }
-
- bool
- operator==(const_iter_t const& other)
- {
- return m_iter == other;
- }
-
- locking_iterator const&
- operator++() { ++m_iter; return *this; }
-
- // DO NOT DEFINE THE POST-INCREMENT OPERATOR!
- // locking_operators are non-copyable,
- // so we can't simply make a copy before incrementing and return
- // the copy after incrementing
- locking_iterator const&
- operator++(int);
- };
-
- const_iter_t const& end() const
- { return m_container.end(); }
-
- locking_iterator begin() const
- {
- return locking_iterator(m_lock, this, m_container.begin());
+ locking_iterator(locking_iterator const& other); // no copies!
+ public:
+ locking_iterator() : m_container(NULL) { }
+
+ locking_iterator(boost::shared_mutex& lock,
+ CONTAINER const* container,
+ const_iter_t const& iter)
+ : m_lock(lock), m_container(container), m_iter(iter)
+ { }
+
+ entry_t const& operator->() {
+ UTIL_THROW_IF2(m_container == NULL, "This locking iterator is invalid "
+ << "or has not been assigned.");
+ return m_iter.operator->();
}
- VAL const& set(KEY const& key, VAL const& val)
- {
- boost::unique_lock< boost::shared_mutex > lock(m_lock);
- entry_t entry(key,val);
- iter_t foo = m_container.insert(entry).first;
- foo->second = val;
- return foo->second;
+ // locking operators transfer the lock upon assignment and become invalid
+ locking_iterator const&
+ operator=(locking_iterator& other) {
+ m_lock.swap(other.m_lock);
+ m_iter = other.m_iter;
+ other.m_iter = other.m_container.end();
}
- VAL const* get(KEY const& key, VAL const& default_val)
- {
- boost::shared_lock< boost::shared_mutex > lock(m_lock);
- entry_t entry(key, default_val);
- iter_t foo = m_container.insert(entry).first;
- return &(foo->second);
+ bool
+ operator==(const_iter_t const& other) {
+ return m_iter == other;
}
- VAL const* get(KEY const& key) const
- {
- boost::shared_lock< boost::shared_mutex > lock(m_lock);
- const_iter_t m = m_container.find(key);
- if (m == m_container.end()) return NULL;
- return &m->second;
+ locking_iterator const&
+ operator++() {
+ ++m_iter;
+ return *this;
}
- size_t erase(KEY const& key)
- {
- boost::unique_lock< boost::shared_mutex > lock(m_lock);
- return m_container.erase(key);
- }
+ // DO NOT DEFINE THE POST-INCREMENT OPERATOR!
+ // locking_operators are non-copyable,
+ // so we can't simply make a copy before incrementing and return
+ // the copy after incrementing
+ locking_iterator const&
+ operator++(int);
};
+
+ const_iter_t const& end() const {
+ return m_container.end();
+ }
+
+ locking_iterator begin() const {
+ return locking_iterator(m_lock, this, m_container.begin());
+ }
+
+ VAL const& set(KEY const& key, VAL const& val) {
+ boost::unique_lock< boost::shared_mutex > lock(m_lock);
+ entry_t entry(key,val);
+ iter_t foo = m_container.insert(entry).first;
+ foo->second = val;
+ return foo->second;
+ }
+
+ VAL const* get(KEY const& key, VAL const& default_val) {
+ boost::shared_lock< boost::shared_mutex > lock(m_lock);
+ entry_t entry(key, default_val);
+ iter_t foo = m_container.insert(entry).first;
+ return &(foo->second);
+ }
+
+ VAL const* get(KEY const& key) const {
+ boost::shared_lock< boost::shared_mutex > lock(m_lock);
+ const_iter_t m = m_container.find(key);
+ if (m == m_container.end()) return NULL;
+ return &m->second;
+ }
+
+ size_t erase(KEY const& key) {
+ boost::unique_lock< boost::shared_mutex > lock(m_lock);
+ return m_container.erase(key);
+ }
+};
}
#endif