AsyncService

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "FWCore/Concurrency/interface/Async.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"

#include <atomic>

namespace edm::service {
  class AsyncService : public Async {
  public:
    AsyncService(ParameterSet const& iConfig, ActivityRegistry& iRegistry);

    static void fillDescriptions(ConfigurationDescriptions& descriptions);

  private:
    void ensureAllowed() const final;

    std::atomic<bool> allowed_ = true;
  };

  AsyncService::AsyncService(ParameterSet const& iConfig, ActivityRegistry& iRegistry) {
    iRegistry.watchPreSourceEarlyTermination([this](TerminationOrigin) { allowed_ = false; });
    iRegistry.watchPreGlobalEarlyTermination([this](GlobalContext const&, TerminationOrigin) { allowed_ = false; });
    iRegistry.watchPreStreamEarlyTermination([this](StreamContext const&, TerminationOrigin) { allowed_ = false; });
    iRegistry.watchPostEndJob([this]() { allowed_ = false; });
  }

  void AsyncService::fillDescriptions(ConfigurationDescriptions& descriptions) {
    ParameterSetDescription desc;
    descriptions.addDefault(desc);
  }

  void AsyncService::ensureAllowed() const {
    if (not allowed_) {
      cms::Exception ex("AsyncCallNotAllowed");
      ex.addContext("Calling Async::run()");
      ex << "Framework is shutting down, further run() calls are not allowed";
      throw ex;
    }
  }
}  // namespace edm::service

#include "FWCore/ServiceRegistry/interface/ServiceMaker.h"

using edm::service::AsyncService;
using AsyncMaker = edm::serviceregistry::AllArgsMaker<edm::Async, AsyncService>;
DEFINE_FWK_SERVICE_MAKER(AsyncService, AsyncMaker);