llrefcount.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file llrefcount.h
  3. * @brief Base class for reference counted objects for use with LLPointer
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LLREFCOUNT_H
  33. #define LLREFCOUNT_H
  34. #include "llatomic.h"
  35. #include "llerror.h"
  36. //-----------------------------------------------------------------------------
  37. // RefCount objects should generally only be accessed by way of LLPointer<>'s.
  38. // See llpointer.h for LLPointer<> definition
  39. //-----------------------------------------------------------------------------
  40. class LLRefCount
  41. {
  42. protected:
  43. LL_INLINE LLRefCount(const LLRefCount&) noexcept
  44. : mRef(0)
  45. {
  46. }
  47. LL_INLINE LLRefCount& operator=(const LLRefCount&) noexcept
  48. {
  49. // Do nothing, since ref count is specific to *this* reference
  50. return *this;
  51. }
  52. virtual ~LLRefCount(); // Use unref()
  53. public:
  54. LL_INLINE LLRefCount() noexcept
  55. : mRef(0)
  56. {
  57. }
  58. LL_INLINE void ref() const noexcept
  59. {
  60. ++mRef;
  61. }
  62. LL_INLINE void unref() const
  63. {
  64. llassert(mRef >= 1);
  65. if (--mRef == 0)
  66. {
  67. // If we hit zero, the caller should be the only smart pointer
  68. // owning the object and we can delete it.
  69. delete this;
  70. }
  71. }
  72. // NOTE: when passing around a const LLRefCount object, this can return
  73. // different results at different types, since mRef is mutable
  74. LL_INLINE S32 getNumRefs() const
  75. {
  76. return mRef;
  77. }
  78. private:
  79. mutable S32 mRef;
  80. };
  81. //-----------------------------------------------------------------------------
  82. // LLThreadSafeRefCount class
  83. //-----------------------------------------------------------------------------
  84. class LLThreadSafeRefCount
  85. {
  86. protected:
  87. virtual ~LLThreadSafeRefCount(); // Use unref()
  88. public:
  89. LL_INLINE LLThreadSafeRefCount() noexcept
  90. : mRef(0)
  91. {
  92. }
  93. // Non-copyable because LLAtomicS32 (std::atomic<S32>) is non-copyable. HB
  94. LLThreadSafeRefCount(const LLThreadSafeRefCount&) noexcept = delete;
  95. LLThreadSafeRefCount& operator=(const LLThreadSafeRefCount&) noexcept = delete;
  96. LL_INLINE void ref() noexcept
  97. {
  98. ++mRef;
  99. }
  100. LL_INLINE void unref()
  101. {
  102. llassert(mRef >= 1);
  103. if (--mRef == 0)
  104. {
  105. // If we hit zero, the caller should be the only smart pointer
  106. // owning the object and we can delete it. It is technically
  107. // possible for a vanilla pointer to mess this up, or another
  108. // thread to jump in, find this object, create another smart
  109. // pointer and end up dangling, but if the code is that bad and not
  110. // thread-safe, it is trouble already.
  111. delete this;
  112. }
  113. }
  114. LL_INLINE S32 getNumRefs() const
  115. {
  116. const S32 current_val = mRef.get();
  117. return current_val;
  118. }
  119. private:
  120. LLAtomicS32 mRef;
  121. };
  122. #endif