1 //===-- MainLoopBase.h ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_Host_posix_MainLoopBase_h_
11 #define lldb_Host_posix_MainLoopBase_h_
13 #include "lldb/Utility/IOObject.h"
14 #include "lldb/Utility/Status.h"
15 #include "llvm/Support/ErrorHandling.h"
18 namespace lldb_private {
20 // The purpose of this class is to enable multiplexed processing of data from
21 // different sources without resorting to multi-threading. Clients can register
22 // IOObjects, which will be monitored for readability, and when they become
23 // ready, the specified callback will be invoked. Monitoring for writability is
24 // not supported, but can be easily added if needed.
26 // The RegisterReadObject function return a handle, which controls the duration
27 // of the monitoring. When this handle is destroyed, the callback is
30 // This class simply defines the interface common for all platforms, actual
31 // implementations are platform-specific.
38 virtual ~MainLoopBase() {}
40 typedef std::unique_ptr<ReadHandle> ReadHandleUP;
42 typedef std::function<void(MainLoopBase &)> Callback;
44 virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
45 const Callback &callback,
47 llvm_unreachable("Not implemented");
50 // Waits for registered events and invoke the proper callbacks. Returns when
51 // all callbacks deregister themselves or when someone requests termination.
52 virtual Status Run() { llvm_unreachable("Not implemented"); }
54 // Requests the exit of the Run() function.
55 virtual void RequestTermination() { llvm_unreachable("Not implemented"); }
58 ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) {
59 return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle()));
62 virtual void UnregisterReadObject(IOObject::WaitableHandle handle) {
63 llvm_unreachable("Not implemented");
69 ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); }
72 ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
73 : m_mainloop(mainloop), m_handle(handle) {}
75 MainLoopBase &m_mainloop;
76 IOObject::WaitableHandle m_handle;
78 friend class MainLoopBase;
79 DISALLOW_COPY_AND_ASSIGN(ReadHandle);
83 DISALLOW_COPY_AND_ASSIGN(MainLoopBase);
86 } // namespace lldb_private
88 #endif // lldb_Host_posix_MainLoopBase_h_