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/Support/ErrorHandling.h"
15 #include <functional>
16 
17 namespace lldb_private {
18 
19 // The purpose of this class is to enable multiplexed processing of data from
20 // different sources without resorting to multi-threading. Clients can register
21 // IOObjects, which will be monitored for readability, and when they become
22 // ready, the specified callback will be invoked. Monitoring for writability is
23 // not supported, but can be easily added if needed.
24 //
25 // The RegisterReadObject function return a handle, which controls the duration
26 // of the monitoring. When this handle is destroyed, the callback is
27 // deregistered.
28 //
29 // This class simply defines the interface common for all platforms, actual
30 // implementations are platform-specific.
31 class MainLoopBase {
32 private:
33  class ReadHandle;
34 
35 public:
36  MainLoopBase() = default;
37  virtual ~MainLoopBase() = default;
38 
39  typedef std::unique_ptr<ReadHandle> ReadHandleUP;
40 
41  typedef std::function<void(MainLoopBase &)> Callback;
42 
43  virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
44  const Callback &callback,
45  Status &error) {
46  llvm_unreachable("Not implemented");
47  }
48 
49  // Waits for registered events and invoke the proper callbacks. Returns when
50  // all callbacks deregister themselves or when someone requests termination.
51  virtual Status Run() { llvm_unreachable("Not implemented"); }
52 
53  // Requests the exit of the Run() function.
54  virtual void RequestTermination() { llvm_unreachable("Not implemented"); }
55 
56 protected:
57  ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) {
58  return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle()));
59  }
60 
62  llvm_unreachable("Not implemented");
63  }
64 
65 private:
66  class ReadHandle {
67  public:
69 
70  private:
72  : m_mainloop(mainloop), m_handle(handle) {}
73 
76 
77  friend class MainLoopBase;
78  ReadHandle(const ReadHandle &) = delete;
79  const ReadHandle &operator=(const ReadHandle &) = delete;
80  };
81 
82  MainLoopBase(const MainLoopBase &) = delete;
83  const MainLoopBase &operator=(const MainLoopBase &) = delete;
84 };
85 
86 } // namespace lldb_private
87 
88 #endif // LLDB_HOST_MAINLOOPBASE_H
lldb_private::MainLoopBase::RegisterReadObject
virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp, const Callback &callback, Status &error)
Definition: MainLoopBase.h:43
lldb_private::MainLoopBase::RequestTermination
virtual void RequestTermination()
Definition: MainLoopBase.h:54
lldb_private::MainLoopBase::ReadHandle::m_mainloop
MainLoopBase & m_mainloop
Definition: MainLoopBase.h:74
lldb_private::MainLoopBase::UnregisterReadObject
virtual void UnregisterReadObject(IOObject::WaitableHandle handle)
Definition: MainLoopBase.h:61
lldb_private::MainLoopBase
Definition: MainLoopBase.h:31
lldb_private::MainLoopBase::ReadHandle::m_handle
IOObject::WaitableHandle m_handle
Definition: MainLoopBase.h:75
lldb_private::MainLoopBase::ReadHandleUP
std::unique_ptr< ReadHandle > ReadHandleUP
Definition: MainLoopBase.h:39
lldb_private::MainLoopBase::ReadHandle::~ReadHandle
~ReadHandle()
Definition: MainLoopBase.h:68
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:57
lldb_private::MainLoopBase::~MainLoopBase
virtual ~MainLoopBase()=default
lldb_private::MainLoopBase::ReadHandle
Definition: MainLoopBase.h:66
lldb_private::IOObject::WaitableHandle
int WaitableHandle
Definition: IOObject.h:29
lldb_private::MainLoopBase::operator=
const MainLoopBase & operator=(const MainLoopBase &)=delete
lldb_private::Status
Definition: Status.h:44
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::MainLoopBase::MainLoopBase
MainLoopBase()=default
lldb_private::MainLoopBase::Callback
std::function< void(MainLoopBase &)> Callback
Definition: MainLoopBase.h:41
lldb_private::MainLoopBase::Run
virtual Status Run()
Definition: MainLoopBase.h:51
IOObject.h
lldb_private::MainLoopBase::ReadHandle::operator=
const ReadHandle & operator=(const ReadHandle &)=delete
lldb_private::MainLoopBase::ReadHandle::ReadHandle
ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
Definition: MainLoopBase.h:71