xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Host/MainLoopBase.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===-- MainLoopBase.h ------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9*5ffd83dbSDimitry Andric #ifndef LLDB_HOST_MAINLOOPBASE_H
10*5ffd83dbSDimitry Andric #define LLDB_HOST_MAINLOOPBASE_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "lldb/Utility/IOObject.h"
130b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
140b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
150b57cec5SDimitry Andric #include <functional>
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric namespace lldb_private {
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric // The purpose of this class is to enable multiplexed processing of data from
200b57cec5SDimitry Andric // different sources without resorting to multi-threading. Clients can register
210b57cec5SDimitry Andric // IOObjects, which will be monitored for readability, and when they become
220b57cec5SDimitry Andric // ready, the specified callback will be invoked. Monitoring for writability is
230b57cec5SDimitry Andric // not supported, but can be easily added if needed.
240b57cec5SDimitry Andric //
250b57cec5SDimitry Andric // The RegisterReadObject function return a handle, which controls the duration
260b57cec5SDimitry Andric // of the monitoring. When this handle is destroyed, the callback is
270b57cec5SDimitry Andric // deregistered.
280b57cec5SDimitry Andric //
290b57cec5SDimitry Andric // This class simply defines the interface common for all platforms, actual
300b57cec5SDimitry Andric // implementations are platform-specific.
310b57cec5SDimitry Andric class MainLoopBase {
320b57cec5SDimitry Andric private:
330b57cec5SDimitry Andric   class ReadHandle;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric public:
360b57cec5SDimitry Andric   MainLoopBase() {}
370b57cec5SDimitry Andric   virtual ~MainLoopBase() {}
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   typedef std::unique_ptr<ReadHandle> ReadHandleUP;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   typedef std::function<void(MainLoopBase &)> Callback;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
440b57cec5SDimitry Andric                                           const Callback &callback,
450b57cec5SDimitry Andric                                           Status &error) {
460b57cec5SDimitry Andric     llvm_unreachable("Not implemented");
470b57cec5SDimitry Andric   }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   // Waits for registered events and invoke the proper callbacks. Returns when
500b57cec5SDimitry Andric   // all callbacks deregister themselves or when someone requests termination.
510b57cec5SDimitry Andric   virtual Status Run() { llvm_unreachable("Not implemented"); }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // Requests the exit of the Run() function.
540b57cec5SDimitry Andric   virtual void RequestTermination() { llvm_unreachable("Not implemented"); }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric protected:
570b57cec5SDimitry Andric   ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) {
580b57cec5SDimitry Andric     return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle()));
590b57cec5SDimitry Andric   }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   virtual void UnregisterReadObject(IOObject::WaitableHandle handle) {
620b57cec5SDimitry Andric     llvm_unreachable("Not implemented");
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric private:
660b57cec5SDimitry Andric   class ReadHandle {
670b57cec5SDimitry Andric   public:
680b57cec5SDimitry Andric     ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   private:
710b57cec5SDimitry Andric     ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
720b57cec5SDimitry Andric         : m_mainloop(mainloop), m_handle(handle) {}
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric     MainLoopBase &m_mainloop;
750b57cec5SDimitry Andric     IOObject::WaitableHandle m_handle;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric     friend class MainLoopBase;
78*5ffd83dbSDimitry Andric     ReadHandle(const ReadHandle &) = delete;
79*5ffd83dbSDimitry Andric     const ReadHandle &operator=(const ReadHandle &) = delete;
800b57cec5SDimitry Andric   };
810b57cec5SDimitry Andric 
82*5ffd83dbSDimitry Andric   MainLoopBase(const MainLoopBase &) = delete;
83*5ffd83dbSDimitry Andric   const MainLoopBase &operator=(const MainLoopBase &) = delete;
840b57cec5SDimitry Andric };
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric } // namespace lldb_private
870b57cec5SDimitry Andric 
88*5ffd83dbSDimitry Andric #endif // LLDB_HOST_MAINLOOPBASE_H
89