RefCounter.h 889 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef __REFCOUNTER_H
  2. #define __REFCOUNTER_H
  3. #include "Locks.h"
  4. namespace tzc {
  5. class RefCounter {
  6. public:
  7. RefCounter() : m_count(0) {}
  8. ~RefCounter() {}
  9. void Duplicate();
  10. void Release();
  11. bool ReleaseAndTest();
  12. TZ_Uint32 Current();
  13. protected:
  14. RefCounter(const RefCounter &);
  15. RefCounter & operator = (const RefCounter &);
  16. private:
  17. Mutex m_mutex;
  18. TZ_Uint32 m_count;
  19. };
  20. //
  21. // inlines
  22. //
  23. inline void RefCounter::Duplicate()
  24. {
  25. ScopedLock lock(m_mutex);
  26. ++m_count;
  27. }
  28. inline void RefCounter::Release()
  29. {
  30. ScopedLock lock(m_mutex);
  31. if (m_count > 0)
  32. {
  33. --m_count;
  34. }
  35. }
  36. inline bool RefCounter::ReleaseAndTest()
  37. {
  38. ScopedLock lock(m_mutex);
  39. if (m_count > 0)
  40. {
  41. --m_count;
  42. }
  43. return m_count == 0;
  44. }
  45. inline TZ_Uint32 RefCounter::Current()
  46. {
  47. ScopedLock lock(m_mutex);
  48. return m_count;
  49. }
  50. }; // namespace tzc
  51. #endif /* ----- #ifndef __REFCOUNTER_H ----- */