1 //===-- Predicate.h ---------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_Predicate_h_
10 #define liblldb_Predicate_h_
15 #include <condition_variable>
18 #include "lldb/Utility/Timeout.h"
19 #include "lldb/lldb-defines.h"
21 //#define DB_PTHREAD_LOG_EVENTS
23 /// Enumerations for broadcasting.
24 namespace lldb_private {
26 enum PredicateBroadcastType {
27 eBroadcastNever, ///< No broadcast will be sent when the value is modified.
28 eBroadcastAlways, ///< Always send a broadcast when the value is modified.
29 eBroadcastOnChange ///< Only broadcast if the value changes when the value is
33 /// \class Predicate Predicate.h "lldb/Utility/Predicate.h"
34 /// A C++ wrapper class for providing threaded access to a value of
37 /// A templatized class that provides multi-threaded access to a value
38 /// of type T. Threads can efficiently wait for bits within T to be set
39 /// or reset, or wait for T to be set to be equal/not equal to a
41 template <class T> class Predicate {
43 /// Default constructor.
45 /// Initializes the mutex, condition and value with their default
47 Predicate() : m_value(), m_mutex(), m_condition() {}
49 /// Construct with initial T value \a initial_value.
51 /// Initializes the mutex and condition with their default
52 /// constructors, and initializes the value with \a initial_value.
54 /// \param[in] initial_value
55 /// The initial value for our T object.
56 Predicate(T initial_value)
57 : m_value(initial_value), m_mutex(), m_condition() {}
61 /// Destroy the condition, mutex, and T objects.
62 ~Predicate() = default;
64 /// Value get accessor.
66 /// Copies the current \a m_value in a thread safe manor and returns
70 /// A copy of the current value.
72 std::lock_guard<std::mutex> guard(m_mutex);
77 /// Value set accessor.
79 /// Set the contained \a m_value to \a new_value in a thread safe
80 /// way and broadcast if needed.
83 /// The new value to set.
85 /// \param[in] broadcast_type
86 /// A value indicating when and if to broadcast. See the
87 /// PredicateBroadcastType enumeration for details.
89 /// \see Predicate::Broadcast()
90 void SetValue(T value, PredicateBroadcastType broadcast_type) {
91 std::lock_guard<std::mutex> guard(m_mutex);
92 #ifdef DB_PTHREAD_LOG_EVENTS
93 printf("%s (value = 0x%8.8x, broadcast_type = %i)\n", __FUNCTION__, value,
96 const T old_value = m_value;
99 Broadcast(old_value, broadcast_type);
102 /// Wait for Cond(m_value) to be true.
104 /// Waits in a thread safe way for Cond(m_value) to be true. If Cond(m_value)
105 /// is already true, this function will return without waiting.
107 /// It is possible for the value to be changed between the time the value is
108 /// set and the time the waiting thread wakes up. If the value no longer
109 /// satisfies the condition when the waiting thread wakes up, it will go back
110 /// into a wait state. It may be necessary for the calling code to use
111 /// additional thread synchronization methods to detect transitory states.
114 /// The condition we want \a m_value satisfy.
116 /// \param[in] timeout
117 /// How long to wait for the condition to hold.
120 /// \li m_value if Cond(m_value) is true.
121 /// \li None otherwise (timeout occurred).
122 template <typename C>
123 llvm::Optional<T> WaitFor(C Cond, const Timeout<std::micro> &timeout) {
124 std::unique_lock<std::mutex> lock(m_mutex);
125 auto RealCond = [&] { return Cond(m_value); };
127 m_condition.wait(lock, RealCond);
130 if (m_condition.wait_for(lock, *timeout, RealCond))
134 /// Wait for \a m_value to be equal to \a value.
136 /// Waits in a thread safe way for \a m_value to be equal to \a
137 /// value. If \a m_value is already equal to \a value, this
138 /// function will return without waiting.
140 /// It is possible for the value to be changed between the time
141 /// the value is set and the time the waiting thread wakes up.
142 /// If the value no longer matches the requested value when the
143 /// waiting thread wakes up, it will go back into a wait state. It
144 /// may be necessary for the calling code to use additional thread
145 /// synchronization methods to detect transitory states.
148 /// The value we want \a m_value to be equal to.
150 /// \param[in] timeout
151 /// How long to wait for the condition to hold.
154 /// \li \b true if the \a m_value is equal to \a value
155 /// \li \b false otherwise (timeout occurred)
156 bool WaitForValueEqualTo(T value,
157 const Timeout<std::micro> &timeout = llvm::None) {
158 return WaitFor([&value](T current) { return value == current; }, timeout) !=
162 /// Wait for \a m_value to not be equal to \a value.
164 /// Waits in a thread safe way for \a m_value to not be equal to \a
165 /// value. If \a m_value is already not equal to \a value, this
166 /// function will return without waiting.
168 /// It is possible for the value to be changed between the time
169 /// the value is set and the time the waiting thread wakes up.
170 /// If the value is equal to the test value when the waiting thread
171 /// wakes up, it will go back into a wait state. It may be
172 /// necessary for the calling code to use additional thread
173 /// synchronization methods to detect transitory states.
176 /// The value we want \a m_value to not be equal to.
178 /// \param[in] timeout
179 /// How long to wait for the condition to hold.
182 /// \li m_value if m_value != value
183 /// \li None otherwise (timeout occurred).
185 WaitForValueNotEqualTo(T value,
186 const Timeout<std::micro> &timeout = llvm::None) {
187 return WaitFor([&value](T current) { return value != current; }, timeout);
191 // pthread condition and mutex variable to control access and allow blocking
192 // between the main thread and the spotlight index thread.
193 T m_value; ///< The templatized value T that we are protecting access to
194 mutable std::mutex m_mutex; ///< The mutex to use when accessing the data
195 std::condition_variable m_condition; ///< The pthread condition variable to
196 /// use for signaling that data available
200 /// Broadcast if needed.
202 /// Check to see if we need to broadcast to our condition variable
203 /// depending on the \a old_value and on the \a broadcast_type.
205 /// If \a broadcast_type is eBroadcastNever, no broadcast will be
208 /// If \a broadcast_type is eBroadcastAlways, the condition variable
209 /// will always be broadcast.
211 /// If \a broadcast_type is eBroadcastOnChange, the condition
212 /// variable be broadcast if the owned value changes.
213 void Broadcast(T old_value, PredicateBroadcastType broadcast_type) {
215 (broadcast_type == eBroadcastAlways) ||
216 ((broadcast_type == eBroadcastOnChange) && old_value != m_value);
217 #ifdef DB_PTHREAD_LOG_EVENTS
218 printf("%s (old_value = 0x%8.8x, broadcast_type = %i) m_value = 0x%8.8x, "
220 __FUNCTION__, old_value, broadcast_type, m_value, broadcast);
223 m_condition.notify_all();
226 DISALLOW_COPY_AND_ASSIGN(Predicate);
229 } // namespace lldb_private
231 #endif // liblldb_Predicate_h_