Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-05-23 23:48:33

0001 // -*- C++ -*-
0002 #ifndef FWCore_Framework_ESSourceProductResolverBase_h
0003 #define FWCore_Framework_ESSourceProductResolverBase_h
0004 //
0005 // Package:     FWCore/Framework
0006 // Class  :     ESSourceProductResolverBase
0007 //
0008 /**\class edm::eventsetup::ESSourceProductResolverBase
0009 
0010  Description: Base class for ESProductResolvers for ESSources that can be specialized based on concurrency needs
0011 
0012  Usage:
0013     The ESSourceProductResolverBase provides the bases for ProductResolvers needed for ESSources.
0014     It allows customization of synchronization needs via the use of template parameters.
0015 
0016     NOTE: if inheriting classes override `void invalidateCache()` they must be sure to call this classes
0017     implementation as part of the call.
0018 
0019 */
0020 //
0021 // Original Author:  Chris Jones
0022 //         Created:  14/05/2020
0023 //
0024 
0025 // system include files
0026 #include <atomic>
0027 
0028 // user include files
0029 #include "FWCore/Framework/interface/ESProductResolver.h"
0030 #include "FWCore/Framework/interface/EventSetupRecordDetails.h"
0031 #include "FWCore/Concurrency/interface/WaitingTaskList.h"
0032 #include "FWCore/ServiceRegistry/interface/ESParentContext.h"
0033 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0034 
0035 // forward declarations
0036 
0037 namespace edm::eventsetup {
0038   class ESSourceProductResolverBase : public ESProductResolver {
0039   public:
0040     ESSourceProductResolverBase() : m_prefetching{false} {}
0041 
0042   protected:
0043     void invalidateCache() override {
0044       m_waitingList.reset();
0045       m_prefetching = false;
0046     }
0047     void invalidateTransientCache() override {}
0048 
0049     virtual void prefetch(edm::eventsetup::DataKey const& iKey, EventSetupRecordDetails) = 0;
0050 
0051     //Should call from prefetchAsyncImpl
0052     template <typename ASYNC, typename GUARD>
0053     void prefetchAsyncImplTemplate(ASYNC iAsync,
0054                                    GUARD iGuardFactory,
0055                                    edm::WaitingTaskHolder iTask,
0056                                    edm::eventsetup::EventSetupRecordImpl const& iRecord,
0057                                    edm::eventsetup::DataKey const& iKey,
0058                                    edm::ESParentContext const& iContext) noexcept {
0059       auto group = iTask.group();
0060       if (needToPrefetch(std::move(iTask))) {
0061         iAsync(*group, [this, iGuardFactory, &iRecord, iKey, iContext]() {
0062           CMS_SA_ALLOW try {
0063             guardPrefetch(iGuardFactory, iRecord, iKey, iContext);
0064             m_waitingList.doneWaiting(std::exception_ptr{});
0065           } catch (...) {
0066             m_waitingList.doneWaiting(std::current_exception());
0067           }
0068         });
0069       }
0070     }
0071 
0072   private:
0073     template <typename GUARD>
0074     void guardPrefetch(GUARD iGuardFactory,
0075                        edm::eventsetup::EventSetupRecordImpl const& iES,
0076                        edm::eventsetup::DataKey const& iKey,
0077                        edm::ESParentContext const& iContext) {
0078       [[maybe_unused]] auto guard = iGuardFactory();
0079       doPrefetchAndSignals(iES, iKey, iContext);
0080     }
0081 
0082     bool needToPrefetch(edm::WaitingTaskHolder iTask) noexcept;
0083 
0084     void doPrefetchAndSignals(edm::eventsetup::EventSetupRecordImpl const&,
0085                               edm::eventsetup::DataKey const& iKey,
0086                               edm::ESParentContext const&);
0087 
0088     // ---------- member data --------------------------------
0089 
0090     edm::WaitingTaskList m_waitingList;
0091     std::atomic<bool> m_prefetching;
0092   };
0093 }  // namespace edm::eventsetup
0094 #endif