event.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  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. #if defined(BOOST_THREAD_PLATFORM_PTHREAD)
  20. # include <boost/atomic/capabilities.hpp>
  21. # if (defined(linux) || defined(__linux) || defined(__linux__)) && BOOST_ATOMIC_INT_LOCK_FREE == 2
  22. # include <boost/atomic/atomic.hpp>
  23. # define BOOST_LOG_EVENT_USE_FUTEX
  24. # elif defined(_POSIX_SEMAPHORES) && (_POSIX_SEMAPHORES + 0) > 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. # endif
  30. #elif defined(BOOST_THREAD_PLATFORM_WIN32)
  31. # include <boost/cstdint.hpp>
  32. # define BOOST_LOG_EVENT_USE_WINAPI
  33. #endif
  34. #if !defined(BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE) && !defined(BOOST_LOG_EVENT_USE_WINAPI)
  35. # include <boost/thread/mutex.hpp>
  36. # include <boost/thread/condition_variable.hpp>
  37. # define BOOST_LOG_EVENT_USE_BOOST_CONDITION
  38. #endif
  39. #include <boost/log/detail/header.hpp>
  40. namespace boost {
  41. BOOST_LOG_OPEN_NAMESPACE
  42. namespace aux {
  43. #if defined(BOOST_LOG_EVENT_USE_FUTEX)
  44. class futex_based_event
  45. {
  46. private:
  47. boost::atomic< int > m_state;
  48. public:
  49. //! Default constructor
  50. BOOST_LOG_API futex_based_event();
  51. //! Destructor
  52. BOOST_LOG_API ~futex_based_event();
  53. //! Waits for the object to become signalled
  54. BOOST_LOG_API void wait();
  55. //! Sets the object to a signalled state
  56. BOOST_LOG_API void set_signalled();
  57. // Copying prohibited
  58. BOOST_DELETED_FUNCTION(futex_based_event(futex_based_event const&))
  59. BOOST_DELETED_FUNCTION(futex_based_event& operator= (futex_based_event const&))
  60. };
  61. typedef futex_based_event event;
  62. #elif defined(BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE)
  63. class sem_based_event
  64. {
  65. private:
  66. boost::atomic_flag m_state;
  67. sem_t m_semaphore;
  68. public:
  69. //! Default constructor
  70. BOOST_LOG_API sem_based_event();
  71. //! Destructor
  72. BOOST_LOG_API ~sem_based_event();
  73. //! Waits for the object to become signalled
  74. BOOST_LOG_API void wait();
  75. //! Sets the object to a signalled state
  76. BOOST_LOG_API void set_signalled();
  77. // Copying prohibited
  78. BOOST_DELETED_FUNCTION(sem_based_event(sem_based_event const&))
  79. BOOST_DELETED_FUNCTION(sem_based_event& operator= (sem_based_event const&))
  80. };
  81. typedef sem_based_event event;
  82. #elif defined(BOOST_LOG_EVENT_USE_WINAPI)
  83. class winapi_based_event
  84. {
  85. private:
  86. boost::uint32_t m_state;
  87. void* m_event;
  88. public:
  89. //! Default constructor
  90. BOOST_LOG_API winapi_based_event();
  91. //! Destructor
  92. BOOST_LOG_API ~winapi_based_event();
  93. //! Waits for the object to become signalled
  94. BOOST_LOG_API void wait();
  95. //! Sets the object to a signalled state
  96. BOOST_LOG_API void set_signalled();
  97. // Copying prohibited
  98. BOOST_DELETED_FUNCTION(winapi_based_event(winapi_based_event const&))
  99. BOOST_DELETED_FUNCTION(winapi_based_event& operator= (winapi_based_event const&))
  100. };
  101. typedef winapi_based_event event;
  102. #else
  103. class generic_event
  104. {
  105. private:
  106. boost::mutex m_mutex;
  107. boost::condition_variable m_cond;
  108. bool m_state;
  109. public:
  110. //! Default constructor
  111. BOOST_LOG_API generic_event();
  112. //! Destructor
  113. BOOST_LOG_API ~generic_event();
  114. //! Waits for the object to become signalled
  115. BOOST_LOG_API void wait();
  116. //! Sets the object to a signalled state
  117. BOOST_LOG_API void set_signalled();
  118. // Copying prohibited
  119. BOOST_DELETED_FUNCTION(generic_event(generic_event const&))
  120. BOOST_DELETED_FUNCTION(generic_event& operator= (generic_event const&))
  121. };
  122. typedef generic_event event;
  123. #endif
  124. } // namespace aux
  125. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  126. } // namespace boost
  127. #include <boost/log/detail/footer.hpp>
  128. #endif // BOOST_LOG_NO_THREADS
  129. #endif // BOOST_LOG_DETAIL_EVENT_HPP_INCLUDED_