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_
15 #include "llvm/Support/ErrorHandling.h"
17 #include "lldb/Core/Error.h"
18 #include "lldb/Host/IOObject.h"
20 namespace lldb_private {
22 // The purpose of this class is to enable multiplexed processing of data from
24 // without resorting to multi-threading. Clients can register IOObjects, which
26 // for readability, and when they become ready, the specified callback will be
28 // Monitoring for writability is not supported, but can be easily added if
31 // The RegisterReadObject function return a handle, which controls the duration
32 // of the monitoring. When
33 // this handle is destroyed, the callback is deregistered.
35 // This class simply defines the interface common for all platforms, actual
36 // implementations are
44 virtual ~MainLoopBase() {}
46 typedef std::unique_ptr<ReadHandle> ReadHandleUP;
48 typedef std::function<void(MainLoopBase &)> Callback;
50 virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
51 const Callback &callback,
53 llvm_unreachable("Not implemented");
56 // Waits for registered events and invoke the proper callbacks. Returns when
58 // deregister themselves or when someone requests termination.
59 virtual Error Run() { llvm_unreachable("Not implemented"); }
61 // Requests the exit of the Run() function.
62 virtual void RequestTermination() { llvm_unreachable("Not implemented"); }
65 ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) {
66 return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle()));
69 virtual void UnregisterReadObject(IOObject::WaitableHandle handle) {
70 llvm_unreachable("Not implemented");
76 ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); }
79 ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
80 : m_mainloop(mainloop), m_handle(handle) {}
82 MainLoopBase &m_mainloop;
83 IOObject::WaitableHandle m_handle;
85 friend class MainLoopBase;
86 DISALLOW_COPY_AND_ASSIGN(ReadHandle);
90 DISALLOW_COPY_AND_ASSIGN(MainLoopBase);
93 } // namespace lldb_private
95 #endif // lldb_Host_posix_MainLoopBase_h_