This revision was automatically updated to reflect the committed changes.
Closed by commit rLLDB341089: Move Predicate.h from Host to Utility (authored 
by teemperor, committed by ).
Herald added a subscriber: jfb.

Changed prior to commit:
  https://reviews.llvm.org/D50384?vs=159649&id=163371#toc

Repository:
  rLLDB LLDB

https://reviews.llvm.org/D50384

Files:
  include/lldb/Core/Event.h
  include/lldb/Core/IOHandler.h
  include/lldb/Host/Editline.h
  include/lldb/Host/Predicate.h
  include/lldb/Host/Socket.h
  include/lldb/Host/posix/ConnectionFileDescriptorPosix.h
  include/lldb/Utility/Predicate.h
  include/lldb/module.modulemap
  source/Core/IOHandler.cpp
  source/Host/common/Host.cpp
  source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.h
  source/Plugins/Process/Windows/Common/DebuggerThread.cpp
  source/Plugins/Process/Windows/Common/DebuggerThread.h
  source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
  unittests/Core/BroadcasterTest.cpp
  unittests/Host/CMakeLists.txt
  unittests/Host/PredicateTest.cpp
  unittests/Utility/CMakeLists.txt
  unittests/Utility/PredicateTest.cpp

Index: unittests/Host/CMakeLists.txt
===================================================================
--- unittests/Host/CMakeLists.txt
+++ unittests/Host/CMakeLists.txt
@@ -3,7 +3,6 @@
   HostInfoTest.cpp
   HostTest.cpp
   MainLoopTest.cpp
-  PredicateTest.cpp
   SocketAddressTest.cpp
   SocketTest.cpp
   SymbolsTest.cpp
Index: unittests/Utility/PredicateTest.cpp
===================================================================
--- unittests/Utility/PredicateTest.cpp
+++ unittests/Utility/PredicateTest.cpp
@@ -0,0 +1,34 @@
+//===-- PredicateTest.cpp ---------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Utility/Predicate.h"
+#include "gtest/gtest.h"
+#include <thread>
+
+using namespace lldb_private;
+
+TEST(Predicate, WaitForValueEqualTo) {
+  Predicate<int> P(0);
+  EXPECT_TRUE(P.WaitForValueEqualTo(0));
+  EXPECT_FALSE(P.WaitForValueEqualTo(1, std::chrono::milliseconds(10)));
+
+  std::thread Setter([&P] {
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
+    P.SetValue(1, eBroadcastAlways);
+  });
+  EXPECT_TRUE(P.WaitForValueEqualTo(1));
+  Setter.join();
+}
+
+TEST(Predicate, WaitForValueNotEqualTo) {
+  Predicate<int> P(0);
+  EXPECT_EQ(0, P.WaitForValueNotEqualTo(1));
+  EXPECT_EQ(llvm::None,
+            P.WaitForValueNotEqualTo(0, std::chrono::milliseconds(10)));
+}
Index: unittests/Utility/CMakeLists.txt
===================================================================
--- unittests/Utility/CMakeLists.txt
+++ unittests/Utility/CMakeLists.txt
@@ -12,6 +12,7 @@
   JSONTest.cpp
   LogTest.cpp
   NameMatchesTest.cpp
+  PredicateTest.cpp
   RegisterValueTest.cpp
   ScalarTest.cpp
   StateTest.cpp
Index: unittests/Core/BroadcasterTest.cpp
===================================================================
--- unittests/Core/BroadcasterTest.cpp
+++ unittests/Core/BroadcasterTest.cpp
@@ -12,7 +12,7 @@
 #include "lldb/Core/Broadcaster.h"
 #include "lldb/Core/Event.h"
 #include "lldb/Core/Listener.h"
-#include "lldb/Host/Predicate.h"
+#include "lldb/Utility/Predicate.h"
 
 #include <thread>
 
