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