mutex.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <google/protobuf/stubs/macros.h>
  33. // ===================================================================
  34. // emulates google3/base/mutex.h
  35. namespace google {
  36. namespace protobuf {
  37. namespace internal {
  38. #define GOOGLE_PROTOBUF_LINKER_INITIALIZED
  39. // Mutex is a natural type to wrap. As both google and other organization have
  40. // specialized mutexes. gRPC also provides an injection mechanism for custom
  41. // mutexes.
  42. class LIBPROTOBUF_EXPORT WrappedMutex {
  43. public:
  44. WrappedMutex() = default;
  45. void Lock() { mu_.lock(); }
  46. void Unlock() { mu_.unlock(); }
  47. // Crash if this Mutex is not held exclusively by this thread.
  48. // May fail to crash when it should; will never crash when it should not.
  49. void AssertHeld() const {}
  50. private:
  51. std::mutex mu_;
  52. };
  53. using Mutex = WrappedMutex;
  54. // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
  55. class LIBPROTOBUF_EXPORT MutexLock {
  56. public:
  57. explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
  58. ~MutexLock() { this->mu_->Unlock(); }
  59. private:
  60. Mutex *const mu_;
  61. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
  62. };
  63. // TODO(kenton): Implement these? Hard to implement portably.
  64. typedef MutexLock ReaderMutexLock;
  65. typedef MutexLock WriterMutexLock;
  66. // MutexLockMaybe is like MutexLock, but is a no-op when mu is NULL.
  67. class LIBPROTOBUF_EXPORT MutexLockMaybe {
  68. public:
  69. explicit MutexLockMaybe(Mutex *mu) :
  70. mu_(mu) { if (this->mu_ != NULL) { this->mu_->Lock(); } }
  71. ~MutexLockMaybe() { if (this->mu_ != NULL) { this->mu_->Unlock(); } }
  72. private:
  73. Mutex *const mu_;
  74. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
  75. };
  76. #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
  77. template<typename T>
  78. class ThreadLocalStorage {
  79. public:
  80. ThreadLocalStorage() {
  81. pthread_key_create(&key_, &ThreadLocalStorage::Delete);
  82. }
  83. ~ThreadLocalStorage() {
  84. pthread_key_delete(key_);
  85. }
  86. T* Get() {
  87. T* result = static_cast<T*>(pthread_getspecific(key_));
  88. if (result == NULL) {
  89. result = new T();
  90. pthread_setspecific(key_, result);
  91. }
  92. return result;
  93. }
  94. private:
  95. static void Delete(void* value) {
  96. delete static_cast<T*>(value);
  97. }
  98. pthread_key_t key_;
  99. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage);
  100. };
  101. #endif
  102. } // namespace internal
  103. // We made these internal so that they would show up as such in the docs,
  104. // but we don't want to stick "internal::" in front of them everywhere.
  105. using internal::Mutex;
  106. using internal::MutexLock;
  107. using internal::ReaderMutexLock;
  108. using internal::WriterMutexLock;
  109. using internal::MutexLockMaybe;
  110. } // namespace protobuf
  111. } // namespace google
  112. #endif // GOOGLE_PROTOBUF_STUBS_MUTEX_H_