Index: source/Host/common/Host.cpp
===================================================================
--- source/Host/common/Host.cpp
+++ source/Host/common/Host.cpp
@@ -52,7 +52,6 @@
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Host/HostProcess.h"
 #include "lldb/Host/MonitoringProcessLauncher.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Host/ProcessLauncher.h"
 #include "lldb/Host/ThreadLauncher.h"
 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
@@ -62,6 +61,7 @@
 #include "lldb/Utility/DataBufferLLVM.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
+#include "lldb/Utility/Predicate.h"
 #include "lldb/Utility/Status.h"
 #include "lldb/lldb-private-forward.h"
 #include "llvm/ADT/SmallString.h"
Index: source/Core/IOHandler.cpp
===================================================================
--- source/Core/IOHandler.cpp
+++ source/Core/IOHandler.cpp
@@ -26,7 +26,7 @@
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/StreamFile.h"
 #include "lldb/Host/File.h"            // for File
-#include "lldb/Host/Predicate.h"       // for Predicate, ::eBroad...
+#include "lldb/Utility/Predicate.h"    // for Predicate, ::eBroad...
 #include "lldb/Utility/Status.h"       // for Status
 #include "lldb/Utility/StreamString.h" // for StreamString
 #include "lldb/Utility/StringList.h"   // for StringList
Index: source/Plugins/Process/Windows/Common/DebuggerThread.h
===================================================================
--- source/Plugins/Process/Windows/Common/DebuggerThread.h
+++ source/Plugins/Process/Windows/Common/DebuggerThread.h
@@ -16,8 +16,8 @@
 #include "ForwardDecl.h"
 #include "lldb/Host/HostProcess.h"
 #include "lldb/Host/HostThread.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Host/windows/windows.h"
