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)
9 * \author Andrey Semashev
12 * \brief The header defines classes and macros for once-blocks.
15 #ifndef BOOST_LOG_UTILITY_ONCE_BLOCK_HPP_INCLUDED_
16 #define BOOST_LOG_UTILITY_ONCE_BLOCK_HPP_INCLUDED_
18 #include <boost/log/detail/config.hpp>
19 #include <boost/log/utility/unique_identifier_name.hpp>
20 #include <boost/log/detail/header.hpp>
22 #ifdef BOOST_HAS_PRAGMA_ONCE
26 #ifndef BOOST_LOG_NO_THREADS
30 BOOST_LOG_OPEN_NAMESPACE
33 * \brief A flag to detect if a code block has already been executed.
35 * This structure should be used in conjunction with the \c BOOST_LOG_ONCE_BLOCK_FLAG
36 * macro. Usage example:
39 * once_block_flag flag = BOOST_LOG_ONCE_BLOCK_INIT;
43 * BOOST_LOG_ONCE_BLOCK_FLAG(flag)
45 * puts("Hello, world once!");
50 struct once_block_flag
52 #ifndef BOOST_LOG_DOXYGEN_PASS
53 // Do not use, implementation detail
56 uninitialized = 0, // this must be zero, so that zero-initialized once_block_flag is equivalent to the one initialized with uninitialized
61 #endif // BOOST_LOG_DOXYGEN_PASS
65 * \def BOOST_LOG_ONCE_BLOCK_INIT
67 * The static initializer for \c once_block_flag.
69 #define BOOST_LOG_ONCE_BLOCK_INIT { boost::log::once_block_flag::uninitialized }
73 class once_block_sentry
76 once_block_flag& m_flag;
79 explicit once_block_sentry(once_block_flag& f) BOOST_NOEXCEPT : m_flag(f)
83 ~once_block_sentry() BOOST_NOEXCEPT
85 if (BOOST_UNLIKELY(m_flag.status != once_block_flag::initialized))
89 bool executed() const BOOST_NOEXCEPT
91 return (m_flag.status == once_block_flag::initialized || enter_once_block());
94 BOOST_LOG_API void commit() BOOST_NOEXCEPT;
97 BOOST_LOG_API bool enter_once_block() const BOOST_NOEXCEPT;
98 BOOST_LOG_API void rollback() BOOST_NOEXCEPT;
100 // Non-copyable, non-assignable
101 BOOST_DELETED_FUNCTION(once_block_sentry(once_block_sentry const&))
102 BOOST_DELETED_FUNCTION(once_block_sentry& operator= (once_block_sentry const&))
107 BOOST_LOG_CLOSE_NAMESPACE // namespace log
111 #else // BOOST_LOG_NO_THREADS
115 BOOST_LOG_OPEN_NAMESPACE
117 struct once_block_flag
122 #define BOOST_LOG_ONCE_BLOCK_INIT { false }
126 class once_block_sentry
129 once_block_flag& m_flag;
132 explicit once_block_sentry(once_block_flag& f) BOOST_NOEXCEPT : m_flag(f)
136 bool executed() const BOOST_NOEXCEPT
138 return m_flag.status;
141 void commit() BOOST_NOEXCEPT
143 m_flag.status = true;
146 // Non-copyable, non-assignable
147 BOOST_DELETED_FUNCTION(once_block_sentry(once_block_sentry const&))
148 BOOST_DELETED_FUNCTION(once_block_sentry& operator= (once_block_sentry const&))
153 BOOST_LOG_CLOSE_NAMESPACE // namespace log
157 #endif // BOOST_LOG_NO_THREADS
159 #ifndef BOOST_LOG_DOXYGEN_PASS
161 #define BOOST_LOG_ONCE_BLOCK_FLAG_INTERNAL(flag_var, sentry_var)\
162 for (boost::log::aux::once_block_sentry sentry_var((flag_var));\
163 BOOST_UNLIKELY(!sentry_var.executed()); sentry_var.commit())
165 // NOTE: flag_var deliberately doesn't have an initializer so that it is zero-initialized at the static initialization stage
166 #define BOOST_LOG_ONCE_BLOCK_INTERNAL(flag_var, sentry_var)\
167 static boost::log::once_block_flag flag_var;\
168 BOOST_LOG_ONCE_BLOCK_FLAG_INTERNAL(flag_var, sentry_var)
170 #endif // BOOST_LOG_DOXYGEN_PASS
173 * \def BOOST_LOG_ONCE_BLOCK_FLAG(flag_var)
175 * Begins a code block to be executed only once, with protection against thread concurrency.
176 * User has to provide the flag variable that controls whether the block has already
179 #define BOOST_LOG_ONCE_BLOCK_FLAG(flag_var)\
180 BOOST_LOG_ONCE_BLOCK_FLAG_INTERNAL(\
182 BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_once_block_sentry_))
185 * \def BOOST_LOG_ONCE_BLOCK()
187 * Begins a code block to be executed only once, with protection against thread concurrency.
189 #define BOOST_LOG_ONCE_BLOCK()\
190 BOOST_LOG_ONCE_BLOCK_INTERNAL(\
191 BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_once_block_flag_),\
192 BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_once_block_sentry_))
194 #include <boost/log/detail/footer.hpp>
196 #endif // BOOST_LOG_UTILITY_ONCE_BLOCK_HPP_INCLUDED_