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_posix_MainLoopBase_h_ 10 #define lldb_Host_posix_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() {} 37 virtual ~MainLoopBase() {} 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 61 virtual void UnregisterReadObject(IOObject::WaitableHandle handle) { 62 llvm_unreachable("Not implemented"); 63 } 64 65 private: 66 class ReadHandle { 67 public: 68 ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); } 69 70 private: 71 ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle) 72 : m_mainloop(mainloop), m_handle(handle) {} 73 74 MainLoopBase &m_mainloop; 75 IOObject::WaitableHandle m_handle; 76 77 friend class MainLoopBase; 78 DISALLOW_COPY_AND_ASSIGN(ReadHandle); 79 }; 80 81 private: 82 DISALLOW_COPY_AND_ASSIGN(MainLoopBase); 83 }; 84 85 } // namespace lldb_private 86 87 #endif // lldb_Host_posix_MainLoopBase_h_ 88