CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends
edm::SharedResourcesRegistry Class Reference

#include <SharedResourcesRegistry.h>

Public Member Functions

SharedResourcesAcquirer createAcquirer (std::vector< std::string > const &) const
 
SharedResourcesAcquirer createAcquirerForSourceDelayedReader ()
 
void registerSharedResource (const std::string &)
 A resource name must be registered before it can be used in the createAcquirer call. More...
 

Static Public Member Functions

static SharedResourcesRegistryinstance ()
 

Static Public Attributes

static const std::string kLegacyModuleResourceName {"__legacy__"}
 All legacy modules share this resource. More...
 

Private Member Functions

const SharedResourcesRegistryoperator= (const SharedResourcesRegistry &)=delete
 
 SharedResourcesRegistry ()
 
 SharedResourcesRegistry (const SharedResourcesRegistry &)=delete
 
 ~SharedResourcesRegistry ()=default
 

Private Attributes

unsigned int nLegacy_
 
std::shared_ptr
< std::recursive_mutex > 
resourceForDelayedReader_
 
std::map< std::string,
std::pair< std::shared_ptr
< std::recursive_mutex >
, unsigned int > > 
resourceMap_
 

Friends

class ::testSharedResourcesRegistry
 

Detailed Description

Definition at line 36 of file SharedResourcesRegistry.h.

Constructor & Destructor Documentation

SharedResourcesRegistry::SharedResourcesRegistry ( )
private

Definition at line 31 of file SharedResourcesRegistry.cc.

31  : nLegacy_(0) {
32  }
edm::SharedResourcesRegistry::~SharedResourcesRegistry ( )
privatedefault
edm::SharedResourcesRegistry::SharedResourcesRegistry ( const SharedResourcesRegistry )
privatedelete

Member Function Documentation

SharedResourcesAcquirer SharedResourcesRegistry::createAcquirer ( std::vector< std::string > const &  resourceNames) const

Definition at line 78 of file SharedResourcesRegistry.cc.

References spr::find(), kLegacyModuleResourceName, mergeVDriftHistosByStation::name, and resourceMap_.

Referenced by edm::one::impl::SharedResourcesUser< T >::createAcquirer(), edm::EDAnalyzer::doBeginJob(), edm::EDProducer::doBeginJob(), edm::EDFilter::doBeginJob(), and edm::OutputModule::doBeginJob().

78  {
79 
80  // The acquirer will acquire the shared resources declared by a module
81  // so that only it can use those resources while it runs. The other
82  // modules using the same resource will not be run until the module
83  // that acquired the resources completes its task.
84 
85  // The legacy shared resource is special.
86  // Legacy modules cannot run concurrently with each other or
87  // any other module that has declared any shared resource. Treat
88  // one modules that call usesResource with no argument in the
89  // same way.
90 
91  // Sort by how often used and then by name
92  // Consistent sorting avoids deadlocks and this particular order optimizes performance
93  std::map<std::pair<unsigned int, std::string>, std::recursive_mutex*> sortedResources;
94 
95  // Is this acquirer for a module that depends on the legacy shared resource?
96  if(std::find(resourceNames.begin(), resourceNames.end(), kLegacyModuleResourceName) != resourceNames.end()) {
97 
98  for(auto const& resource : resourceMap_) {
99  // It's redundant to declare legacy if the legacy modules
100  // all declare all the other resources, so just skip it.
101  // But if the only shared resource is the legacy resource don't skip it.
102  if(resource.first == kLegacyModuleResourceName && resourceMap_.size() > 1) continue;
103  //If only one module wants it, it really isn't shared
104  if(resource.second.second > 1) {
105  sortedResources.insert(std::make_pair(std::make_pair(resource.second.second, resource.first),resource.second.first.get()));
106  }
107  }
108  // Handle cases where the module does not declare the legacy resource
109  } else {
110  for(auto const& name : resourceNames) {
111  auto resource = resourceMap_.find(name);
112  assert(resource != resourceMap_.end());
113  //If only one module wants it, it really isn't shared
114  if(resource->second.second > 1) {
115  sortedResources.insert(std::make_pair(std::make_pair(resource->second.second, resource->first),resource->second.first.get()));
116  }
117  }
118  }
119 
120  std::vector<std::recursive_mutex*> mutexes;
121  mutexes.reserve(sortedResources.size());
122  for(auto const& resource: sortedResources) {
123  mutexes.push_back(resource.second);
124  }
125  return SharedResourcesAcquirer(std::move(mutexes));
126  }
std::map< std::string, std::pair< std::shared_ptr< std::recursive_mutex >, unsigned int > > resourceMap_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.
SharedResourcesAcquirer SharedResourcesRegistry::createAcquirerForSourceDelayedReader ( )

