1 //===-- MainLoopBase.h ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef lldb_Host_posix_MainLoopBase_h_
10 #define lldb_Host_posix_MainLoopBase_h_
12 #include "lldb/Utility/IOObject.h"
13 #include "lldb/Utility/Status.h"
14 #include "llvm/Support/ErrorHandling.h"
17 namespace lldb_private {
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.
25 // The RegisterReadObject function return a handle, which controls the duration
26 // of the monitoring. When this handle is destroyed, the callback is
29 // This class simply defines the interface common for all platforms, actual
30 // implementations are platform-specific.
37 virtual ~MainLoopBase() {}
39 typedef std::unique_ptr<ReadHandle> ReadHandleUP;
41 typedef std::function<void(MainLoopBase &)> Callback;
43 virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
44 const Callback &callback,
46 llvm_unreachable("Not implemented");
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"); }
53 // Requests the exit of the Run() function.
54 virtual void RequestTermination() { llvm_unreachable("Not implemented"); }
57 ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) {
58 return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle()));
61 virtual void UnregisterReadObject(IOObject::WaitableHandle handle) {
62 llvm_unreachable("Not implemented");
68 ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); }
71 ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
72 : m_mainloop(mainloop), m_handle(handle) {}
74 MainLoopBase &m_mainloop;
75 IOObject::WaitableHandle m_handle;
77 friend class MainLoopBase;
78 DISALLOW_COPY_AND_ASSIGN(ReadHandle);
82 DISALLOW_COPY_AND_ASSIGN(MainLoopBase);
85 } // namespace lldb_private
87 #endif // lldb_Host_posix_MainLoopBase_h_