PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/llcommon/llrefcount.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 164 lines | 121 code | 17 blank | 26 comment | 16 complexity | 62ece9e1b388b3d6b8b1c0500c16fcd5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llrefcount.cpp
  3. * @brief Base class for reference counted objects for use with LLPointer
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llrefcount.h"
  28. #include "llerror.h"
  29. #if LL_REF_COUNT_DEBUG
  30. #include "llthread.h"
  31. #include "llapr.h"
  32. #endif
  33. LLRefCount::LLRefCount(const LLRefCount& other)
  34. : mRef(0)
  35. {
  36. #if LL_REF_COUNT_DEBUG
  37. if(gAPRPoolp)
  38. {
  39. mMutexp = new LLMutex(gAPRPoolp) ;
  40. }
  41. else
  42. {
  43. mMutexp = NULL ;
  44. }
  45. mCrashAtUnlock = FALSE ;
  46. #endif
  47. }
  48. LLRefCount& LLRefCount::operator=(const LLRefCount&)
  49. {
  50. // do nothing, since ref count is specific to *this* reference
  51. return *this;
  52. }
  53. LLRefCount::LLRefCount() :
  54. mRef(0)
  55. {
  56. #if LL_REF_COUNT_DEBUG
  57. if(gAPRPoolp)
  58. {
  59. mMutexp = new LLMutex(gAPRPoolp) ;
  60. }
  61. else
  62. {
  63. mMutexp = NULL ;
  64. }
  65. mCrashAtUnlock = FALSE ;
  66. #endif
  67. }
  68. LLRefCount::~LLRefCount()
  69. {
  70. if (mRef != 0)
  71. {
  72. llerrs << "deleting non-zero reference" << llendl;
  73. }
  74. #if LL_REF_COUNT_DEBUG
  75. if(gAPRPoolp)
  76. {
  77. delete mMutexp ;
  78. }
  79. #endif
  80. }
  81. #if LL_REF_COUNT_DEBUG
  82. void LLRefCount::ref() const
  83. {
  84. if(mMutexp)
  85. {
  86. if(mMutexp->isLocked())
  87. {
  88. mCrashAtUnlock = TRUE ;
  89. llerrs << "the mutex is locked by the thread: " << mLockedThreadID
  90. << " Current thread: " << LLThread::currentID() << llendl ;
  91. }
  92. mMutexp->lock() ;
  93. mLockedThreadID = LLThread::currentID() ;
  94. mRef++;
  95. if(mCrashAtUnlock)
  96. {
  97. while(1); //crash here.
  98. }
  99. mMutexp->unlock() ;
  100. }
  101. else
  102. {
  103. mRef++;
  104. }
  105. }
  106. S32 LLRefCount::unref() const
  107. {
  108. if(mMutexp)
  109. {
  110. if(mMutexp->isLocked())
  111. {
  112. mCrashAtUnlock = TRUE ;
  113. llerrs << "the mutex is locked by the thread: " << mLockedThreadID
  114. << " Current thread: " << LLThread::currentID() << llendl ;
  115. }
  116. mMutexp->lock() ;
  117. mLockedThreadID = LLThread::currentID() ;
  118. llassert(mRef >= 1);
  119. if (0 == --mRef)
  120. {
  121. if(mCrashAtUnlock)
  122. {
  123. while(1); //crash here.
  124. }
  125. mMutexp->unlock() ;
  126. delete this;
  127. return 0;
  128. }
  129. if(mCrashAtUnlock)
  130. {
  131. while(1); //crash here.
  132. }
  133. mMutexp->unlock() ;
  134. return mRef;
  135. }
  136. else
  137. {
  138. llassert(mRef >= 1);
  139. if (0 == --mRef)
  140. {
  141. delete this;
  142. return 0;
  143. }
  144. return mRef;
  145. }
  146. }
  147. #endif