CMS 3D CMS Logo

SharedResourcesRegistry.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCore/Framework
4 // Class : SharedResourcesRegistry
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Sun, 06 Oct 2013 15:48:50 GMT
11 //
12 
13 #include <algorithm>
14 #include <cassert>
15 
18 
19 namespace edm {
20 
22  static SharedResourcesRegistry s_instance;
23  return &s_instance;
24  }
25 
27 
29  auto& queueAndCounter = resourceMap_[resourceName];
30 
31  // count the number of times the resource was registered
32  ++queueAndCounter.second;
33 
34  // If registering a resource the second time
35  // we know we will need the queue so go ahead and create it.
36  if (queueAndCounter.second == 2) {
37  queueAndCounter.first = std::make_shared<SerialTaskQueue>();
38  }
39  }
40 
41  std::pair<SharedResourcesAcquirer, std::shared_ptr<std::recursive_mutex>>
43  if (not resourceForDelayedReader_) {
45  std::make_shared<std::recursive_mutex>(); // propagate_const<T> has no reset() function
46  queueForDelayedReader_ = std::make_shared<SerialTaskQueue>();
47  }
48 
49  std::vector<std::shared_ptr<SerialTaskQueue>> queues = {get_underlying(queueForDelayedReader_)};
51  }
52 
53  SharedResourcesAcquirer SharedResourcesRegistry::createAcquirer(std::vector<std::string> const& resourceNames) const {
54  // The acquirer will acquire the shared resources declared by a module
55  // so that only it can use those resources while it runs. The other
56  // modules using the same resource will not be run until the module
57  // that acquired the resources completes its task.
58 
59  // Sort by how often used and then by name
60  // Consistent sorting avoids deadlocks and this particular order optimizes performance
61  std::map<std::pair<unsigned int, std::string>, std::shared_ptr<SerialTaskQueue>> sortedResources;
62 
63  for (auto const& name : resourceNames) {
64  auto resource = resourceMap_.find(name);
65  assert(resource != resourceMap_.end());
66  // If only one module wants it, it really isn't shared
67  if (resource->second.second > 1) {
68  sortedResources.insert(
69  std::make_pair(std::make_pair(resource->second.second, resource->first), resource->second.first));
70  }
71  }
72 
73  std::vector<std::shared_ptr<SerialTaskQueue>> queues;
74  queues.reserve(sortedResources.size());
75  for (auto const& resource : sortedResources) {
76  queues.push_back(resource.second);
77  }
78  if (queues.empty()) {
79  //Calling code is depending on there being at least one shared queue
80  queues.reserve(1);
81  queues.push_back(std::make_shared<SerialTaskQueue>());
82  }
83 
84  return SharedResourcesAcquirer(std::move(queues));
85  }
86 } // namespace edm
edm::propagate_const< std::shared_ptr< SerialTaskQueue > > queueForDelayedReader_
assert(be >=bs)
static SharedResourcesRegistry * instance()
std::map< std::string, std::pair< std::shared_ptr< SerialTaskQueue >, unsigned int > > resourceMap_
edm::propagate_const< std::shared_ptr< std::recursive_mutex > > resourceForDelayedReader_
void registerSharedResource(const std::string &)
A resource name must be registered before it can be used in the createAcquirer call.
constexpr T & get_underlying(propagate_const< T > &)
HLT enums.
std::pair< SharedResourcesAcquirer, std::shared_ptr< std::recursive_mutex > > createAcquirerForSourceDelayedReader()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
def move(src, dest)
Definition: eostools.py:511