jrespool.tpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 __JRESPOOL_TPP
  14. #define __JRESPOOL_TPP
  15. #include "jlib.hpp"
  16. #include "jexcept.hpp"
  17. #include "jarray.hpp"
  18. #include "jmutex.hpp"
  19. #include "jsem.hpp"
  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 CResourcePool : public CInterface
  26. {
  27. public:
  28. CResourcePool()
  29. {
  30. numresources = 0;
  31. resources = NULL;
  32. }
  33. CResourcePool(unsigned size,IResourceFactory<T>* fac): factory(fac)
  34. {
  35. numresources = size;
  36. resources = (T **)calloc(sizeof(T *),numresources);
  37. }
  38. ~CResourcePool()
  39. {
  40. for (unsigned i=0;i<numresources;i++)
  41. ::Release(resources[i]);
  42. free(resources);
  43. }
  44. void init(unsigned size,IResourceFactory<T>* fac)
  45. {
  46. CriticalBlock b(crit);
  47. if(resources)
  48. {
  49. for (unsigned i=0;i<numresources;i++)
  50. ::Release(resources[i]);
  51. free(resources);
  52. }
  53. numresources = size;
  54. resources = (T **)calloc(sizeof(T *),numresources);
  55. factory.set(fac);
  56. }
  57. T * get(long timeout=0)
  58. {
  59. const long interval=1000;
  60. for(;;)
  61. {
  62. {
  63. CriticalBlock b(crit);
  64. for (unsigned i=0;i<numresources;i++)
  65. {
  66. T * &it = resources[i];
  67. if(it == NULL)
  68. it = factory->createResource();
  69. else if (it->IsShared())
  70. continue;
  71. it->Link();
  72. return it;
  73. }
  74. }
  75. long slp=timeout!=INFINITE && timeout<interval ? timeout : interval;
  76. if(slp<=0)
  77. break;
  78. long start=msTick();
  79. long elapsed=sem.wait(slp) ? (msTick()-start) : slp;
  80. if(timeout!=INFINITE)
  81. timeout-=elapsed;
  82. }
  83. throw MakeStringException(1, "Run out of resources");
  84. }
  85. void release()
  86. {
  87. sem.signal();
  88. }
  89. protected:
  90. CriticalSection crit;
  91. Semaphore sem;
  92. unsigned numresources;
  93. T **resources;
  94. Linked<IResourceFactory<T> > factory;
  95. };
  96. #endif