MainLoopBase.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifndef LLDB_HOST_MAINLOOPBASE_H
  9. #define LLDB_HOST_MAINLOOPBASE_H
  10. #include "lldb/Utility/IOObject.h"
  11. #include "lldb/Utility/Status.h"
  12. #include "llvm/Support/ErrorHandling.h"
  13. #include <functional>
  14. namespace lldb_private {
  15. // The purpose of this class is to enable multiplexed processing of data from
  16. // different sources without resorting to multi-threading. Clients can register
  17. // IOObjects, which will be monitored for readability, and when they become
  18. // ready, the specified callback will be invoked. Monitoring for writability is
  19. // not supported, but can be easily added if needed.
  20. //
  21. // The RegisterReadObject function return a handle, which controls the duration
  22. // of the monitoring. When this handle is destroyed, the callback is
  23. // deregistered.
  24. //
  25. // This class simply defines the interface common for all platforms, actual
  26. // implementations are platform-specific.
  27. class MainLoopBase {
  28. private:
  29. class ReadHandle;
  30. public:
  31. MainLoopBase() {}
  32. virtual ~MainLoopBase() {}
  33. typedef std::unique_ptr<ReadHandle> ReadHandleUP;
  34. typedef std::function<void(MainLoopBase &)> Callback;
  35. virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
  36. const Callback &callback,
  37. Status &error) {
  38. llvm_unreachable("Not implemented");
  39. }
  40. // Waits for registered events and invoke the proper callbacks. Returns when
  41. // all callbacks deregister themselves or when someone requests termination.
  42. virtual Status Run() { llvm_unreachable("Not implemented"); }
  43. // Requests the exit of the Run() function.
  44. virtual void RequestTermination() { llvm_unreachable("Not implemented"); }
  45. protected:
  46. ReadHandleUP CreateReadHandle(const lldb::IOObjectSP &object_sp) {
  47. return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle()));
  48. }
  49. virtual void UnregisterReadObject(IOObject::WaitableHandle handle) {
  50. llvm_unreachable("Not implemented");
  51. }
  52. private:
  53. class ReadHandle {
  54. public:
  55. ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); }
  56. private:
  57. ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
  58. : m_mainloop(mainloop), m_handle(handle) {}
  59. MainLoopBase &m_mainloop;
  60. IOObject::WaitableHandle m_handle;
  61. friend class MainLoopBase;
  62. ReadHandle(const ReadHandle &) = delete;
  63. const ReadHandle &operator=(const ReadHandle &) = delete;
  64. };
  65. MainLoopBase(const MainLoopBase &) = delete;
  66. const MainLoopBase &operator=(const MainLoopBase &) = delete;
  67. };
  68. } // namespace lldb_private
  69. #endif // LLDB_HOST_MAINLOOPBASE_H