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 // Add a pending callback that will be executed once after all the pending 50 // events are processed. The callback will be executed even if termination 51 // was requested. 52 virtual void AddPendingCallback(const Callback &callback) { 53 llvm_unreachable("Not implemented"); 54 } 55 56 // Waits for registered events and invoke the proper callbacks. Returns when 57 // all callbacks deregister themselves or when someone requests termination. 58 virtual Status Run() { llvm_unreachable("Not implemented"); } 59 60 // Requests the exit of the Run() function. 61 virtual void RequestTermination() { llvm_unreachable("Not implemented"); } 62 63 protected: 64 ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) { 65 return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle())); 66 } 67 68 virtual void UnregisterReadObject(IOObject::WaitableHandle handle) { 69 llvm_unreachable("Not implemented"); 70 } 71 72 private: 73 class ReadHandle { 74 public: 75 ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); } 76 77 private: 78 ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle) 79 : m_mainloop(mainloop), m_handle(handle) {} 80 81 MainLoopBase &m_mainloop; 82 IOObject::WaitableHandle m_handle; 83 84 friend class MainLoopBase; 85 ReadHandle(const ReadHandle &) = delete; 86 const ReadHandle &operator=(const ReadHandle &) = delete; 87 }; 88 89 MainLoopBase(const MainLoopBase &) = delete; 90 const MainLoopBase &operator=(const MainLoopBase &) = delete; 91 }; 92 93 } // namespace lldb_private 94 95 #endif // LLDB_HOST_MAINLOOPBASE_H 96