syncprims-pub-impl.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // -*- C++ -*-
  2. // Copyright (C) 2010-2017, Vaclav Haisman. 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. #ifndef LOG4CPLUS_THREAD_SYNCPRIMS_PUB_IMPL_H
  25. #define LOG4CPLUS_THREAD_SYNCPRIMS_PUB_IMPL_H
  26. #include <log4cplus/config.hxx>
  27. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  28. #pragma once
  29. #endif
  30. #include <algorithm>
  31. #if (defined (LOG4CPLUS_INLINES_ARE_EXPORTED) \
  32. && defined (LOG4CPLUS_BUILD_DLL)) \
  33. || defined (LOG4CPLUS_ENABLE_SYNCPRIMS_PUB_IMPL)
  34. #include <log4cplus/thread/syncprims.h>
  35. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  36. # include <log4cplus/thread/impl/syncprims-impl.h>
  37. #endif
  38. #define LOG4CPLUS_THROW_RTE(msg) \
  39. do { log4cplus::thread::impl::syncprims_throw_exception (msg, __FILE__, \
  40. __LINE__); } while (0)
  41. namespace log4cplus { namespace thread {
  42. namespace impl
  43. {
  44. LOG4CPLUS_EXPORT void LOG4CPLUS_ATTRIBUTE_NORETURN
  45. syncprims_throw_exception(char const * const msg,
  46. char const * const file, int line);
  47. }
  48. //
  49. //
  50. //
  51. LOG4CPLUS_INLINE_EXPORT
  52. SimpleMutex::SimpleMutex ()
  53. LOG4CPLUS_THREADED (: mtx ())
  54. { }
  55. LOG4CPLUS_INLINE_EXPORT
  56. SimpleMutex::~SimpleMutex ()
  57. { }
  58. LOG4CPLUS_INLINE_EXPORT
  59. void
  60. SimpleMutex::lock () const
  61. {
  62. LOG4CPLUS_THREADED (mtx.lock ());
  63. }
  64. LOG4CPLUS_INLINE_EXPORT
  65. bool
  66. SimpleMutex::try_lock () const
  67. {
  68. #if defined (LOG4CPLUS_SINGLE_THREADED)
  69. return true;
  70. #else
  71. return mtx.try_lock ();
  72. #endif
  73. }
  74. LOG4CPLUS_INLINE_EXPORT
  75. void
  76. SimpleMutex::unlock () const
  77. {
  78. LOG4CPLUS_THREADED (mtx.unlock ());
  79. }
  80. //
  81. //
  82. //
  83. LOG4CPLUS_INLINE_EXPORT
  84. Mutex::Mutex ()
  85. LOG4CPLUS_THREADED (: mtx ())
  86. { }
  87. LOG4CPLUS_INLINE_EXPORT
  88. Mutex::~Mutex ()
  89. { }
  90. LOG4CPLUS_INLINE_EXPORT
  91. void
  92. Mutex::lock () const
  93. {
  94. LOG4CPLUS_THREADED (mtx.lock ());
  95. }
  96. LOG4CPLUS_INLINE_EXPORT
  97. void
  98. Mutex::unlock () const
  99. {
  100. LOG4CPLUS_THREADED (mtx.unlock ());
  101. }
  102. //
  103. //
  104. //
  105. LOG4CPLUS_INLINE_EXPORT
  106. Semaphore::Semaphore (unsigned LOG4CPLUS_THREADED (max_),
  107. unsigned LOG4CPLUS_THREADED (initial))
  108. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  109. : maximum(max_)
  110. , val ((std::min) (maximum, initial))
  111. #endif
  112. { }
  113. LOG4CPLUS_INLINE_EXPORT
  114. Semaphore::~Semaphore ()
  115. { }
  116. LOG4CPLUS_INLINE_EXPORT
  117. void
  118. Semaphore::unlock () const
  119. {
  120. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  121. std::lock_guard<std::mutex> guard (mtx);
  122. if (val >= maximum)
  123. LOG4CPLUS_THROW_RTE ("Semaphore::unlock(): val >= max");
  124. ++val;
  125. cv.notify_all ();
  126. #endif
  127. }
  128. LOG4CPLUS_INLINE_EXPORT
  129. void
  130. Semaphore::lock () const
  131. {
  132. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  133. std::unique_lock<std::mutex> guard (mtx);
  134. if (LOG4CPLUS_UNLIKELY(val > maximum))
  135. LOG4CPLUS_THROW_RTE ("Semaphore::unlock(): val > max");
  136. while (val == 0)
  137. cv.wait (guard);
  138. --val;
  139. if (LOG4CPLUS_UNLIKELY(val >= maximum))
  140. LOG4CPLUS_THROW_RTE ("Semaphore::unlock(): val >= max");
  141. #endif
  142. }
  143. //
  144. //
  145. //
  146. LOG4CPLUS_INLINE_EXPORT
  147. ManualResetEvent::ManualResetEvent (bool LOG4CPLUS_THREADED (sig))
  148. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  149. : signaled (sig)
  150. , sigcount (0)
  151. #endif
  152. { }
  153. LOG4CPLUS_INLINE_EXPORT
  154. ManualResetEvent::~ManualResetEvent ()
  155. { }
  156. LOG4CPLUS_INLINE_EXPORT
  157. void
  158. ManualResetEvent::signal () const
  159. {
  160. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  161. std::unique_lock<std::mutex> guard (mtx);
  162. signaled = true;
  163. sigcount += 1;
  164. cv.notify_all ();
  165. #endif
  166. }
  167. LOG4CPLUS_INLINE_EXPORT
  168. void
  169. ManualResetEvent::wait () const
  170. {
  171. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  172. std::unique_lock<std::mutex> guard (mtx);
  173. if (! signaled)
  174. {
  175. unsigned prev_count = sigcount;
  176. do
  177. {
  178. cv.wait (guard);
  179. }
  180. while (prev_count == sigcount);
  181. }
  182. #endif
  183. }
  184. LOG4CPLUS_INLINE_EXPORT
  185. bool
  186. ManualResetEvent::timed_wait (unsigned long LOG4CPLUS_THREADED (msec)) const
  187. {
  188. #if defined (LOG4CPLUS_SINGLE_THREADED)
  189. return true;
  190. #else
  191. std::unique_lock<std::mutex> guard (mtx);
  192. if (! signaled)
  193. {
  194. unsigned prev_count = sigcount;
  195. std::chrono::steady_clock::time_point const wait_until_time
  196. = std::chrono::steady_clock::now ()
  197. + std::chrono::milliseconds (msec);
  198. do
  199. {
  200. int ret = static_cast<int>(
  201. cv.wait_until (guard, wait_until_time));
  202. switch (ret)
  203. {
  204. case static_cast<int>(std::cv_status::no_timeout):
  205. break;
  206. case static_cast<int>(std::cv_status::timeout):
  207. return false;
  208. default:
  209. guard.unlock ();
  210. guard.release ();
  211. LOG4CPLUS_THROW_RTE ("ManualResetEvent::timed_wait");
  212. }
  213. }
  214. while (prev_count == sigcount);
  215. }
  216. return true;
  217. #endif
  218. }
  219. LOG4CPLUS_INLINE_EXPORT
  220. void
  221. ManualResetEvent::reset () const
  222. {
  223. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  224. std::lock_guard<std::mutex> guard (mtx);
  225. signaled = false;
  226. #endif
  227. }
  228. //
  229. //
  230. //
  231. LOG4CPLUS_INLINE_EXPORT
  232. SharedMutexImplBase::~SharedMutexImplBase ()
  233. { }
  234. //
  235. //
  236. //
  237. LOG4CPLUS_INLINE_EXPORT
  238. SharedMutex::SharedMutex ()
  239. : sm (LOG4CPLUS_THREADED (new impl::SharedMutex))
  240. { }
  241. LOG4CPLUS_INLINE_EXPORT
  242. SharedMutex::~SharedMutex ()
  243. {
  244. LOG4CPLUS_THREADED (delete static_cast<impl::SharedMutex *>(sm));
  245. }
  246. LOG4CPLUS_INLINE_EXPORT
  247. void
  248. SharedMutex::rdlock () const
  249. {
  250. LOG4CPLUS_THREADED (static_cast<impl::SharedMutex *>(sm)->rdlock ());
  251. }
  252. LOG4CPLUS_INLINE_EXPORT
  253. void
  254. SharedMutex::wrlock () const
  255. {
  256. LOG4CPLUS_THREADED (static_cast<impl::SharedMutex *>(sm)->wrlock ());
  257. }
  258. LOG4CPLUS_INLINE_EXPORT
  259. void
  260. SharedMutex::rdunlock () const
  261. {
  262. LOG4CPLUS_THREADED (static_cast<impl::SharedMutex *>(sm)->rdunlock ());
  263. }
  264. LOG4CPLUS_INLINE_EXPORT
  265. void
  266. SharedMutex::wrunlock () const
  267. {
  268. LOG4CPLUS_THREADED (static_cast<impl::SharedMutex *>(sm)->wrunlock ());
  269. }
  270. } } // namespace log4cplus { namespace thread {
  271. #endif // LOG4CPLUS_ENABLE_SYNCPRIMS_PUB_IMPL
  272. #endif // LOG4CPLUS_THREAD_SYNCPRIMS_PUB_IMPL_H