event.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2021.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file detail/event.hpp
  9. * \author Andrey Semashev
  10. * \date 24.07.2011
  11. */
  12. #ifndef BOOST_LOG_DETAIL_EVENT_HPP_INCLUDED_
  13. #define BOOST_LOG_DETAIL_EVENT_HPP_INCLUDED_
  14. #include <boost/log/detail/config.hpp>
  15. #ifdef BOOST_HAS_PRAGMA_ONCE
  16. #pragma once
  17. #endif
  18. #ifndef BOOST_LOG_NO_THREADS
  19. #include <boost/atomic/capabilities.hpp>
  20. #if BOOST_ATOMIC_HAS_NATIVE_INT32_WAIT_NOTIFY == 2
  21. #include <boost/cstdint.hpp>
  22. #include <boost/atomic/atomic.hpp>
  23. #define BOOST_LOG_EVENT_USE_ATOMIC
  24. #elif defined(BOOST_THREAD_PLATFORM_PTHREAD) && defined(_POSIX_SEMAPHORES) && _POSIX_SEMAPHORES > 0 && BOOST_ATOMIC_FLAG_LOCK_FREE == 2
  25. #include <semaphore.h>
  26. #include <boost/cstdint.hpp>
  27. #include <boost/atomic/atomic_flag.hpp>
  28. #define BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE
  29. #elif defined(BOOST_THREAD_PLATFORM_WIN32)
  30. #include <boost/cstdint.hpp>
  31. #include <boost/atomic/atomic.hpp>
  32. #define BOOST_LOG_EVENT_USE_WINAPI
  33. #else
  34. #include <boost/thread/mutex.hpp>
  35. #include <boost/thread/condition_variable.hpp>
  36. #define BOOST_LOG_EVENT_USE_BOOST_CONDITION_VARIABLE
  37. #endif
  38. #include <boost/log/detail/header.hpp>
  39. namespace boost {
  40. BOOST_LOG_OPEN_NAMESPACE
  41. namespace aux {
  42. #if defined(BOOST_LOG_EVENT_USE_ATOMIC)
  43. class atomic_based_event
  44. {
  45. private:
  46. boost::atomic< boost::uint32_t > m_state;
  47. public:
  48. //! Default constructor
  49. atomic_based_event() : m_state(0u) {}
  50. //! Waits for the object to become signalled
  51. BOOST_LOG_API void wait();
  52. //! Sets the object to a signalled state
  53. BOOST_LOG_API void set_signalled();
  54. // Copying prohibited
  55. BOOST_DELETED_FUNCTION(atomic_based_event(atomic_based_event const&))
  56. BOOST_DELETED_FUNCTION(atomic_based_event& operator= (atomic_based_event const&))
  57. };
  58. typedef atomic_based_event event;
  59. #elif defined(BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE)
  60. class sem_based_event
  61. {
  62. private:
  63. boost::atomic_flag m_state;
  64. sem_t m_semaphore;
  65. public:
  66. //! Default constructor
  67. BOOST_LOG_API sem_based_event();
  68. //! Destructor
  69. BOOST_LOG_API ~sem_based_event();
  70. //! Waits for the object to become signalled
  71. BOOST_LOG_API void wait();
  72. //! Sets the object to a signalled state
  73. BOOST_LOG_API void set_signalled();
  74. // Copying prohibited
  75. BOOST_DELETED_FUNCTION(sem_based_event(sem_based_event const&))
  76. BOOST_DELETED_FUNCTION(sem_based_event& operator= (sem_based_event const&))
  77. };
  78. typedef sem_based_event event;
  79. #elif defined(BOOST_LOG_EVENT_USE_WINAPI)
  80. class winapi_based_event
  81. {
  82. private:
  83. boost::atomic< boost::uint32_t > m_state;
  84. void* m_event;
  85. public:
  86. //! Default constructor
  87. BOOST_LOG_API winapi_based_event();
  88. //! Destructor
  89. BOOST_LOG_API ~winapi_based_event();
  90. //! Waits for the object to become signalled
  91. BOOST_LOG_API void wait();
  92. //! Sets the object to a signalled state
  93. BOOST_LOG_API void set_signalled();
  94. // Copying prohibited
  95. BOOST_DELETED_FUNCTION(winapi_based_event(winapi_based_event const&))
  96. BOOST_DELETED_FUNCTION(winapi_based_event& operator= (winapi_based_event const&))
  97. };
  98. typedef winapi_based_event event;
  99. #else
  100. class generic_event
  101. {
  102. private:
  103. boost::mutex m_mutex;
  104. boost::condition_variable m_cond;
  105. bool m_state;
  106. public:
  107. //! Default constructor
  108. BOOST_LOG_API generic_event();
  109. //! Destructor
  110. BOOST_LOG_API ~generic_event();
  111. //! Waits for the object to become signalled
  112. BOOST_LOG_API void wait();
  113. //! Sets the object to a signalled state
  114. BOOST_LOG_API void set_signalled();
  115. // Copying prohibited
  116. BOOST_DELETED_FUNCTION(generic_event(generic_event const&))
  117. BOOST_DELETED_FUNCTION(generic_event& operator= (generic_event const&))
  118. };
  119. typedef generic_event event;
  120. #endif
  121. } // namespace aux
  122. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  123. } // namespace boost
  124. #include <boost/log/detail/footer.hpp>
  125. #endif // BOOST_LOG_NO_THREADS
  126. #endif // BOOST_LOG_DETAIL_EVENT_HPP_INCLUDED_