syncprims-cxx11.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // -*- C++ -*-
  2. // Copyright (C) 2013-2015, Vaclav Zeman. All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without modifica-
  5. // tion, are permitted provided that the following conditions are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. //
  10. // 2. Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. //
  14. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  15. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  17. // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  19. // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  20. // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. //! @file
  25. //! This file contains implementations of synchronization
  26. //! primitives using the Win32 API. It does not contain any include
  27. //! guards because it is only a fragment to be included by
  28. //! syncprims.h.
  29. #include <stdexcept>
  30. #include <chrono>
  31. #include <algorithm>
  32. namespace log4cplus { namespace thread { namespace impl {
  33. //
  34. //
  35. //
  36. inline
  37. Mutex::Mutex (log4cplus::thread::Mutex::Type)
  38. { }
  39. inline
  40. Mutex::~Mutex ()
  41. { }
  42. inline
  43. void
  44. Mutex::lock () const
  45. {
  46. mtx.lock ();
  47. }
  48. inline
  49. void
  50. Mutex::unlock () const
  51. {
  52. mtx.unlock ();
  53. }
  54. //
  55. //
  56. //
  57. inline
  58. Semaphore::Semaphore (unsigned max_, unsigned initial)
  59. : max (max_)
  60. , val ((std::min) (max, initial))
  61. { }
  62. inline
  63. Semaphore::~Semaphore ()
  64. { }
  65. inline
  66. void
  67. Semaphore::unlock () const
  68. {
  69. std::lock_guard<std::mutex> guard (mtx);
  70. if (val >= max)
  71. LOG4CPLUS_THROW_RTE ("Semaphore::unlock(): val >= max");
  72. ++val;
  73. cv.notify_all ();
  74. }
  75. inline
  76. void
  77. Semaphore::lock () const
  78. {
  79. std::unique_lock<std::mutex> guard (mtx);
  80. while (val == 0)
  81. cv.wait (guard);
  82. --val;
  83. if (LOG4CPLUS_UNLIKELY(val >= max))
  84. LOG4CPLUS_THROW_RTE ("Semaphore::unlock(): val >= max");
  85. }
  86. //
  87. //
  88. //
  89. inline
  90. FairMutex::FairMutex ()
  91. : sem (1, 1)
  92. { }
  93. inline
  94. FairMutex::~FairMutex ()
  95. { }
  96. inline
  97. void
  98. FairMutex::lock () const
  99. {
  100. sem.lock ();
  101. }
  102. inline
  103. void
  104. FairMutex::unlock () const
  105. {
  106. sem.unlock ();
  107. }
  108. //
  109. //
  110. //
  111. inline
  112. ManualResetEvent::ManualResetEvent (bool sig)
  113. : sigcount (0)
  114. , signaled (sig)
  115. { }
  116. inline
  117. ManualResetEvent::~ManualResetEvent ()
  118. { }
  119. inline
  120. void
  121. ManualResetEvent::signal () const
  122. {
  123. std::unique_lock<std::mutex> guard (mtx);
  124. signaled = true;
  125. sigcount += 1;
  126. cv.notify_all ();
  127. }
  128. inline
  129. void
  130. ManualResetEvent::wait () const
  131. {
  132. std::unique_lock<std::mutex> guard (mtx);
  133. if (! signaled)
  134. {
  135. unsigned prev_count = sigcount;
  136. do
  137. {
  138. cv.wait (guard);
  139. }
  140. while (prev_count == sigcount);
  141. }
  142. }
  143. inline
  144. bool
  145. ManualResetEvent::timed_wait (unsigned long msec) const
  146. {
  147. std::unique_lock<std::mutex> guard (mtx);
  148. if (! signaled)
  149. {
  150. unsigned prev_count = sigcount;
  151. std::chrono::steady_clock::time_point const wait_until_time
  152. = std::chrono::steady_clock::now ()
  153. + std::chrono::milliseconds (msec);
  154. do
  155. {
  156. int ret = static_cast<int>(
  157. cv.wait_until (guard, wait_until_time));
  158. switch (ret)
  159. {
  160. case static_cast<int>(std::cv_status::no_timeout):
  161. break;
  162. case static_cast<int>(std::cv_status::timeout):
  163. return false;
  164. default:
  165. guard.unlock ();
  166. guard.release ();
  167. LOG4CPLUS_THROW_RTE ("ManualResetEvent::timed_wait");
  168. }
  169. }
  170. while (prev_count == sigcount);
  171. }
  172. return true;
  173. }
  174. inline
  175. void
  176. ManualResetEvent::reset () const
  177. {
  178. std::lock_guard<std::mutex> guard (mtx);
  179. signaled = false;
  180. }
  181. //
  182. //
  183. //
  184. #include "log4cplus/thread/impl/syncprims-pmsm.h"
  185. } } } // namespace log4cplus { namespace thread { namespace impl {