LLDB  mainline
MainLoopBase.h
Go to the documentation of this file.
1 //===-- MainLoopBase.h ------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_HOST_MAINLOOPBASE_H
10 #define LLDB_HOST_MAINLOOPBASE_H
11 
12 #include "lldb/Utility/IOObject.h"
13 #include "lldb/Utility/Status.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include <functional>
17 #include <mutex>
18 
19 namespace lldb_private {
20 
21 // The purpose of this class is to enable multiplexed processing of data from
22 // different sources without resorting to multi-threading. Clients can register
23 // IOObjects, which will be monitored for readability, and when they become
24 // ready, the specified callback will be invoked. Monitoring for writability is
25 // not supported, but can be easily added if needed.
26 //
27 // The RegisterReadObject function return a handle, which controls the duration
28 // of the monitoring. When this handle is destroyed, the callback is
29 // deregistered.
30 //
31 // Since this class is primarily intended to be used for single-threaded
32 // processing, it does not attempt to perform any internal synchronisation and
33 // any concurrent accesses must be protected externally. However, it is
34 // perfectly legitimate to have more than one instance of this class running on
35 // separate threads, or even a single thread.
36 class MainLoopBase {
37 private:
38  class ReadHandle;
39 
40 public:
42  virtual ~MainLoopBase() = default;
43 
44  typedef std::unique_ptr<ReadHandle> ReadHandleUP;
45 
46  typedef std::function<void(MainLoopBase &)> Callback;
47 
48  virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
49  const Callback &callback,
50  Status &error) = 0;
51 
52  // Add a pending callback that will be executed once after all the pending
53  // events are processed. The callback will be executed even if termination
54  // was requested.
55  void AddPendingCallback(const Callback &callback);
56 
57  // Waits for registered events and invoke the proper callbacks. Returns when
58  // all callbacks deregister themselves or when someone requests termination.
59  virtual Status Run() { llvm_unreachable("Not implemented"); }
60 
61  // This should only be performed from a callback. Do not attempt to terminate
62  // the processing from another thread.
63  virtual void RequestTermination() { m_terminate_request = true; }
64 
65 protected:
66  ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) {
67  return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle()));
68  }
69 
70  virtual void UnregisterReadObject(IOObject::WaitableHandle handle) = 0;
71 
72  // Interrupt the loop that is currently waiting for events and execute
73  // the current pending callbacks immediately.
74  virtual void TriggerPendingCallbacks() = 0;
75 
77 
78  std::mutex m_callback_mutex;
79  std::vector<Callback> m_pending_callbacks;
81 
82 private:
83  class ReadHandle {
84  public:
86 
87  private:
89  : m_mainloop(mainloop), m_handle(handle) {}
90 
93 
94  friend class MainLoopBase;
95  ReadHandle(const ReadHandle &) = delete;
96  const ReadHandle &operator=(const ReadHandle &) = delete;
97  };
98 
99  MainLoopBase(const MainLoopBase &) = delete;
100  const MainLoopBase &operator=(const MainLoopBase &) = delete;
101 };
102 
103 } // namespace lldb_private
104 
105 #endif // LLDB_HOST_MAINLOOPBASE_H
lldb_private::MainLoopBase::RequestTermination
virtual void RequestTermination()
Definition: MainLoopBase.h:63
lldb_private::MainLoopBase::ReadHandle::m_mainloop
MainLoopBase & m_mainloop
Definition: MainLoopBase.h:91
lldb_private::MainLoopBase::TriggerPendingCallbacks
virtual void TriggerPendingCallbacks()=0
lldb_private::MainLoopBase::UnregisterReadObject
virtual void UnregisterReadObject(IOObject::WaitableHandle handle)=0
lldb_private::MainLoopBase
Definition: MainLoopBase.h:36
lldb_private::MainLoopBase::ReadHandle::m_handle
IOObject::WaitableHandle m_handle
Definition: MainLoopBase.h:92
lldb_private::MainLoopBase::MainLoopBase
MainLoopBase()
Definition: MainLoopBase.h:41
lldb_private::MainLoopBase::ReadHandleUP
std::unique_ptr< ReadHandle > ReadHandleUP
Definition: MainLoopBase.h:44
lldb_private::MainLoopBase::ReadHandle::~ReadHandle
~ReadHandle()
Definition: MainLoopBase.h:85
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::MainLoopBase::CreateReadHandle
ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp)
Definition: MainLoopBase.h:66
lldb_private::MainLoopBase::~MainLoopBase
virtual ~MainLoopBase()=default
lldb_private::MainLoopBase::ReadHandle
Definition: MainLoopBase.h:83
lldb_private::IOObject::WaitableHandle
int WaitableHandle
Definition: IOObject.h:29
lldb_private::MainLoopBase::RegisterReadObject
virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp, const Callback &callback, Status &error)=0
lldb_private::MainLoopBase::operator=
const MainLoopBase & operator=(const MainLoopBase &)=delete
lldb_private::Status
Definition: Status.h:44
lldb_private::MainLoopBase::m_callback_mutex
std::mutex m_callback_mutex
Definition: MainLoopBase.h:78
lldb_private::MainLoopBase::AddPendingCallback
void AddPendingCallback(const Callback &callback)
Definition: MainLoopBase.cpp:14
lldb_private::MainLoopBase::m_terminate_request
bool m_terminate_request
Definition: MainLoopBase.h:80
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::MainLoopBase::Callback
std::function< void(MainLoopBase &)> Callback
Definition: MainLoopBase.h:46
lldb_private::MainLoopBase::ProcessPendingCallbacks
void ProcessPendingCallbacks()
Definition: MainLoopBase.cpp:22
lldb_private::MainLoopBase::Run
virtual Status Run()
Definition: MainLoopBase.h:59
IOObject.h
lldb_private::MainLoopBase::ReadHandle::operator=
const ReadHandle & operator=(const ReadHandle &)=delete
lldb_private::MainLoopBase::m_pending_callbacks
std::vector< Callback > m_pending_callbacks
Definition: MainLoopBase.h:79
lldb_private::MainLoopBase::ReadHandle::ReadHandle
ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
Definition: MainLoopBase.h:88