Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-05-23 02:04:52

0001 // -*- C++ -*-
0002 //
0003 // Package:     EDProduct
0004 // Class  :     EDProductGetter
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Tue Nov  1 15:06:41 EST 2005
0011 //
0012 
0013 // system include files
0014 #include <atomic>
0015 
0016 // user include files
0017 #include "DataFormats/Common/interface/EDProductGetter.h"
0018 #include "DataFormats/Provenance/interface/ProductID.h"
0019 #include "FWCore/Utilities/interface/EDMException.h"
0020 #include "FWCore/Utilities/interface/Likely.h"
0021 
0022 namespace edm {
0023   //
0024   // constants, enums and typedefs
0025   //
0026 
0027   //
0028   // static data member definitions
0029   //
0030 
0031   //
0032   // constructors and destructor
0033   //
0034   EDProductGetter::EDProductGetter() {}
0035 
0036   // EDProductGetter::EDProductGetter(EDProductGetter const& rhs)
0037   // {
0038   //    // do actual copying here;
0039   // }
0040 
0041   EDProductGetter::~EDProductGetter() {}
0042 
0043   //
0044   // assignment operators
0045   //
0046   // EDProductGetter const& EDProductGetter::operator=(EDProductGetter const& rhs)
0047   // {
0048   //   //An exception safe implementation is
0049   //   EDProductGetter temp(rhs);
0050   //   swap(rhs);
0051   //
0052   //   return *this;
0053   // }
0054 
0055   //
0056   // member functions
0057   //
0058 
0059   //
0060   // const member functions
0061   //
0062 
0063   //
0064   // static member functions
0065   //
0066 
0067   EDProductGetter const* mustBeNonZero(EDProductGetter const* prodGetter,
0068                                        std::string refType,
0069                                        ProductID const& productID) {
0070     if (prodGetter != nullptr)
0071       return prodGetter;
0072     throw Exception(errors::InvalidReference, refType)
0073         << "Attempt to construct a " << refType << " with ProductID " << productID << "\n"
0074         << "but with a null pointer to a product getter.\n"
0075         << "The product getter pointer passed to the constructor must refer\n"
0076         << "to a real getter, such as an EventPrincipal.\n";
0077   }
0078 
0079   thread_local EDProductGetter const* s_productGetter = nullptr;
0080   static std::atomic<EDProductGetter const*> s_multiThreadProductGetter{nullptr};
0081   EDProductGetter const* EDProductGetter::switchProductGetter(EDProductGetter const* iNew) {
0082     //std::cout <<"switch from "<<s_productGetter<<" to "<<iNew<<std::endl;
0083     EDProductGetter const* old = s_productGetter;
0084     s_productGetter = iNew;
0085     return old;
0086   }
0087 
0088   void EDProductGetter::setMultiThreadProductGetter(EDProductGetter const* prodGetter) {
0089     EDProductGetter const* expected = nullptr;
0090     while (not s_multiThreadProductGetter.compare_exchange_strong(expected, prodGetter, std::memory_order_acq_rel)) {
0091       expected = nullptr;
0092     };
0093   }
0094 
0095   void EDProductGetter::unsetMultiThreadProductGetter() {
0096     s_multiThreadProductGetter.store(nullptr, std::memory_order_release);
0097   }
0098 
0099   void EDProductGetter::assignEDProductGetter(EDProductGetter const*& iGetter) {
0100     //std::cout <<"assign "<<s_productGetter<<std::endl;
0101     if LIKELY (s_productGetter != nullptr) {
0102       iGetter = s_productGetter;
0103       return;
0104     }
0105     iGetter = s_multiThreadProductGetter.load(std::memory_order_acquire);
0106   }
0107 
0108 }  // namespace edm