诸暨麻将添加redis
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

187 řádky
6.2 KiB

  1. // Copyright (c) 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #ifndef GOOGLE_PROTOBUF_STUBS_MUTEX_H_
  30. #define GOOGLE_PROTOBUF_STUBS_MUTEX_H_
  31. #include <mutex>
  32. #ifdef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  33. #include <windows.h>
  34. // GetMessage conflicts with GeneratedMessageReflection::GetMessage().
  35. #ifdef GetMessage
  36. #undef GetMessage
  37. #endif
  38. #endif
  39. #include <google/protobuf/stubs/macros.h>
  40. // Define thread-safety annotations for use below, if we are building with
  41. // Clang.
  42. #if defined(__clang__) && !defined(SWIG)
  43. #define GOOGLE_PROTOBUF_ACQUIRE(...) \
  44. __attribute__((acquire_capability(__VA_ARGS__)))
  45. #define GOOGLE_PROTOBUF_RELEASE(...) \
  46. __attribute__((release_capability(__VA_ARGS__)))
  47. #define GOOGLE_PROTOBUF_CAPABILITY(x) __attribute__((capability(x)))
  48. #else
  49. #define GOOGLE_PROTOBUF_ACQUIRE(...)
  50. #define GOOGLE_PROTOBUF_RELEASE(...)
  51. #define GOOGLE_PROTOBUF_CAPABILITY(x)
  52. #endif
  53. #include <google/protobuf/port_def.inc>
  54. // ===================================================================
  55. // emulates google3/base/mutex.h
  56. namespace google {
  57. namespace protobuf {
  58. namespace internal {
  59. #define GOOGLE_PROTOBUF_LINKER_INITIALIZED
  60. #ifdef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  61. // This class is a lightweight replacement for std::mutex on Windows platforms.
  62. // std::mutex does not work on Windows XP SP2 with the latest VC++ libraries,
  63. // because it utilizes the Concurrency Runtime that is only supported on Windows
  64. // XP SP3 and above.
  65. class PROTOBUF_EXPORT CriticalSectionLock {
  66. public:
  67. CriticalSectionLock() { InitializeCriticalSection(&critical_section_); }
  68. ~CriticalSectionLock() { DeleteCriticalSection(&critical_section_); }
  69. void lock() { EnterCriticalSection(&critical_section_); }
  70. void unlock() { LeaveCriticalSection(&critical_section_); }
  71. private:
  72. CRITICAL_SECTION critical_section_;
  73. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CriticalSectionLock);
  74. };
  75. #endif
  76. // Mutex is a natural type to wrap. As both google and other organization have
  77. // specialized mutexes. gRPC also provides an injection mechanism for custom
  78. // mutexes.
  79. class GOOGLE_PROTOBUF_CAPABILITY("mutex") PROTOBUF_EXPORT WrappedMutex {
  80. public:
  81. WrappedMutex() = default;
  82. void Lock() GOOGLE_PROTOBUF_ACQUIRE() { mu_.lock(); }
  83. void Unlock() GOOGLE_PROTOBUF_RELEASE() { mu_.unlock(); }
  84. // Crash if this Mutex is not held exclusively by this thread.
  85. // May fail to crash when it should; will never crash when it should not.
  86. void AssertHeld() const {}
  87. private:
  88. #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  89. std::mutex mu_;
  90. #else // ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  91. CriticalSectionLock mu_;
  92. #endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  93. };
  94. using Mutex = WrappedMutex;
  95. // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
  96. class PROTOBUF_EXPORT MutexLock {
  97. public:
  98. explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
  99. ~MutexLock() { this->mu_->Unlock(); }
  100. private:
  101. Mutex *const mu_;
  102. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
  103. };
  104. // TODO(kenton): Implement these? Hard to implement portably.
  105. typedef MutexLock ReaderMutexLock;
  106. typedef MutexLock WriterMutexLock;
  107. // MutexLockMaybe is like MutexLock, but is a no-op when mu is nullptr.
  108. class PROTOBUF_EXPORT MutexLockMaybe {
  109. public:
  110. explicit MutexLockMaybe(Mutex *mu) :
  111. mu_(mu) { if (this->mu_ != nullptr) { this->mu_->Lock(); } }
  112. ~MutexLockMaybe() { if (this->mu_ != nullptr) { this->mu_->Unlock(); } }
  113. private:
  114. Mutex *const mu_;
  115. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
  116. };
  117. #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
  118. template<typename T>
  119. class ThreadLocalStorage {
  120. public:
  121. ThreadLocalStorage() {
  122. pthread_key_create(&key_, &ThreadLocalStorage::Delete);
  123. }
  124. ~ThreadLocalStorage() {
  125. pthread_key_delete(key_);
  126. }
  127. T* Get() {
  128. T* result = static_cast<T*>(pthread_getspecific(key_));
  129. if (result == nullptr) {
  130. result = new T();
  131. pthread_setspecific(key_, result);
  132. }
  133. return result;
  134. }
  135. private:
  136. static void Delete(void* value) {
  137. delete static_cast<T*>(value);
  138. }
  139. pthread_key_t key_;
  140. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage);
  141. };
  142. #endif
  143. } // namespace internal
  144. // We made these internal so that they would show up as such in the docs,
  145. // but we don't want to stick "internal::" in front of them everywhere.
  146. using internal::Mutex;
  147. using internal::MutexLock;
  148. using internal::ReaderMutexLock;
  149. using internal::WriterMutexLock;
  150. using internal::MutexLockMaybe;
  151. } // namespace protobuf
  152. } // namespace google
  153. #undef GOOGLE_PROTOBUF_ACQUIRE
  154. #undef GOOGLE_PROTOBUF_RELEASE
  155. #include <google/protobuf/port_undef.inc>
  156. #endif // GOOGLE_PROTOBUF_STUBS_MUTEX_H_