Definition at line 68 of file SharedResourcesRegistry.cc.

References resourceForDelayedReader_.

Referenced by edm::PoolSource::PoolSource().

68  {
70  resourceForDelayedReader_.reset(new std::recursive_mutex{});
71  }
72  std::vector<std::recursive_mutex*> mutexes = {resourceForDelayedReader_.get()};
73 
74  return SharedResourcesAcquirer(std::move(mutexes));
75  }
std::shared_ptr< std::recursive_mutex > resourceForDelayedReader_
SharedResourcesRegistry * SharedResourcesRegistry::instance ( )
static
const SharedResourcesRegistry& edm::SharedResourcesRegistry::operator= ( const SharedResourcesRegistry )
privatedelete
void SharedResourcesRegistry::registerSharedResource ( const std::string &  resourceName)

A resource name must be registered before it can be used in the createAcquirer call.

Definition at line 35 of file SharedResourcesRegistry.cc.

References kLegacyModuleResourceName, nLegacy_, and resourceMap_.

Referenced by edm::EDAnalyzer::EDAnalyzer(), edm::EDFilter::EDFilter(), edm::EDProducer::EDProducer(), edm::OutputModule::OutputModule(), and edm::one::impl::SharedResourcesUser< T >::usesResource().

35  {
36 
37  auto& mutexAndCounter = resourceMap_[resourceName];
38 
39  if(resourceName == kLegacyModuleResourceName) {
40  ++nLegacy_;
41  for(auto & resource : resourceMap_) {
42  if(!resource.second.first) {
43  resource.second.first = std::make_shared<std::recursive_mutex>();
44  }
45  ++resource.second.second;
46  }
47  } else {
48  // count the number of times the resource was registered
49  ++mutexAndCounter.second;
50 
51  // When first registering a nonlegacy resource, we have to
52  // account for any legacy resource registrations already made.
53  if(mutexAndCounter.second == 1) {
54  if(nLegacy_ > 0U) {
55  mutexAndCounter.first = std::make_shared<std::recursive_mutex>();
56  mutexAndCounter.second += nLegacy_;
57  }
58  // If registering a nonlegacy resource the second time and
59  // the legacy resource has not been registered yet,
60  // we know we will need the mutex so go ahead and create it.
61  } else if(mutexAndCounter.second == 2) {
62  mutexAndCounter.first = std::make_shared<std::recursive_mutex>();
63  }
64  }
65  }
std::map< std::string, std::pair< std::shared_ptr< std::recursive_mutex >, unsigned int > > resourceMap_
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.

Friends And Related Function Documentation

friend class ::testSharedResourcesRegistry
friend

Definition at line 41 of file SharedResourcesRegistry.h.

Member Data Documentation

const std::string SharedResourcesRegistry::kLegacyModuleResourceName {"__legacy__"}
static
unsigned int edm::SharedResourcesRegistry::nLegacy_
private

Definition at line 75 of file SharedResourcesRegistry.h.

Referenced by registerSharedResource().

std::shared_ptr<std::recursive_mutex> edm::SharedResourcesRegistry::resourceForDelayedReader_
private

Definition at line 73 of file SharedResourcesRegistry.h.

Referenced by createAcquirerForSourceDelayedReader().

std::map<std::string, std::pair<std::shared_ptr<std::recursive_mutex>,unsigned int> > edm::SharedResourcesRegistry::resourceMap_
private

Definition at line 71 of file SharedResourcesRegistry.h.

Referenced by createAcquirer(), and registerSharedResource().