IlmThreadMutex.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2005-2012, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_ILM_THREAD_MUTEX_H
  35. #define INCLUDED_ILM_THREAD_MUTEX_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // class Mutex, class Lock
  39. //
  40. // Class Mutex is a wrapper for a system-dependent mutual exclusion
  41. // mechanism. Actual locking and unlocking of a Mutex object must
  42. // be performed using an instance of a Lock (defined below).
  43. //
  44. // Class lock provides safe locking and unlocking of mutexes even in
  45. // the presence of C++ exceptions. Constructing a Lock object locks
  46. // the mutex; destroying the Lock unlocks the mutex.
  47. //
  48. // Lock objects are not themselves thread-safe. You should never
  49. // share a Lock object among multiple threads.
  50. //
  51. // Typical usage:
  52. //
  53. // Mutex mtx; // Create a Mutex object that is visible
  54. // //to multiple threads
  55. //
  56. // ... // create some threads
  57. //
  58. // // Then, within each thread, construct a critical section like so:
  59. //
  60. // {
  61. // Lock lock (mtx); // Lock constructor locks the mutex
  62. // ... // do some computation on shared data
  63. // } // leaving the block unlocks the mutex
  64. //
  65. //-----------------------------------------------------------------------------
  66. #include "IlmThreadExport.h"
  67. #include "IlmBaseConfig.h"
  68. #include "IlmThreadNamespace.h"
  69. #ifdef ILMBASE_FORCE_CXX03
  70. # if defined _WIN32 || defined _WIN64
  71. # ifdef NOMINMAX
  72. # undef NOMINMAX
  73. # endif
  74. # define NOMINMAX
  75. # include <windows.h>
  76. # elif HAVE_PTHREAD
  77. # include <pthread.h>
  78. # endif
  79. #else
  80. # include <mutex>
  81. #endif
  82. ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
  83. // in c++11, this can just be
  84. //
  85. // using Mutex = std::mutex;
  86. // unfortunately we can't use std::unique_lock as a replacement for Lock since
  87. // they have different API.
  88. //
  89. // if we decide to break the API, we can just
  90. //
  91. // using Lock = std::lock_guard<std::mutex>;
  92. // or
  93. // using Lock = std::unique_lock<std::mutex>;
  94. //
  95. // (or eliminate the type completely and have people use the std library)
  96. #ifdef ILMBASE_FORCE_CXX03
  97. class Lock;
  98. class ILMTHREAD_EXPORT Mutex
  99. {
  100. public:
  101. Mutex ();
  102. virtual ~Mutex ();
  103. private:
  104. void lock () const;
  105. void unlock () const;
  106. #if defined _WIN32 || defined _WIN64
  107. mutable CRITICAL_SECTION _mutex;
  108. #elif HAVE_PTHREAD
  109. mutable pthread_mutex_t _mutex;
  110. #endif
  111. void operator = (const Mutex& M); // not implemented
  112. Mutex (const Mutex& M); // not implemented
  113. friend class Lock;
  114. };
  115. #else
  116. using Mutex = std::mutex;
  117. #endif
  118. class ILMTHREAD_EXPORT Lock
  119. {
  120. public:
  121. Lock (const Mutex& m, bool autoLock = true):
  122. _mutex (const_cast<Mutex &>(m)), _locked (false)
  123. {
  124. if (autoLock)
  125. {
  126. _mutex.lock();
  127. _locked = true;
  128. }
  129. }
  130. ~Lock ()
  131. {
  132. if (_locked)
  133. _mutex.unlock();
  134. }
  135. void acquire ()
  136. {
  137. _mutex.lock();
  138. _locked = true;
  139. }
  140. void release ()
  141. {
  142. _mutex.unlock();
  143. _locked = false;
  144. }
  145. bool locked ()
  146. {
  147. return _locked;
  148. }
  149. private:
  150. Mutex & _mutex;
  151. bool _locked;
  152. };
  153. ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
  154. #endif // INCLUDED_ILM_THREAD_MUTEX_H