ObjectPool.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Yab Bin (yanb@es.zju.edu.cn) @ 2014-12
  4. //
  5. #ifndef __OBJECTPOOL_H
  6. #define __OBJECTPOOL_H
  7. #include "BaseTypes.h"
  8. #include "BaseHeaders.h"
  9. #include "Locks.h"
  10. namespace tzc {
  11. template <typename T>
  12. class ObjectPool {
  13. public:
  14. enum __OBJPOOL_ERROR {
  15. OPSucess = 0,
  16. OPNoFreeObj,
  17. OPNoTaskObj,
  18. OPEmptyPool
  19. };
  20. public:
  21. ObjectPool();
  22. ~ObjectPool();
  23. // return 0 if successful, -1 while error occurs
  24. TZ_INT PopObj(T & Obj);
  25. TZ_INT PopFreeObj(T & Obj);
  26. TZ_INT PopTaskObj(T & Obj);
  27. void PushFreeObj(T & Obj);
  28. void PushTaskObj(T & Obj);
  29. TZ_INT GetFreeCnt();
  30. TZ_INT GetTaskCnt();
  31. TZ_INT GetOPError() const;
  32. private:
  33. void SetOPError(const TZ_INT errcode);
  34. private:
  35. Mutex m_mtxFreePool;
  36. std::list<T> m_vFreePool;
  37. Mutex m_mtxTaskPool;
  38. std::list<T> m_vTaskPool;
  39. TZ_INT m_OPErrno;
  40. };
  41. template <typename T>
  42. ObjectPool<T>::ObjectPool():
  43. m_OPErrno(OPSucess)
  44. {
  45. }
  46. template <typename T>
  47. ObjectPool<T>::~ObjectPool()
  48. {
  49. m_vFreePool.clear();
  50. m_vTaskPool.clear();
  51. }
  52. template <typename T>
  53. TZ_INT ObjectPool<T>::GetOPError() const
  54. {
  55. return m_OPErrno;
  56. }
  57. template <typename T>
  58. void ObjectPool<T>::SetOPError(const TZ_INT errcode)
  59. {
  60. m_OPErrno = errcode;
  61. }
  62. template<typename T>
  63. TZ_INT ObjectPool<T>::PopObj(T & Obj)
  64. {
  65. if (this->GetTaskCnt())
  66. {
  67. return this->PopTaskObj(Obj);
  68. }
  69. if (this->GetFreeCnt())
  70. {
  71. return this->PopFreeObj(Obj);
  72. }
  73. this->SetOPError(OPEmptyPool);
  74. return -1;
  75. }
  76. template<typename T>
  77. TZ_INT ObjectPool<T>::PopFreeObj(T & Obj)
  78. {
  79. m_mtxFreePool.Lock();
  80. if(m_vFreePool.empty())
  81. {
  82. m_mtxFreePool.Unlock();
  83. this->SetOPError(OPNoFreeObj);
  84. return -1;
  85. }
  86. Obj = m_vFreePool.front();
  87. m_vFreePool.pop_front();
  88. m_mtxFreePool.Unlock();
  89. this->SetOPError(OPSucess);
  90. return 0;
  91. }
  92. template <typename T>
  93. TZ_INT ObjectPool<T>::PopTaskObj(T & Obj)
  94. {
  95. m_mtxTaskPool.Lock();
  96. if(m_vTaskPool.empty())
  97. {
  98. m_mtxTaskPool.Unlock();
  99. this->SetOPError(OPNoTaskObj);
  100. return -1;
  101. }
  102. Obj = m_vTaskPool.front();
  103. m_vTaskPool.pop_front();
  104. m_mtxTaskPool.Unlock();
  105. this->SetOPError(OPSucess);
  106. return 0;
  107. }
  108. template <typename T>
  109. void ObjectPool<T>::PushFreeObj(T & Obj)
  110. {
  111. m_mtxFreePool.Lock();
  112. m_vFreePool.push_back(Obj);
  113. m_mtxFreePool.Unlock();
  114. }
  115. template <typename T>
  116. void ObjectPool<T>::PushTaskObj(T & Obj)
  117. {
  118. m_mtxTaskPool.Lock();
  119. m_vTaskPool.push_back(Obj);
  120. m_mtxTaskPool.Unlock();
  121. }
  122. template <typename T>
  123. TZ_INT ObjectPool<T>::GetFreeCnt()
  124. {
  125. m_mtxFreePool.Lock();
  126. TZ_INT iTmpCount(m_vFreePool.size());
  127. m_mtxFreePool.Unlock();
  128. return iTmpCount;
  129. }
  130. template <typename T>
  131. TZ_INT ObjectPool<T>::GetTaskCnt()
  132. {
  133. m_mtxTaskPool.Lock();
  134. TZ_INT iTmpCount(m_vTaskPool.size());
  135. m_mtxTaskPool.Unlock();
  136. return iTmpCount;
  137. }
  138. } // namespace tzc
  139. #endif /* ----- #ifndef __OBJECTPOOL_H ----- */