blob: fa8cc77a94ba2bf25fc3957f7c994395551bf7ed [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===-- 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 Deprezf4ef2d02021-04-20 13:36:24 +02009#ifndef LLDB_HOST_MAINLOOPBASE_H
10#define LLDB_HOST_MAINLOOPBASE_H
Andrew Walbran3d2c1972020-04-07 12:24:26 +010011
12#include "lldb/Utility/IOObject.h"
13#include "lldb/Utility/Status.h"
14#include "llvm/Support/ErrorHandling.h"
15#include <functional>
16
17namespace 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.
31class MainLoopBase {
32private:
33 class ReadHandle;
34
35public:
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
56protected:
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
65private:
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 Deprezf4ef2d02021-04-20 13:36:24 +020078 ReadHandle(const ReadHandle &) = delete;
79 const ReadHandle &operator=(const ReadHandle &) = delete;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010080 };
81
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020082 MainLoopBase(const MainLoopBase &) = delete;
83 const MainLoopBase &operator=(const MainLoopBase &) = delete;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010084};
85
86} // namespace lldb_private
87
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020088#endif // LLDB_HOST_MAINLOOPBASE_H