once_block.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 once_block.hpp
  9. * \author Andrey Semashev
  10. * \date 23.06.2010
  11. *
  12. * \brief The header defines classes and macros for once-blocks.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_ONCE_BLOCK_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_ONCE_BLOCK_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #include <boost/log/utility/unique_identifier_name.hpp>
  18. #include <boost/log/detail/header.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. #ifndef BOOST_LOG_NO_THREADS
  23. namespace boost {
  24. BOOST_LOG_OPEN_NAMESPACE
  25. /*!
  26. * \brief A flag to detect if a code block has already been executed.
  27. *
  28. * This structure should be used in conjunction with the \c BOOST_LOG_ONCE_BLOCK_FLAG
  29. * macro. Usage example:
  30. *
  31. * <code>
  32. * once_block_flag flag = BOOST_LOG_ONCE_BLOCK_INIT;
  33. *
  34. * void foo()
  35. * {
  36. * BOOST_LOG_ONCE_BLOCK_FLAG(flag)
  37. * {
  38. * puts("Hello, world once!");
  39. * }
  40. * }
  41. * </code>
  42. */
  43. struct once_block_flag
  44. {
  45. #ifndef BOOST_LOG_DOXYGEN_PASS
  46. // Do not use, implementation detail
  47. enum
  48. {
  49. uninitialized = 0, // this must be zero, so that zero-initialized once_block_flag is equivalent to the one initialized with uninitialized
  50. being_initialized,
  51. initialized
  52. };
  53. unsigned char status;
  54. #endif // BOOST_LOG_DOXYGEN_PASS
  55. };
  56. /*!
  57. * \def BOOST_LOG_ONCE_BLOCK_INIT
  58. *
  59. * The static initializer for \c once_block_flag.
  60. */
  61. #define BOOST_LOG_ONCE_BLOCK_INIT { boost::log::once_block_flag::uninitialized }
  62. namespace aux {
  63. class once_block_sentry
  64. {
  65. private:
  66. once_block_flag& m_flag;
  67. public:
  68. explicit once_block_sentry(once_block_flag& f) BOOST_NOEXCEPT : m_flag(f)
  69. {
  70. }
  71. ~once_block_sentry() BOOST_NOEXCEPT
  72. {
  73. if (BOOST_UNLIKELY(m_flag.status != once_block_flag::initialized))
  74. rollback();
  75. }
  76. bool executed() const BOOST_NOEXCEPT
  77. {
  78. return (m_flag.status == once_block_flag::initialized || enter_once_block());
  79. }
  80. BOOST_LOG_API void commit() BOOST_NOEXCEPT;
  81. private:
  82. BOOST_LOG_API bool enter_once_block() const BOOST_NOEXCEPT;
  83. BOOST_LOG_API void rollback() BOOST_NOEXCEPT;
  84. // Non-copyable, non-assignable
  85. BOOST_DELETED_FUNCTION(once_block_sentry(once_block_sentry const&))
  86. BOOST_DELETED_FUNCTION(once_block_sentry& operator= (once_block_sentry const&))
  87. };
  88. } // namespace aux
  89. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  90. } // namespace boost
  91. #else // BOOST_LOG_NO_THREADS
  92. namespace boost {
  93. BOOST_LOG_OPEN_NAMESPACE
  94. struct once_block_flag
  95. {
  96. bool status;
  97. };
  98. #define BOOST_LOG_ONCE_BLOCK_INIT { false }
  99. namespace aux {
  100. class once_block_sentry
  101. {
  102. private:
  103. once_block_flag& m_flag;
  104. public:
  105. explicit once_block_sentry(once_block_flag& f) BOOST_NOEXCEPT : m_flag(f)
  106. {
  107. }
  108. bool executed() const BOOST_NOEXCEPT
  109. {
  110. return m_flag.status;
  111. }
  112. void commit() BOOST_NOEXCEPT
  113. {
  114. m_flag.status = true;
  115. }
  116. // Non-copyable, non-assignable
  117. BOOST_DELETED_FUNCTION(once_block_sentry(once_block_sentry const&))
  118. BOOST_DELETED_FUNCTION(once_block_sentry& operator= (once_block_sentry const&))
  119. };
  120. } // namespace aux
  121. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  122. } // namespace boost
  123. #endif // BOOST_LOG_NO_THREADS
  124. #ifndef BOOST_LOG_DOXYGEN_PASS
  125. #define BOOST_LOG_ONCE_BLOCK_FLAG_INTERNAL(flag_var, sentry_var)\
  126. for (boost::log::aux::once_block_sentry sentry_var((flag_var));\
  127. BOOST_UNLIKELY(!sentry_var.executed()); sentry_var.commit())
  128. // NOTE: flag_var deliberately doesn't have an initializer so that it is zero-initialized at the static initialization stage
  129. #define BOOST_LOG_ONCE_BLOCK_INTERNAL(flag_var, sentry_var)\
  130. static boost::log::once_block_flag flag_var;\
  131. BOOST_LOG_ONCE_BLOCK_FLAG_INTERNAL(flag_var, sentry_var)
  132. #endif // BOOST_LOG_DOXYGEN_PASS
  133. /*!
  134. * \def BOOST_LOG_ONCE_BLOCK_FLAG(flag_var)
  135. *
  136. * Begins a code block to be executed only once, with protection against thread concurrency.
  137. * User has to provide the flag variable that controls whether the block has already
  138. * been executed.
  139. */
  140. #define BOOST_LOG_ONCE_BLOCK_FLAG(flag_var)\
  141. BOOST_LOG_ONCE_BLOCK_FLAG_INTERNAL(\
  142. flag_var,\
  143. BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_once_block_sentry_))
  144. /*!
  145. * \def BOOST_LOG_ONCE_BLOCK()
  146. *
  147. * Begins a code block to be executed only once, with protection against thread concurrency.
  148. */
  149. #define BOOST_LOG_ONCE_BLOCK()\
  150. BOOST_LOG_ONCE_BLOCK_INTERNAL(\
  151. BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_once_block_flag_),\
  152. BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_once_block_sentry_))
  153. #include <boost/log/detail/footer.hpp>
  154. #endif // BOOST_LOG_UTILITY_ONCE_BLOCK_HPP_INCLUDED_