You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_CRITICALSECTION_H__
  28. #define TALK_BASE_CRITICALSECTION_H__
  29. #include "talk/base/constructormagic.h"
  30. #ifdef WIN32
  31. #include "talk/base/win32.h"
  32. #endif
  33. #ifdef POSIX
  34. #include <pthread.h>
  35. #endif
  36. #ifdef _DEBUG
  37. #define CS_TRACK_OWNER 1
  38. #endif // _DEBUG
  39. #if CS_TRACK_OWNER
  40. #define TRACK_OWNER(x) x
  41. #else // !CS_TRACK_OWNER
  42. #define TRACK_OWNER(x)
  43. #endif // !CS_TRACK_OWNER
  44. namespace talk_base {
  45. #ifdef WIN32
  46. class CriticalSection {
  47. public:
  48. CriticalSection() {
  49. InitializeCriticalSection(&crit_);
  50. // Windows docs say 0 is not a valid thread id
  51. TRACK_OWNER(thread_ = 0);
  52. }
  53. ~CriticalSection() {
  54. DeleteCriticalSection(&crit_);
  55. }
  56. void Enter() {
  57. EnterCriticalSection(&crit_);
  58. TRACK_OWNER(thread_ = GetCurrentThreadId());
  59. }
  60. bool TryEnter() {
  61. if (TryEnterCriticalSection(&crit_) != FALSE) {
  62. TRACK_OWNER(thread_ = GetCurrentThreadId());
  63. return true;
  64. }
  65. return false;
  66. }
  67. void Leave() {
  68. TRACK_OWNER(thread_ = 0);
  69. LeaveCriticalSection(&crit_);
  70. }
  71. #if CS_TRACK_OWNER
  72. bool CurrentThreadIsOwner() const { return thread_ == GetCurrentThreadId(); }
  73. #endif // CS_TRACK_OWNER
  74. private:
  75. CRITICAL_SECTION crit_;
  76. TRACK_OWNER(DWORD thread_); // The section's owning thread id
  77. };
  78. #endif // WIN32
  79. #ifdef POSIX
  80. class CriticalSection {
  81. public:
  82. CriticalSection() {
  83. pthread_mutexattr_t mutex_attribute;
  84. pthread_mutexattr_init(&mutex_attribute);
  85. pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
  86. pthread_mutex_init(&mutex_, &mutex_attribute);
  87. pthread_mutexattr_destroy(&mutex_attribute);
  88. TRACK_OWNER(thread_ = 0);
  89. }
  90. ~CriticalSection() {
  91. pthread_mutex_destroy(&mutex_);
  92. }
  93. void Enter() {
  94. pthread_mutex_lock(&mutex_);
  95. TRACK_OWNER(thread_ = pthread_self());
  96. }
  97. bool TryEnter() {
  98. if (pthread_mutex_trylock(&mutex_) == 0) {
  99. TRACK_OWNER(thread_ = pthread_self());
  100. return true;
  101. }
  102. return false;
  103. }
  104. void Leave() {
  105. TRACK_OWNER(thread_ = 0);
  106. pthread_mutex_unlock(&mutex_);
  107. }
  108. #if CS_TRACK_OWNER
  109. bool CurrentThreadIsOwner() const { return pthread_equal(thread_, pthread_self()); }
  110. #endif // CS_TRACK_OWNER
  111. private:
  112. pthread_mutex_t mutex_;
  113. TRACK_OWNER(pthread_t thread_);
  114. };
  115. #endif // POSIX
  116. // CritScope, for serializing execution through a scope.
  117. class CritScope {
  118. public:
  119. explicit CritScope(CriticalSection *pcrit) {
  120. pcrit_ = pcrit;
  121. pcrit_->Enter();
  122. }
  123. ~CritScope() {
  124. pcrit_->Leave();
  125. }
  126. private:
  127. CriticalSection *pcrit_;
  128. DISALLOW_COPY_AND_ASSIGN(CritScope);
  129. };
  130. // Tries to lock a critical section on construction via
  131. // CriticalSection::TryEnter, and unlocks on destruction if the
  132. // lock was taken. Never blocks.
  133. //
  134. // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
  135. // subsequent code. Users *must* check locked() to determine if the
  136. // lock was taken. If you're not calling locked(), you're doing it wrong!
  137. class TryCritScope {
  138. public:
  139. explicit TryCritScope(CriticalSection *pcrit) {
  140. pcrit_ = pcrit;
  141. locked_ = pcrit_->TryEnter();
  142. }
  143. ~TryCritScope() {
  144. if (locked_) {
  145. pcrit_->Leave();
  146. }
  147. }
  148. bool locked() const {
  149. return locked_;
  150. }
  151. private:
  152. CriticalSection *pcrit_;
  153. bool locked_;
  154. DISALLOW_COPY_AND_ASSIGN(TryCritScope);
  155. };
  156. // TODO: Replace with platform-specific "atomic" ops.
  157. // Something like: google3/base/atomicops.h TODO: And, move
  158. // it to atomicops.h, which can't be done easily because of complex
  159. // compile rules.
  160. class AtomicOps {
  161. public:
  162. #ifdef WIN32
  163. // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.
  164. static int Increment(int* i) {
  165. return ::InterlockedIncrement(reinterpret_cast<LONG*>(i));
  166. }
  167. static int Decrement(int* i) {
  168. return ::InterlockedDecrement(reinterpret_cast<LONG*>(i));
  169. }
  170. #else
  171. static int Increment(int* i) {
  172. // Could be faster, and less readable:
  173. // static CriticalSection* crit = StaticCrit();
  174. // CritScope scope(crit);
  175. CritScope scope(StaticCrit());
  176. return ++(*i);
  177. }
  178. static int Decrement(int* i) {
  179. // Could be faster, and less readable:
  180. // static CriticalSection* crit = StaticCrit();
  181. // CritScope scope(crit);
  182. CritScope scope(StaticCrit());
  183. return --(*i);
  184. }
  185. private:
  186. static CriticalSection* StaticCrit() {
  187. static CriticalSection* crit = new CriticalSection();
  188. return crit;
  189. }
  190. #endif
  191. };
  192. } // namespace talk_base
  193. #endif // TALK_BASE_CRITICALSECTION_H__