test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 //
63 //
64 // Original Author: Chris Jones
65 // Created: Fri, 31 July 2014 14:29:41 GMT
66 //
67 
68 #include <memory>
69 #include <cassert>
70 #include <atomic>
71 #include "tbb/task.h"
72 #include "tbb/concurrent_queue.h"
73 
74 
75 namespace edm {
76  template<class T>
78  public:
80 
83  T* item = 0;
84  while( m_availableQueue.try_pop(item)) {
85  delete item;
86  }
87  }
88 
92  void add(std::unique_ptr<T> iItem){
93  if(0!=iItem) {
94  m_availableQueue.push(iItem.release());
95  }
96  }
97 
101  std::shared_ptr<T> tryToGet() {
102  T* item = 0;
103  m_availableQueue.try_pop(item);
104  if (0==item) {
105  return std::shared_ptr<T>{};
106  }
107  //instead of deleting, hand back to queue
108  auto pHolder = this;
110  return std::shared_ptr<T>{item, [pHolder](T* iItem) {pHolder->addBack(iItem);} };
111  }
112 
114  template< typename F>
115  std::shared_ptr<T> makeOrGet( F iFunc) {
116  std::shared_ptr<T> returnValue;
117  while ( ! ( returnValue = tryToGet()) ) {
118  add( std::unique_ptr<T>(iFunc()) );
119  }
120  return returnValue;
121  }
122 
126  template< typename FM, typename FC>
127  std::shared_ptr<T> makeOrGetAndClear( FM iMakeFunc, FC iClearFunc) {
128  std::shared_ptr<T> returnValue;
129  while ( ! ( returnValue = tryToGet()) ) {
130  add( std::unique_ptr<T>(iMakeFunc()) );
131  }
132  iClearFunc(returnValue.get());
133  return returnValue;
134  }
135 
136  private:
137  void addBack(T* iItem){
138  m_availableQueue.push(iItem);
140  }
141 
142  tbb::concurrent_queue<T*> m_availableQueue;
143  std::atomic<size_t> m_outstandingObjects;
144  };
145 
146 }
147 
148 
149 
150 #endif /* end of include guard: FWCore_Utilities_ReusableObjectHolder_h */
tbb::concurrent_queue< T * > m_availableQueue
assert(m_qm.get())
std::shared_ptr< T > makeOrGet(F iFunc)
If there isn&#39;t an object already available, creates a new one using iFunc.
std::shared_ptr< T > makeOrGetAndClear(FM iMakeFunc, FC iClearFunc)
void add(std::unique_ptr< T > iItem)
std::shared_ptr< T > tryToGet()
std::atomic< size_t > m_outstandingObjects
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
long double T