/src/FreeImage/Source/OpenEXR/IlmThread/IlmThreadMutex.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 158 lines · 69 code · 26 blank · 63 comment · 4 complexity · 7b7e3a4ea37a999a6b1cb25d28d8c51b MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2005, 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 "IlmBaseConfig.h"
  67. #if defined _WIN32 || defined _WIN64
  68. #ifdef NOMINMAX
  69. #undef NOMINMAX
  70. #endif
  71. #define NOMINMAX
  72. #include <windows.h>
  73. #elif HAVE_PTHREAD
  74. #include <pthread.h>
  75. #endif
  76. namespace IlmThread {
  77. class Lock;
  78. class Mutex
  79. {
  80. public:
  81. Mutex ();
  82. virtual ~Mutex ();
  83. private:
  84. void lock () const;
  85. void unlock () const;
  86. #if defined _WIN32 || defined _WIN64
  87. mutable CRITICAL_SECTION _mutex;
  88. #elif HAVE_PTHREAD
  89. mutable pthread_mutex_t _mutex;
  90. #endif
  91. void operator = (const Mutex& M); // not implemented
  92. Mutex (const Mutex& M); // not implemented
  93. friend class Lock;
  94. };
  95. class Lock
  96. {
  97. public:
  98. Lock (const Mutex& m, bool autoLock = true):
  99. _mutex (m),
  100. _locked (false)
  101. {
  102. if (autoLock)
  103. {
  104. _mutex.lock();
  105. _locked = true;
  106. }
  107. }
  108. ~Lock ()
  109. {
  110. if (_locked)
  111. _mutex.unlock();
  112. }
  113. void acquire ()
  114. {
  115. _mutex.lock();
  116. _locked = true;
  117. }
  118. void release ()
  119. {
  120. _mutex.unlock();
  121. _locked = false;
  122. }
  123. bool locked ()
  124. {
  125. return _locked;
  126. }
  127. private:
  128. const Mutex & _mutex;
  129. bool _locked;
  130. };
  131. } // namespace IlmThread
  132. #endif