Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:20

0001 #ifndef FWCore_Utilities_ThreadSafeRegistry_icc
0002 #define FWCore_Utilities_ThreadSafeRegistry_icc
0003 #include <mutex>
0004 
0005 #include "FWCore/Utilities/interface/ThreadSafeRegistry.h"
0006 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0007 
0008 namespace edm {
0009   namespace detail {
0010     // ----------------------------------------------------------------------
0011     template <typename KEY, typename T>
0012     ThreadSafeRegistry<KEY, T>* ThreadSafeRegistry<KEY, T>::instance() {
0013       CMS_THREAD_SAFE static ThreadSafeRegistry<KEY, T> me;
0014       return &me;
0015     }
0016 
0017     template <typename KEY, typename T>
0018     bool ThreadSafeRegistry<KEY, T>::getMapped(key_type const& k, value_type& result) const {
0019       bool found;
0020       typename collection_type::const_iterator i;
0021       {
0022         // This scope limits the lifetime of the lock to the shorted
0023         // required interval.
0024         std::lock_guard<std::mutex> lock(mutex_);
0025         i = data_.find(k);
0026         found = (i != data_.end());
0027       }
0028       if (found)
0029         result = i->second;
0030       return found;
0031     }
0032 
0033     template <typename KEY, typename T>
0034     typename ThreadSafeRegistry<KEY, T>::value_type const* ThreadSafeRegistry<KEY, T>::getMapped(
0035         key_type const& k) const {
0036       bool found;
0037       typename collection_type::const_iterator i;
0038       {
0039         // This scope limits the lifetime of the lock to the shorted
0040         // required interval.
0041         std::lock_guard<std::mutex> lock(mutex_);
0042         i = data_.find(k);
0043         found = (i != data_.end());
0044       }
0045       return found ? &(i->second) : static_cast<value_type const*>(nullptr);
0046     }
0047 
0048     template <typename KEY, typename T>
0049     bool ThreadSafeRegistry<KEY, T>::insertMapped(value_type const& v) {
0050       bool newly_added = false;
0051       std::lock_guard<std::mutex> lock(mutex_);
0052 
0053       key_type id = v.id();
0054       if (data_.find(id) == data_.end()) {
0055         data_[id] = v;
0056         newly_added = true;
0057       }
0058       return newly_added;
0059     }
0060 
0061   }  // namespace detail
0062 }  // namespace edm
0063 
0064 // To be used to explicitly instanciate the template code and
0065 // static variables, so that they work even in the case of hidden symbol visibility
0066 // in plugins.
0067 #define DEFINE_THREAD_SAFE_REGISTRY_INSTANCE(X)                                                         \
0068   namespace edm {                                                                                       \
0069     namespace detail {                                                                                  \
0070       template ThreadSafeRegistry<X::key_type, X::value_type>*                                          \
0071       ThreadSafeRegistry<X::key_type, X::value_type>::instance();                                       \
0072       template bool ThreadSafeRegistry<X::key_type, X::value_type>::getMapped(X::key_type const&,       \
0073                                                                               X::value_type&) const;    \
0074       template X::value_type const* ThreadSafeRegistry<X::key_type, X::value_type>::getMapped(          \
0075           X::key_type const&) const;                                                                    \
0076       template bool ThreadSafeRegistry<X::key_type, X::value_type>::insertMapped(X::value_type const&); \
0077     }                                                                                                   \
0078   }
0079 
0080 #endif  //  FWCore_Utilities_ThreadSafeRegistry_icc