Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-06-04 04:34:53

0001 #ifndef FWCore_Framework_WorkerInPath_h
0002 #define FWCore_Framework_WorkerInPath_h
0003 
0004 /*
0005 
0006     Author: Jim Kowalkowski 28-01-06
0007 
0008 
0009     A wrapper around a Worker, so that statistics can be managed
0010     per path.  A Path holds Workers as these things.
0011 
0012 */
0013 
0014 #include "FWCore/Framework/interface/maker/Worker.h"
0015 #include "FWCore/Concurrency/interface/WaitingTaskHolder.h"
0016 #include "FWCore/ServiceRegistry/interface/ParentContext.h"
0017 #include "FWCore/ServiceRegistry/interface/PlaceInPathContext.h"
0018 
0019 #include <utility>
0020 
0021 namespace edm {
0022 
0023   class PathContext;
0024   class StreamID;
0025   class ServiceToken;
0026 
0027   class WorkerInPath {
0028   public:
0029     enum FilterAction { Normal = 0, Ignore, Veto };
0030 
0031     WorkerInPath(Worker*, FilterAction theAction, unsigned int placeInPath, bool runConcurrently);
0032 
0033     template <typename T>
0034     void runWorkerAsync(WaitingTaskHolder,
0035                         typename T::TransitionInfoType const&,
0036                         ServiceToken const&,
0037                         StreamID,
0038                         typename T::Context const*) noexcept;
0039 
0040     bool checkResultsOfRunWorker(bool wasEvent);
0041 
0042     void skipWorker(EventPrincipal const& iPrincipal) { worker_->skipOnPath(iPrincipal); }
0043     void skipWorker(RunPrincipal const&) {}
0044     void skipWorker(LuminosityBlockPrincipal const&) {}
0045 
0046     void clearCounters() { timesVisited_ = timesPassed_ = timesFailed_ = timesExcept_ = 0; }
0047 
0048     int timesVisited() const { return timesVisited_; }
0049     int timesPassed() const { return timesPassed_; }
0050     int timesFailed() const { return timesFailed_; }
0051     int timesExcept() const { return timesExcept_; }
0052 
0053     FilterAction filterAction() const { return filterAction_; }
0054     Worker* getWorker() const { return worker_; }
0055     bool runConcurrently() const noexcept { return runConcurrently_; }
0056     unsigned int bitPosition() const noexcept { return placeInPathContext_.placeInPath(); }
0057 
0058     void setPathContext(PathContext const* v) { placeInPathContext_.setPathContext(v); }
0059 
0060   private:
0061     int timesVisited_;
0062     int timesPassed_;
0063     int timesFailed_;
0064     int timesExcept_;
0065 
0066     FilterAction filterAction_;
0067     Worker* worker_;
0068 
0069     PlaceInPathContext placeInPathContext_;
0070     bool runConcurrently_;
0071   };
0072 
0073   inline bool WorkerInPath::checkResultsOfRunWorker(bool wasEvent) {
0074     if (not wasEvent) {
0075       return true;
0076     }
0077     auto state = worker_->state();
0078     bool rc = true;
0079     switch (state) {
0080       case Worker::Fail: {
0081         rc = false;
0082         break;
0083       }
0084       case Worker::Pass:
0085         break;
0086       case Worker::Exception: {
0087         ++timesExcept_;
0088         return true;
0089       }
0090 
0091       default:
0092         assert(false);
0093     }
0094 
0095     if (Ignore == filterAction()) {
0096       rc = true;
0097     } else if (Veto == filterAction()) {
0098       rc = !rc;
0099     }
0100 
0101     if (rc) {
0102       ++timesPassed_;
0103     } else {
0104       ++timesFailed_;
0105     }
0106     return rc;
0107   }
0108 
0109   template <typename T>
0110   void WorkerInPath::runWorkerAsync(WaitingTaskHolder iTask,
0111                                     typename T::TransitionInfoType const& info,
0112                                     ServiceToken const& token,
0113                                     StreamID streamID,
0114                                     typename T::Context const* context) noexcept {
0115     static_assert(T::isEvent_);
0116 
0117     ++timesVisited_;
0118     ParentContext parentContext(&placeInPathContext_);
0119     worker_->doWorkAsync<T>(std::move(iTask), info, token, streamID, parentContext, context);
0120   }
0121 }  // namespace edm
0122 
0123 #endif