mutex.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifdef GOOGLE_PROTOBUF_NO_THREADLOCAL
  32. #include <pthread.h>
  33. #endif
  34. #include <google/protobuf/stubs/macros.h>
  35. // ===================================================================
  36. // emulates google3/base/mutex.h
  37. namespace google {
  38. namespace protobuf {
  39. namespace internal {
  40. // A Mutex is a non-reentrant (aka non-recursive) mutex. At most one thread T
  41. // may hold a mutex at a given time. If T attempts to Lock() the same Mutex
  42. // while holding it, T will deadlock.
  43. class LIBPROTOBUF_EXPORT Mutex {
  44. public:
  45. // Create a Mutex that is not held by anybody.
  46. Mutex();
  47. // Destructor
  48. ~Mutex();
  49. // Block if necessary until this Mutex is free, then acquire it exclusively.
  50. void Lock();
  51. // Release this Mutex. Caller must hold it exclusively.
  52. void Unlock();
  53. // Crash if this Mutex is not held exclusively by this thread.
  54. // May fail to crash when it should; will never crash when it should not.
  55. void AssertHeld();
  56. private:
  57. struct Internal;
  58. Internal* mInternal;
  59. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Mutex);
  60. };
  61. // Undefine the macros to workaround the conflicts with Google internal
  62. // MutexLock implementation.
  63. // TODO(liujisi): Remove the undef once internal macros are removed.
  64. #undef MutexLock
  65. #undef ReaderMutexLock
  66. #undef WriterMutexLock
  67. #undef MutexLockMaybe
  68. // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
  69. class LIBPROTOBUF_EXPORT MutexLock {
  70. public:
  71. explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
  72. ~MutexLock() { this->mu_->Unlock(); }
  73. private:
  74. Mutex *const mu_;
  75. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
  76. };
  77. // TODO(kenton): Implement these? Hard to implement portably.
  78. typedef MutexLock ReaderMutexLock;
  79. typedef MutexLock WriterMutexLock;
  80. // MutexLockMaybe is like MutexLock, but is a no-op when mu is NULL.
  81. class LIBPROTOBUF_EXPORT MutexLockMaybe {
  82. public:
  83. explicit MutexLockMaybe(Mutex *mu) :
  84. mu_(mu) { if (this->mu_ != NULL) { this->mu_->Lock(); } }
  85. ~MutexLockMaybe() { if (this->mu_ != NULL) { this->mu_->Unlock(); } }
  86. private:
  87. Mutex *const mu_;
  88. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
  89. };
  90. #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
  91. template<typename T>
  92. class ThreadLocalStorage {
  93. public:
  94. ThreadLocalStorage() {
  95. pthread_key_create(&key_, &ThreadLocalStorage::Delete);
  96. }
  97. ~ThreadLocalStorage() {
  98. pthread_key_delete(key_);
  99. }
  100. T* Get() {
  101. T* result = static_cast<T*>(pthread_getspecific(key_));
  102. if (result == NULL) {
  103. result = new T();
  104. pthread_setspecific(key_, result);
  105. }
  106. return result;
  107. }
  108. private:
  109. static void Delete(void* value) {
  110. delete static_cast<T*>(value);
  111. }
  112. pthread_key_t key_;
  113. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage);
  114. };
  115. #endif
  116. } // namespace internal
  117. // We made these internal so that they would show up as such in the docs,
  118. // but we don't want to stick "internal::" in front of them everywhere.
  119. using internal::Mutex;
  120. using internal::MutexLock;
  121. using internal::ReaderMutexLock;
  122. using internal::WriterMutexLock;
  123. using internal::MutexLockMaybe;
  124. } // namespace protobuf
  125. } // namespace google
  126. #endif // GOOGLE_PROTOBUF_STUBS_MUTEX_H_