/src/FreeImage/Source/OpenEXR/IlmThread/IlmThreadSemaphorePosixCompat.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 155 lines · 84 code · 31 blank · 40 comment · 12 complexity · cde5c631bab05b16d71a7e85a7ac4c11 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. //-----------------------------------------------------------------------------
  35. //
  36. // class Semaphore -- implementation for for platforms that do
  37. // support Posix threads but do not support Posix semaphores,
  38. // for example, OS X
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include "IlmBaseConfig.h"
  42. #if HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES
  43. #include "IlmThreadSemaphore.h"
  44. #include "Iex.h"
  45. #include <assert.h>
  46. namespace IlmThread {
  47. Semaphore::Semaphore (unsigned int value)
  48. {
  49. if (int error = ::pthread_mutex_init (&_semaphore.mutex, 0))
  50. Iex::throwErrnoExc ("Cannot initialize mutex (%T).", error);
  51. if (int error = ::pthread_cond_init (&_semaphore.nonZero, 0))
  52. Iex::throwErrnoExc ("Cannot initialize condition variable (%T).",
  53. error);
  54. _semaphore.count = value;
  55. _semaphore.numWaiting = 0;
  56. }
  57. Semaphore::~Semaphore ()
  58. {
  59. int error = ::pthread_cond_destroy (&_semaphore.nonZero);
  60. assert (error == 0);
  61. error = ::pthread_mutex_destroy (&_semaphore.mutex);
  62. assert (error == 0);
  63. }
  64. void
  65. Semaphore::wait ()
  66. {
  67. ::pthread_mutex_lock (&_semaphore.mutex);
  68. _semaphore.numWaiting++;
  69. while (_semaphore.count == 0)
  70. {
  71. if (int error = ::pthread_cond_wait (&_semaphore.nonZero,
  72. &_semaphore.mutex))
  73. {
  74. ::pthread_mutex_unlock (&_semaphore.mutex);
  75. Iex::throwErrnoExc ("Cannot wait on condition variable (%T).",
  76. error);
  77. }
  78. }
  79. _semaphore.numWaiting--;
  80. _semaphore.count--;
  81. ::pthread_mutex_unlock (&_semaphore.mutex);
  82. }
  83. bool
  84. Semaphore::tryWait ()
  85. {
  86. ::pthread_mutex_lock (&_semaphore.mutex);
  87. if (_semaphore.count == 0)
  88. {
  89. ::pthread_mutex_unlock (&_semaphore.mutex);
  90. return false;
  91. }
  92. else
  93. {
  94. _semaphore.count--;
  95. ::pthread_mutex_unlock (&_semaphore.mutex);
  96. return true;
  97. }
  98. }
  99. void
  100. Semaphore::post ()
  101. {
  102. ::pthread_mutex_lock (&_semaphore.mutex);
  103. if (_semaphore.numWaiting > 0)
  104. {
  105. if (int error = ::pthread_cond_signal (&_semaphore.nonZero))
  106. {
  107. ::pthread_mutex_unlock (&_semaphore.mutex);
  108. Iex::throwErrnoExc ("Cannot signal condition variable (%T).",
  109. error);
  110. }
  111. }
  112. _semaphore.count++;
  113. ::pthread_mutex_unlock (&_semaphore.mutex);
  114. }
  115. int
  116. Semaphore::value () const
  117. {
  118. ::pthread_mutex_lock (&_semaphore.mutex);
  119. int value = _semaphore.count;
  120. ::pthread_mutex_unlock (&_semaphore.mutex);
  121. return value;
  122. }
  123. } // namespace IlmThread
  124. #endif