respool.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef __RESPOOL_HPP
  14. #define __RESPOOL_HPP
  15. #include "jwrapper.hpp"
  16. #include "jexcept.hpp"
  17. #include <vector>
  18. namespace esp
  19. {
  20. template <typename T> interface IResourceFactory: implements IInterface
  21. {
  22. virtual T* createResource() = 0;
  23. };
  24. // T should implement IInterface
  25. template <typename T> class ResourcePool : public CInterface, implements IInterface
  26. {
  27. public:
  28. IMPLEMENT_IINTERFACE;
  29. ResourcePool()
  30. {
  31. }
  32. ResourcePool(unsigned size,IResourceFactory<T>* fac): resources(size), factory(fac)
  33. {
  34. }
  35. void init(unsigned size,IResourceFactory<T>* fac)
  36. {
  37. CriticalBlock b(crit);
  38. resources.clear();
  39. resources.resize(size);
  40. factory.set(fac);
  41. }
  42. Linked<T> get(long timeout=0)
  43. {
  44. const long interval=1000;
  45. for(;;)
  46. {
  47. {
  48. CriticalBlock b(crit);
  49. typename std::vector<Linked<T> >::iterator it;
  50. for(it=resources.begin();it!=resources.end();it++)
  51. {
  52. if(it->get() == NULL)
  53. {
  54. Owned<T> e = factory->createResource();
  55. if(e)
  56. {
  57. it->set(e.get());
  58. return e;
  59. }
  60. }
  61. else if(!it->get()->IsShared())
  62. {
  63. return *it;
  64. }
  65. }
  66. }
  67. long slp=timeout!=INFINITE && timeout<interval ? timeout : interval;
  68. if(slp<=0)
  69. break;
  70. long start=msTick();
  71. long elapsed=sem.wait(slp) ? (msTick()-start) : slp;
  72. if(timeout!=INFINITE)
  73. timeout-=elapsed;
  74. }
  75. throw MakeStringException(1, "Run out of resources");
  76. }
  77. void release()
  78. {
  79. sem.signal();
  80. }
  81. void clearOne(T* t)
  82. {
  83. if(!t)
  84. return;
  85. {
  86. CriticalBlock b(crit);
  87. typename std::vector<Linked<T> >::iterator it;
  88. for(it=resources.begin();it!=resources.end();it++)
  89. {
  90. if(it->get() && it->get() == t)
  91. {
  92. it->clear();
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. void clearAll()
  99. {
  100. CriticalBlock b(crit);
  101. typename std::vector<Linked<T> >::iterator it;
  102. for(it=resources.begin();it!=resources.end();it++)
  103. {
  104. if(it->get())
  105. {
  106. it->clear();
  107. }
  108. }
  109. }
  110. protected:
  111. CriticalSection crit;
  112. Semaphore sem;
  113. std::vector<Linked<T> > resources;
  114. Linked<IResourceFactory<T> > factory;
  115. };
  116. }
  117. #endif