Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 9 | #ifndef LLDB_HOST_MAINLOOPBASE_H |
| 10 | #define LLDB_HOST_MAINLOOPBASE_H |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 78 | ReadHandle(const ReadHandle &) = delete; |
| 79 | const ReadHandle &operator=(const ReadHandle &) = delete; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 82 | MainLoopBase(const MainLoopBase &) = delete; |
| 83 | const MainLoopBase &operator=(const MainLoopBase &) = delete; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace lldb_private |
| 87 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 88 | #endif // LLDB_HOST_MAINLOOPBASE_H |