IlmThreadSemaphoreWin32.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. //-----------------------------------------------------------------------------
  35. //
  36. // class Semaphore -- implementation for Windows
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include "IlmThreadSemaphore.h"
  40. #include "Iex.h"
  41. #include <string>
  42. #include <assert.h>
  43. #include <iostream>
  44. ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_ENTER
  45. using namespace IEX_NAMESPACE;
  46. namespace {
  47. std::string
  48. errorString ()
  49. {
  50. LPSTR messageBuffer;
  51. DWORD bufferLength;
  52. std::string message;
  53. //
  54. // Call FormatMessage() to allow for message
  55. // text to be acquired from the system.
  56. //
  57. if (bufferLength = FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
  58. FORMAT_MESSAGE_IGNORE_INSERTS |
  59. FORMAT_MESSAGE_FROM_SYSTEM,
  60. 0,
  61. GetLastError (),
  62. MAKELANGID (LANG_NEUTRAL,
  63. SUBLANG_DEFAULT),
  64. (LPSTR) &messageBuffer,
  65. 0,
  66. NULL))
  67. {
  68. message = messageBuffer;
  69. LocalFree (messageBuffer);
  70. }
  71. return message;
  72. }
  73. } // namespace
  74. Semaphore::Semaphore (unsigned int value)
  75. {
  76. if ((_semaphore = ::CreateSemaphore (0, value, 0x7fffffff, 0)) == 0)
  77. {
  78. THROW (LogicExc, "Could not create semaphore "
  79. "(" << errorString() << ").");
  80. }
  81. }
  82. Semaphore::~Semaphore()
  83. {
  84. bool ok = ::CloseHandle (_semaphore) != FALSE;
  85. assert (ok);
  86. }
  87. void
  88. Semaphore::wait()
  89. {
  90. if (::WaitForSingleObject (_semaphore, INFINITE) != WAIT_OBJECT_0)
  91. {
  92. THROW (LogicExc, "Could not wait on semaphore "
  93. "(" << errorString() << ").");
  94. }
  95. }
  96. bool
  97. Semaphore::tryWait()
  98. {
  99. return ::WaitForSingleObject (_semaphore, 0) == WAIT_OBJECT_0;
  100. }
  101. void
  102. Semaphore::post()
  103. {
  104. if (!::ReleaseSemaphore (_semaphore, 1, 0))
  105. {
  106. THROW (LogicExc, "Could not post on semaphore "
  107. "(" << errorString() << ").");
  108. }
  109. }
  110. int
  111. Semaphore::value() const
  112. {
  113. LONG v = -1;
  114. if (!::ReleaseSemaphore (_semaphore, 0, &v) || v < 0)
  115. {
  116. THROW (LogicExc, "Could not get value of semaphore "
  117. "(" << errorString () << ").");
  118. }
  119. return v;
  120. }
  121. ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT