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 | |
13 | #ifndef BOOST_LOG_DETAIL_EVENT_HPP_INCLUDED_ |
14 | #define BOOST_LOG_DETAIL_EVENT_HPP_INCLUDED_ |
15 | |
16 | #include <boost/log/detail/config.hpp> |
17 | |
18 | #ifdef BOOST_HAS_PRAGMA_ONCE |
19 | #pragma once |
20 | #endif |
21 | |
22 | #ifndef BOOST_LOG_NO_THREADS |
23 | |
24 | #include <boost/atomic/capabilities.hpp> |
25 | |
26 | #if BOOST_ATOMIC_HAS_NATIVE_INT32_WAIT_NOTIFY == 2 |
27 | #include <boost/cstdint.hpp> |
28 | #include <boost/atomic/atomic.hpp> |
29 | #define BOOST_LOG_EVENT_USE_ATOMIC |
30 | #elif defined(BOOST_THREAD_PLATFORM_PTHREAD) && defined(_POSIX_SEMAPHORES) && _POSIX_SEMAPHORES > 0 && BOOST_ATOMIC_FLAG_LOCK_FREE == 2 |
31 | #include <semaphore.h> |
32 | #include <boost/cstdint.hpp> |
33 | #include <boost/atomic/atomic_flag.hpp> |
34 | #define BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE |
35 | #elif defined(BOOST_THREAD_PLATFORM_WIN32) |
36 | #include <boost/cstdint.hpp> |
37 | #include <boost/atomic/atomic.hpp> |
38 | #define BOOST_LOG_EVENT_USE_WINAPI |
39 | #else |
40 | #include <boost/thread/mutex.hpp> |
41 | #include <boost/thread/condition_variable.hpp> |
42 | #define BOOST_LOG_EVENT_USE_BOOST_CONDITION_VARIABLE |
43 | #endif |
44 | |
45 | #include <boost/log/detail/header.hpp> |
46 | |
47 | namespace boost { |
48 | |
49 | BOOST_LOG_OPEN_NAMESPACE |
50 | |
51 | namespace aux { |
52 | |
53 | #if defined(BOOST_LOG_EVENT_USE_ATOMIC) |
54 | |
55 | class atomic_based_event |
56 | { |
57 | private: |
58 | boost::atomic< boost::uint32_t > m_state; |
59 | |
60 | public: |
61 | //! Default constructor |
62 | atomic_based_event() : m_state(0u) {} |
63 | |
64 | //! Waits for the object to become signalled |
65 | BOOST_LOG_API void wait(); |
66 | //! Sets the object to a signalled state |
67 | BOOST_LOG_API void set_signalled(); |
68 | |
69 | // Copying prohibited |
70 | BOOST_DELETED_FUNCTION(atomic_based_event(atomic_based_event const&)) |
71 | BOOST_DELETED_FUNCTION(atomic_based_event& operator= (atomic_based_event const&)) |
72 | }; |
73 | |
74 | typedef atomic_based_event event; |
75 | |
76 | #elif defined(BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE) |
77 | |
78 | class sem_based_event |
79 | { |
80 | private: |
81 | boost::atomic_flag m_state; |
82 | sem_t m_semaphore; |
83 | |
84 | public: |
85 | //! Default constructor |
86 | BOOST_LOG_API sem_based_event(); |
87 | //! Destructor |
88 | BOOST_LOG_API ~sem_based_event(); |
89 | |
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 | |
95 | // Copying prohibited |
96 | BOOST_DELETED_FUNCTION(sem_based_event(sem_based_event const&)) |
97 | BOOST_DELETED_FUNCTION(sem_based_event& operator= (sem_based_event const&)) |
98 | }; |
99 | |
100 | typedef sem_based_event event; |
101 | |
102 | #elif defined(BOOST_LOG_EVENT_USE_WINAPI) |
103 | |
104 | class winapi_based_event |
105 | { |
106 | private: |
107 | boost::atomic< boost::uint32_t > m_state; |
108 | void* m_event; |
109 | |
110 | public: |
111 | //! Default constructor |
112 | BOOST_LOG_API winapi_based_event(); |
113 | //! Destructor |
114 | BOOST_LOG_API ~winapi_based_event(); |
115 | |
116 | //! Waits for the object to become signalled |
117 | BOOST_LOG_API void wait(); |
118 | //! Sets the object to a signalled state |
119 | BOOST_LOG_API void set_signalled(); |
120 | |
121 | // Copying prohibited |
122 | BOOST_DELETED_FUNCTION(winapi_based_event(winapi_based_event const&)) |
123 | BOOST_DELETED_FUNCTION(winapi_based_event& operator= (winapi_based_event const&)) |
124 | }; |
125 | |
126 | typedef winapi_based_event event; |
127 | |
128 | #else |
129 | |
130 | class generic_event |
131 | { |
132 | private: |
133 | boost::mutex m_mutex; |
134 | boost::condition_variable m_cond; |
135 | bool m_state; |
136 | |
137 | public: |
138 | //! Default constructor |
139 | BOOST_LOG_API generic_event(); |
140 | //! Destructor |
141 | BOOST_LOG_API ~generic_event(); |
142 | |
143 | //! Waits for the object to become signalled |
144 | BOOST_LOG_API void wait(); |
145 | //! Sets the object to a signalled state |
146 | BOOST_LOG_API void set_signalled(); |
147 | |
148 | // Copying prohibited |
149 | BOOST_DELETED_FUNCTION(generic_event(generic_event const&)) |
150 | BOOST_DELETED_FUNCTION(generic_event& operator= (generic_event const&)) |
151 | }; |
152 | |
153 | typedef generic_event event; |
154 | |
155 | #endif |
156 | |
157 | } // namespace aux |
158 | |
159 | BOOST_LOG_CLOSE_NAMESPACE // namespace log |
160 | |
161 | } // namespace boost |
162 | |
163 | #include <boost/log/detail/footer.hpp> |
164 | |
165 | #endif // BOOST_LOG_NO_THREADS |
166 | |
167 | #endif // BOOST_LOG_DETAIL_EVENT_HPP_INCLUDED_ |
168 | |