+#include "lldb/Utility/Predicate.h"
 
 namespace lldb_private {
 
Index: source/Plugins/Process/Windows/Common/DebuggerThread.cpp
===================================================================
--- source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -12,15 +12,15 @@
 #include "IDebugDelegate.h"
 
 #include "lldb/Core/ModuleSpec.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Host/ThreadLauncher.h"
 #include "lldb/Host/windows/HostProcessWindows.h"
 #include "lldb/Host/windows/HostThreadWindows.h"
 #include "lldb/Host/windows/ProcessLauncherWindows.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/ProcessLaunchInfo.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
+#include "lldb/Utility/Predicate.h"
 #include "lldb/Utility/Status.h"
 
 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
Index: source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.h
===================================================================
--- source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.h
+++ source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.h
@@ -21,7 +21,7 @@
 #include "lldb/Core/Communication.h"
 #include "lldb/Core/Listener.h"
 #include "lldb/Core/StreamBuffer.h"
-#include "lldb/Host/Predicate.h"
+#include "lldb/Utility/Predicate.h"
 #include "lldb/lldb-private.h"
 
 class CommunicationKDP : public lldb_private::Communication {
Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
===================================================================
--- source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
+++ source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
@@ -23,8 +23,8 @@
 #include "lldb/Core/Communication.h"
 #include "lldb/Core/Listener.h"
 #include "lldb/Host/HostThread.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Utility/Args.h"
+#include "lldb/Utility/Predicate.h"
 #include "lldb/lldb-public.h"
 
 #include "lldb/Utility/StringExtractorGDBRemote.h"
Index: include/lldb/Host/posix/ConnectionFileDescriptorPosix.h
===================================================================
--- include/lldb/Host/posix/ConnectionFileDescriptorPosix.h
+++ include/lldb/Host/posix/ConnectionFileDescriptorPosix.h
@@ -20,9 +20,9 @@
 // Other libraries and framework includes
 // Project includes
 #include "lldb/Host/Pipe.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Utility/Connection.h"
 #include "lldb/Utility/IOObject.h"
+#include "lldb/Utility/Predicate.h"
 
 namespace lldb_private {
 
Index: include/lldb/Host/Socket.h
===================================================================
--- include/lldb/Host/Socket.h
+++ include/lldb/Host/Socket.h
@@ -15,9 +15,9 @@
 
 #include "lldb/lldb-private.h"
 
-#include "lldb/Host/Predicate.h"
 #include "lldb/Host/SocketAddress.h"
 #include "lldb/Utility/IOObject.h"
+#include "lldb/Utility/Predicate.h"
 #include "lldb/Utility/Status.h"
 
 #ifdef _WIN32
Index: include/lldb/Host/Editline.h
===================================================================
--- include/lldb/Host/Editline.h
+++ include/lldb/Host/Editline.h
@@ -54,8 +54,8 @@
 #include <vector>
 
 #include "lldb/Host/ConnectionFileDescriptor.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Utility/FileSpec.h"
+#include "lldb/Utility/Predicate.h"
 
 namespace lldb_private {
 namespace line_editor {
Index: include/lldb/module.modulemap
===================================================================
--- include/lldb/module.modulemap
+++ include/lldb/module.modulemap
@@ -38,7 +38,6 @@
   module PipeBase { header "Host/PipeBase.h" export * }
   module Pipe { header "Host/Pipe.h" export * }
   module PosixApi { header "Host/PosixApi.h" export * }
-  module Predicate { header "Host/Predicate.h" export * }
   module ProcessLauncher { header "Host/ProcessLauncher.h" export * }
   module ProcessRunLock { header "Host/ProcessRunLock.h" export * }
   module PseudoTerminal { header "Host/PseudoTerminal.h" export * }
Index: include/lldb/Utility/Predicate.h
===================================================================
--- include/lldb/Utility/Predicate.h
+++ include/lldb/Utility/Predicate.h
@@ -0,0 +1,260 @@
+//===-- Predicate.h ---------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_Predicate_h_
+#define liblldb_Predicate_h_
+
+// C Includes
+#include <stdint.h>
+#include <time.h>
+
+// C++ Includes
+#include <condition_variable>
+#include <mutex>
+
+// Other libraries and framework includes
+// Project includes
+#include "lldb/Utility/Timeout.h"
+#include "lldb/lldb-defines.h"
+
+//#define DB_PTHREAD_LOG_EVENTS
+
+//----------------------------------------------------------------------
+/// Enumerations for broadcasting.
+//----------------------------------------------------------------------
+namespace lldb_private {
+
+typedef enum {
+  eBroadcastNever,   ///< No broadcast will be sent when the value is modified.
+  eBroadcastAlways,  ///< Always send a broadcast when the value is modified.
+  eBroadcastOnChange ///< Only broadcast if the value changes when the value is
+                     /// modified.
+} PredicateBroadcastType;
+
+//----------------------------------------------------------------------
+/// @class Predicate Predicate.h "lldb/Utility/Predicate.h"
+/// A C++ wrapper class for providing threaded access to a value of
+/// type T.
+///
+/// A templatized class that provides multi-threaded access to a value
+/// of type T. Threads can efficiently wait for bits within T to be set
+/// or reset, or wait for T to be set to be equal/not equal to a
+/// specified values.
+//----------------------------------------------------------------------
+template <class T> class Predicate {
+public:
+  //------------------------------------------------------------------
+  /// Default constructor.
+  ///
+  /// Initializes the mutex, condition and value with their default
+  /// constructors.
+  //------------------------------------------------------------------
+  Predicate() : m_value(), m_mutex(), m_condition() {}
+
+  //------------------------------------------------------------------
+  /// Construct with initial T value \a initial_value.
+  ///
+  /// Initializes the mutex and condition with their default
+  /// constructors, and initializes the value with \a initial_value.
+  ///
+  /// @param[in] initial_value
+  ///     The initial value for our T object.
+  //------------------------------------------------------------------
+  Predicate(T initial_value)
+      : m_value(initial_value), m_mutex(), m_condition() {}
+
+  //------------------------------------------------------------------
+  /// Destructor.
+  ///
+  /// Destroy the condition, mutex, and T objects.
+  //------------------------------------------------------------------
+  ~Predicate() = default;
+
+  //------------------------------------------------------------------
+  /// Value get accessor.
+  ///
+  /// Copies the current \a m_value in a thread safe manor and returns
+  /// the copied value.
+  ///
+  /// @return
+  ///     A copy of the current value.
+  //------------------------------------------------------------------
+  T GetValue() const {
+    std::lock_guard<std::mutex> guard(m_mutex);
+    T value = m_value;
+    return value;
+  }
+
+  //------------------------------------------------------------------
+  /// Value set accessor.
+  ///
+  /// Set the contained \a m_value to \a new_value in a thread safe
+  /// way and broadcast if needed.
+  ///
+  /// @param[in] value
+  ///     The new value to set.
+  ///
+  /// @param[in] broadcast_type
+  ///     A value indicating when and if to broadcast. See the
+  ///     PredicateBroadcastType enumeration for details.
+  ///
+  /// @see Predicate::Broadcast()
+  //------------------------------------------------------------------
+  void SetValue(T value, PredicateBroadcastType broadcast_type) {
+    std::lock_guard<std::mutex> guard(m_mutex);
+#ifdef DB_PTHREAD_LOG_EVENTS
+    printf("%s (value = 0x%8.8x, broadcast_type = %i)\n", __FUNCTION__, value,
+           broadcast_type);
+#endif
+    const T old_value = m_value;
+    m_value = value;
+
+    Broadcast(old_value, broadcast_type);
+  }
+
+  //------------------------------------------------------------------
+  /// Wait for Cond(m_value) to be true.
+  ///
+  /// Waits in a thread safe way for Cond(m_value) to be true. If Cond(m_value)
+  /// is already true, this function will return without waiting.
+  ///
+  /// It is possible for the value to be changed between the time the value is
+  /// set and the time the waiting thread wakes up. If the value no longer
+  /// satisfies the condition when the waiting thread wakes up, it will go back
+  /// into a wait state. It may be necessary for the calling code to use
+  /// additional thread synchronization methods to detect transitory states.
+  ///
+  /// @param[in] Cond
+  ///     The condition we want \a m_value satisfy.
+  ///
+  /// @param[in] timeout
+  ///     How long to wait for the condition to hold.
+  ///
+  /// @return
+  ///     @li m_value if Cond(m_value) is true.
+  ///     @li None otherwise (timeout occurred).
+  //------------------------------------------------------------------
+  template <typename C>
+  llvm::Optional<T> WaitFor(C Cond, const Timeout<std::micro> &timeout) {
+    std::unique_lock<std::mutex> lock(m_mutex);
+    auto RealCond = [&] { return Cond(m_value); };
+    if (!timeout) {
+      m_condition.wait(lock, RealCond);
+      return m_value;
+    }
+    if (m_condition.wait_for(lock, *timeout, RealCond))
+      return m_value;
+    return llvm::None;
+  }
+  //------------------------------------------------------------------
+  /// Wait for \a m_value to be equal to \a value.
+  ///
+  /// Waits in a thread safe way for \a m_value to be equal to \a
+  /// value. If \a m_value is already equal to \a value, this
+  /// function will return without waiting.
+  ///
+  /// It is possible for the value to be changed between the time
+  /// the value is set and the time the waiting thread wakes up.
+  /// If the value no longer matches the requested value when the
+  /// waiting thread wakes up, it will go back into a wait state.  It
+  /// may be necessary for the calling code to use additional thread
+  /// synchronization methods to detect transitory states.
+  ///
+  /// @param[in] value
+  ///     The value we want \a m_value to be equal to.
+  ///
+  /// @param[in] timeout
+  ///     How long to wait for the condition to hold.
+  ///
+  /// @return
+  ///     @li \b true if the \a m_value is equal to \a value
+  ///     @li \b false otherwise (timeout occurred)
+  //------------------------------------------------------------------
+  bool WaitForValueEqualTo(T value,
+                           const Timeout<std::micro> &timeout = llvm::None) {
+    return WaitFor([&value](T current) { return value == current; }, timeout) !=
+           llvm::None;
+  }
+
+  //------------------------------------------------------------------
+  /// Wait for \a m_value to not be equal to \a value.
+  ///
+  /// Waits in a thread safe way for \a m_value to not be equal to \a
+  /// value. If \a m_value is already not equal to \a value, this
+  /// function will return without waiting.
+  ///
+  /// It is possible for the value to be changed between the time
+  /// the value is set and the time the waiting thread wakes up.
+  /// If the value is equal to the test value when the waiting thread
+  /// wakes up, it will go back into a wait state.  It may be
+  /// necessary for the calling code to use additional thread
+  /// synchronization methods to detect transitory states.
+  ///
+  /// @param[in] value
+  ///     The value we want \a m_value to not be equal to.
+  ///
+  /// @param[in] timeout
+  ///     How long to wait for the condition to hold.
+  ///
+  /// @return
+  ///     @li m_value if m_value != value
+  ///     @li None otherwise (timeout occurred).
+  //------------------------------------------------------------------
+  llvm::Optional<T>
+  WaitForValueNotEqualTo(T value,
+                         const Timeout<std::micro> &timeout = llvm::None) {
+    return WaitFor([&value](T current) { return value != current; }, timeout);
+  }
+
+protected:
+  //----------------------------------------------------------------------
+  // pthread condition and mutex variable to control access and allow blocking
+  // between the main thread and the spotlight index thread.
+  //----------------------------------------------------------------------
+  T m_value; ///< The templatized value T that we are protecting access to
+  mutable std::mutex m_mutex; ///< The mutex to use when accessing the data
+  std::condition_variable m_condition; ///< The pthread condition variable to
+                                       /// use for signaling that data available
+                                       /// or changed.
+
+private:
+  //------------------------------------------------------------------
+  /// Broadcast if needed.
+  ///
+  /// Check to see if we need to broadcast to our condition variable
+  /// depending on the \a old_value and on the \a broadcast_type.
+  ///
+  /// If \a broadcast_type is eBroadcastNever, no broadcast will be
+  /// sent.
+  ///
+  /// If \a broadcast_type is eBroadcastAlways, the condition variable
+  /// will always be broadcast.
+  ///
+  /// If \a broadcast_type is eBroadcastOnChange, the condition
+  /// variable be broadcast if the owned value changes.
+  //------------------------------------------------------------------
+  void Broadcast(T old_value, PredicateBroadcastType broadcast_type) {
+    bool broadcast =
+        (broadcast_type == eBroadcastAlways) ||
+        ((broadcast_type == eBroadcastOnChange) && old_value != m_value);
+#ifdef DB_PTHREAD_LOG_EVENTS
+    printf("%s (old_value = 0x%8.8x, broadcast_type = %i) m_value = 0x%8.8x, "
+           "broadcast = %u\n",
+           __FUNCTION__, old_value, broadcast_type, m_value, broadcast);
+#endif
+    if (broadcast)
+      m_condition.notify_all();
+  }
+
+  DISALLOW_COPY_AND_ASSIGN(Predicate);
+};
+
+} // namespace lldb_private
+
+#endif // liblldb_Predicate_h_
Index: include/lldb/Core/Event.h
===================================================================
--- include/lldb/Core/Event.h
+++ include/lldb/Core/Event.h
@@ -11,8 +11,8 @@
 #define liblldb_Event_h_
 
 #include "lldb/Core/Broadcaster.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Utility/ConstString.h"
+#include "lldb/Utility/Predicate.h"
 #include "lldb/Utility/StructuredData.h"
 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
 #include "lldb/lldb-forward.h" // for EventDataSP, ProcessSP, Struct...
Index: include/lldb/Core/IOHandler.h
===================================================================
--- include/lldb/Core/IOHandler.h
+++ include/lldb/Core/IOHandler.h
@@ -11,9 +11,9 @@
 #define liblldb_IOHandler_h_
 
 #include "lldb/Core/ValueObjectList.h"
-#include "lldb/Host/Predicate.h"
 #include "lldb/Utility/ConstString.h"
 #include "lldb/Utility/Flags.h"
+#include "lldb/Utility/Predicate.h"
 #include "lldb/Utility/Stream.h"
 #include "lldb/Utility/StringList.h"
 #include "lldb/lldb-defines.h"  // for DISALLOW_COPY_AND_ASSIGN
Index: unittests/Host/PredicateTest.cpp
===================================================================
--- unittests/Host/PredicateTest.cpp
+++ unittests/Host/PredicateTest.cpp
@@ -1,34 +0,0 @@
-//===-- PredicateTest.cpp ---------------------------------------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "lldb/Host/Predicate.h"
-#include "gtest/gtest.h"
-#include <thread>
-
-using namespace lldb_private;
-
-TEST(Predicate, WaitForValueEqualTo) {
-  Predicate<int> P(0);
-  EXPECT_TRUE(P.WaitForValueEqualTo(0));
-  EXPECT_FALSE(P.WaitForValueEqualTo(1, std::chrono::milliseconds(10)));
-
-  std::thread Setter([&P] {
-    std::this_thread::sleep_for(std::chrono::milliseconds(100));
-    P.SetValue(1, eBroadcastAlways);
-  });
-  EXPECT_TRUE(P.WaitForValueEqualTo(1));
-  Setter.join();
-}
-
-TEST(Predicate, WaitForValueNotEqualTo) {
-  Predicate<int> P(0);
-  EXPECT_EQ(0, P.WaitForValueNotEqualTo(1));
-  EXPECT_EQ(llvm::None,
-            P.WaitForValueNotEqualTo(0, std::chrono::milliseconds(10)));
-}
Index: include/lldb/Host/Predicate.h
===================================================================
--- include/lldb/Host/Predicate.h
+++ include/lldb/Host/Predicate.h
@@ -1,260 +0,0 @@
-//===-- Predicate.h ---------------------------------------------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef liblldb_Predicate_h_
-#define liblldb_Predicate_h_
-
-// C Includes
-#include <stdint.h>
-#include <time.h>
-
-// C++ Includes
-#include <condition_variable>
-#include <mutex>
-
-// Other libraries and framework includes
-// Project includes
-#include "lldb/Utility/Timeout.h"
-#include "lldb/lldb-defines.h"
-
-//#define DB_PTHREAD_LOG_EVENTS
-
-//----------------------------------------------------------------------
-/// Enumerations for broadcasting.
-//----------------------------------------------------------------------
-namespace lldb_private {
-
-typedef enum {
-  eBroadcastNever,   ///< No broadcast will be sent when the value is modified.
-  eBroadcastAlways,  ///< Always send a broadcast when the value is modified.
-  eBroadcastOnChange ///< Only broadcast if the value changes when the value is
-                     ///modified.
-} PredicateBroadcastType;
-
-//----------------------------------------------------------------------
-/// @class Predicate Predicate.h "lldb/Host/Predicate.h"
-/// A C++ wrapper class for providing threaded access to a value of
-/// type T.
-///
-/// A templatized class that provides multi-threaded access to a value
-/// of type T. Threads can efficiently wait for bits within T to be set
-/// or reset, or wait for T to be set to be equal/not equal to a
-/// specified values.
-//----------------------------------------------------------------------
-template <class T> class Predicate {
-public:
-  //------------------------------------------------------------------
-  /// Default constructor.
-  ///
-  /// Initializes the mutex, condition and value with their default
-  /// constructors.
-  //------------------------------------------------------------------
-  Predicate() : m_value(), m_mutex(), m_condition() {}
-
-  //------------------------------------------------------------------
-  /// Construct with initial T value \a initial_value.
-  ///
-  /// Initializes the mutex and condition with their default
-  /// constructors, and initializes the value with \a initial_value.
-  ///
-  /// @param[in] initial_value
-  ///     The initial value for our T object.
-  //------------------------------------------------------------------
-  Predicate(T initial_value)
-      : m_value(initial_value), m_mutex(), m_condition() {}
-
-  //------------------------------------------------------------------
-  /// Destructor.
-  ///
-  /// Destroy the condition, mutex, and T objects.
-  //------------------------------------------------------------------
-  ~Predicate() = default;
-
-  //------------------------------------------------------------------
-  /// Value get accessor.
-  ///
-  /// Copies the current \a m_value in a thread safe manor and returns
-  /// the copied value.
-  ///
-  /// @return
-  ///     A copy of the current value.
-  //------------------------------------------------------------------
-  T GetValue() const {
-    std::lock_guard<std::mutex> guard(m_mutex);
-    T value = m_value;
-    return value;
-  }
-
-  //------------------------------------------------------------------
-  /// Value set accessor.
-  ///
-  /// Set the contained \a m_value to \a new_value in a thread safe
-  /// way and broadcast if needed.
-  ///
-  /// @param[in] value
-  ///     The new value to set.
-  ///
-  /// @param[in] broadcast_type
-  ///     A value indicating when and if to broadcast. See the
-  ///     PredicateBroadcastType enumeration for details.
-  ///
-  /// @see Predicate::Broadcast()
-  //------------------------------------------------------------------
-  void SetValue(T value, PredicateBroadcastType broadcast_type) {
-    std::lock_guard<std::mutex> guard(m_mutex);
-#ifdef DB_PTHREAD_LOG_EVENTS
-    printf("%s (value = 0x%8.8x, broadcast_type = %i)\n", __FUNCTION__, value,
-           broadcast_type);
-#endif
-    const T old_value = m_value;
-    m_value = value;
-
-    Broadcast(old_value, broadcast_type);
-  }
-
-  //------------------------------------------------------------------
-  /// Wait for Cond(m_value) to be true.
-  ///
-  /// Waits in a thread safe way for Cond(m_value) to be true. If Cond(m_value)
-  /// is already true, this function will return without waiting.
-  ///
-  /// It is possible for the value to be changed between the time the value is
-  /// set and the time the waiting thread wakes up. If the value no longer
-  /// satisfies the condition when the waiting thread wakes up, it will go back
-  /// into a wait state. It may be necessary for the calling code to use
-  /// additional thread synchronization methods to detect transitory states.
-  ///
-  /// @param[in] Cond
-  ///     The condition we want \a m_value satisfy.
-  ///
-  /// @param[in] timeout
-  ///     How long to wait for the condition to hold.
-  ///
-  /// @return
-  ///     @li m_value if Cond(m_value) is true.
-  ///     @li None otherwise (timeout occurred).
-  //------------------------------------------------------------------
-  template <typename C>
-  llvm::Optional<T> WaitFor(C Cond, const Timeout<std::micro> &timeout) {
-    std::unique_lock<std::mutex> lock(m_mutex);
-    auto RealCond = [&] { return Cond(m_value); };
-    if (!timeout) {
-      m_condition.wait(lock, RealCond);
-      return m_value;
-    }
-    if (m_condition.wait_for(lock, *timeout, RealCond))
-      return m_value;
-    return llvm::None;
-  }
-  //------------------------------------------------------------------
-  /// Wait for \a m_value to be equal to \a value.
-  ///
-  /// Waits in a thread safe way for \a m_value to be equal to \a
-  /// value. If \a m_value is already equal to \a value, this
-  /// function will return without waiting.
-  ///
-  /// It is possible for the value to be changed between the time
-  /// the value is set and the time the waiting thread wakes up.
-  /// If the value no longer matches the requested value when the
-  /// waiting thread wakes up, it will go back into a wait state.  It
-  /// may be necessary for the calling code to use additional thread
-  /// synchronization methods to detect transitory states.
-  ///
-  /// @param[in] value
-  ///     The value we want \a m_value to be equal to.
-  ///
-  /// @param[in] timeout
-  ///     How long to wait for the condition to hold.
-  ///
-  /// @return
-  ///     @li \b true if the \a m_value is equal to \a value
-  ///     @li \b false otherwise (timeout occurred)
-  //------------------------------------------------------------------
-  bool WaitForValueEqualTo(T value,
-                           const Timeout<std::micro> &timeout = llvm::None) {
-    return WaitFor([&value](T current) { return value == current; }, timeout) !=
-           llvm::None;
-  }
-
-  //------------------------------------------------------------------
-  /// Wait for \a m_value to not be equal to \a value.
-  ///
-  /// Waits in a thread safe way for \a m_value to not be equal to \a
-  /// value. If \a m_value is already not equal to \a value, this
-  /// function will return without waiting.
-  ///
-  /// It is possible for the value to be changed between the time
-  /// the value is set and the time the waiting thread wakes up.
-  /// If the value is equal to the test value when the waiting thread
-  /// wakes up, it will go back into a wait state.  It may be
-  /// necessary for the calling code to use additional thread
-  /// synchronization methods to detect transitory states.
-  ///
-  /// @param[in] value
-  ///     The value we want \a m_value to not be equal to.
-  ///
-  /// @param[in] timeout
-  ///     How long to wait for the condition to hold.
-  ///
-  /// @return
-  ///     @li m_value if m_value != value
-  ///     @li None otherwise (timeout occurred).
-  //------------------------------------------------------------------
-  llvm::Optional<T>
-  WaitForValueNotEqualTo(T value,
-                         const Timeout<std::micro> &timeout = llvm::None) {
-    return WaitFor([&value](T current) { return value != current; }, timeout);
-  }
-
-protected:
-  //----------------------------------------------------------------------
-  // pthread condition and mutex variable to control access and allow blocking
-  // between the main thread and the spotlight index thread.
-  //----------------------------------------------------------------------
-  T m_value; ///< The templatized value T that we are protecting access to
-  mutable std::mutex m_mutex; ///< The mutex to use when accessing the data
-  std::condition_variable m_condition; ///< The pthread condition variable to
-                                       ///use for signaling that data available
-                                       ///or changed.
-
-private:
-  //------------------------------------------------------------------
-  /// Broadcast if needed.
-  ///
-  /// Check to see if we need to broadcast to our condition variable
-  /// depending on the \a old_value and on the \a broadcast_type.
-  ///
-  /// If \a broadcast_type is eBroadcastNever, no broadcast will be
-  /// sent.
-  ///
-  /// If \a broadcast_type is eBroadcastAlways, the condition variable
-  /// will always be broadcast.
-  ///
-  /// If \a broadcast_type is eBroadcastOnChange, the condition
-  /// variable be broadcast if the owned value changes.
-  //------------------------------------------------------------------
-  void Broadcast(T old_value, PredicateBroadcastType broadcast_type) {
-    bool broadcast =
-        (broadcast_type == eBroadcastAlways) ||
-        ((broadcast_type == eBroadcastOnChange) && old_value != m_value);
-#ifdef DB_PTHREAD_LOG_EVENTS
-    printf("%s (old_value = 0x%8.8x, broadcast_type = %i) m_value = 0x%8.8x, "
-           "broadcast = %u\n",
-           __FUNCTION__, old_value, broadcast_type, m_value, broadcast);
-#endif
-    if (broadcast)
-      m_condition.notify_all();
-  }
-
-  DISALLOW_COPY_AND_ASSIGN(Predicate);
-};
-
-} // namespace lldb_private
-
-#endif // liblldb_Predicate_h_
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to