CMS 3D CMS Logo

ReusableObjectHolder.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_ReusableObjectHolder_h
2 #define FWCore_Utilities_ReusableObjectHolder_h
3 
4 // -*- C++ -*-
5 //
6 // Package: FWCore/Utilities
7 // Class : ReusableObjectHolder
8 //
67 //
68 // Original Author: Chris Jones
69 // Created: Fri, 31 July 2014 14:29:41 GMT
70 //
71 
72 #include <memory>
73 #include <cassert>
74 #include <atomic>
75 #include "tbb/task.h"
76 #include "tbb/concurrent_queue.h"
77 
78 namespace edm {
79  template <class T, class Deleter = std::default_delete<T>>
81  public:
82  using deleter_type = Deleter;
83 
87  assert(0 == iOther.m_outstandingObjects);
88  }
91  std::unique_ptr<T, Deleter> item;
92  while (m_availableQueue.try_pop(item)) {
93  item.reset();
94  }
95  }
96 
100  void add(std::unique_ptr<T, Deleter> iItem) {
101  if (nullptr != iItem) {
102  m_availableQueue.push(std::move(iItem));
103  }
104  }
105 
109  std::shared_ptr<T> tryToGet() {
110  std::unique_ptr<T, Deleter> item;
111  m_availableQueue.try_pop(item);
112  if (nullptr == item) {
113  return std::shared_ptr<T>{};
114  }
115  //instead of deleting, hand back to queue
116  auto pHolder = this;
117  auto deleter = item.get_deleter();
119  return std::shared_ptr<T>{item.release(), [pHolder, deleter](T* iItem) {
120  pHolder->addBack(std::unique_ptr<T, Deleter>{iItem, deleter});
121  }};
122  }
123 
125  template <typename F>
126  std::shared_ptr<T> makeOrGet(F iFunc) {
127  std::shared_ptr<T> returnValue;
128  while (!(returnValue = tryToGet())) {
129  add(makeUnique(iFunc()));
130  }
131  return returnValue;
132  }
133 
137  template <typename FM, typename FC>
138  std::shared_ptr<T> makeOrGetAndClear(FM iMakeFunc, FC iClearFunc) {
139  std::shared_ptr<T> returnValue;
140  while (!(returnValue = tryToGet())) {
141  add(makeUnique(iMakeFunc()));
142  }
143  iClearFunc(returnValue.get());
144  return returnValue;
145  }
146 
147  private:
148  std::unique_ptr<T> makeUnique(T* ptr) {
149  static_assert(std::is_same_v<Deleter, std::default_delete<T>>,
150  "Generating functions returning raw pointers are supported only with std::default_delete<T>");
151  return std::unique_ptr<T>{ptr};
152  }
153 
154  std::unique_ptr<T, Deleter> makeUnique(std::unique_ptr<T, Deleter> ptr) { return ptr; }
155 
156  void addBack(std::unique_ptr<T, Deleter> iItem) {
157  m_availableQueue.push(std::move(iItem));
159  }
160 
161  tbb::concurrent_queue<std::unique_ptr<T, Deleter>> m_availableQueue;
162  std::atomic<size_t> m_outstandingObjects;
163  };
164 
165 } // namespace edm
166 
167 #endif /* end of include guard: FWCore_Utilities_ReusableObjectHolder_h */
edm::ReusableObjectHolder::makeUnique
std::unique_ptr< T > makeUnique(T *ptr)
Definition: ReusableObjectHolder.h:148
edm
HLT enums.
Definition: AlignableModifier.h:19
deleter
Definition: QcdLowPtDQM.cc:34
edm::ReusableObjectHolder::ReusableObjectHolder
ReusableObjectHolder()
Definition: ReusableObjectHolder.h:84
edm::ReusableObjectHolder::m_outstandingObjects
std::atomic< size_t > m_outstandingObjects
Definition: ReusableObjectHolder.h:162
cms::cuda::assert
assert(be >=bs)
edm::ReusableObjectHolder::makeUnique
std::unique_ptr< T, Deleter > makeUnique(std::unique_ptr< T, Deleter > ptr)
Definition: ReusableObjectHolder.h:154
edm::ReusableObjectHolder::add
void add(std::unique_ptr< T, Deleter > iItem)
Definition: ReusableObjectHolder.h:100
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
edm::ReusableObjectHolder
Definition: ReusableObjectHolder.h:80
edm::ReusableObjectHolder::makeOrGet
std::shared_ptr< T > makeOrGet(F iFunc)
If there isn't an object already available, creates a new one using iFunc.
Definition: ReusableObjectHolder.h:126
edm::ReusableObjectHolder::~ReusableObjectHolder
~ReusableObjectHolder()
Definition: ReusableObjectHolder.h:89
edm::ReusableObjectHolder::ReusableObjectHolder
ReusableObjectHolder(ReusableObjectHolder &&iOther)
Definition: ReusableObjectHolder.h:85
edm::ReusableObjectHolder::makeOrGetAndClear
std::shared_ptr< T > makeOrGetAndClear(FM iMakeFunc, FC iClearFunc)
Definition: ReusableObjectHolder.h:138
edm::ReusableObjectHolder< edm::ESProductHost >::deleter_type
std::default_delete< edm::ESProductHost > deleter_type
Definition: ReusableObjectHolder.h:82
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
edm::ReusableObjectHolder::m_availableQueue
tbb::concurrent_queue< std::unique_ptr< T, Deleter > > m_availableQueue
Definition: ReusableObjectHolder.h:161
edm::ReusableObjectHolder::addBack
void addBack(std::unique_ptr< T, Deleter > iItem)
Definition: ReusableObjectHolder.h:156
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
T
long double T
Definition: Basic3DVectorLD.h:48
edm::ReusableObjectHolder::tryToGet
std::shared_ptr< T > tryToGet()
Definition: ReusableObjectHolder.h:109