]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/MainLoopBase.h
Merge ^/head r305220 through r305300.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Host / MainLoopBase.h
1 //===-- MainLoopBase.h ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef lldb_Host_posix_MainLoopBase_h_
11 #define lldb_Host_posix_MainLoopBase_h_
12
13 #include <functional>
14
15 #include "llvm/Support/ErrorHandling.h"
16
17 #include "lldb/Core/Error.h"
18 #include "lldb/Host/IOObject.h"
19
20 namespace lldb_private {
21
22 // The purpose of this class is to enable multiplexed processing of data from different sources
23 // without resorting to multi-threading. Clients can register IOObjects, which will be monitored
24 // for readability, and when they become ready, the specified callback will be invoked.
25 // Monitoring for writability is not supported, but can be easily added if needed.
26 //
27 // The RegisterReadObject function return a handle, which controls the duration of the monitoring. When
28 // this handle is destroyed, the callback is deregistered.
29 //
30 // This class simply defines the interface common for all platforms, actual implementations are
31 // platform-specific.
32 class MainLoopBase
33 {
34 private:
35     class ReadHandle;
36
37 public:
38     MainLoopBase() { }
39     virtual ~MainLoopBase() { }
40
41     typedef std::unique_ptr<ReadHandle> ReadHandleUP;
42
43     typedef std::function<void(MainLoopBase &)> Callback;
44
45     virtual ReadHandleUP
46     RegisterReadObject(const lldb::IOObjectSP &object_sp, const Callback &callback, Error &error)
47     { llvm_unreachable("Not implemented"); }
48
49     // Waits for registered events and invoke the proper callbacks. Returns when all callbacks
50     // deregister themselves or when someone requests termination.
51     virtual Error
52     Run()
53     { llvm_unreachable("Not implemented"); }
54
55     // Requests the exit of the Run() function.
56     virtual void
57     RequestTermination()
58     { llvm_unreachable("Not implemented"); }
59
60 protected:
61     ReadHandleUP
62     CreateReadHandle(const lldb::IOObjectSP &object_sp)
63     { return ReadHandleUP(new ReadHandle(*this, object_sp->GetWaitableHandle())); }
64
65     virtual void
66     UnregisterReadObject(IOObject::WaitableHandle handle)
67     { llvm_unreachable("Not implemented"); }
68
69 private:
70     class ReadHandle
71     {
72     public:
73         ~ReadHandle() { m_mainloop.UnregisterReadObject(m_handle); }
74
75     private:
76         ReadHandle(MainLoopBase &mainloop, IOObject::WaitableHandle handle)
77             : m_mainloop(mainloop), m_handle(handle)
78         { }
79
80         MainLoopBase &m_mainloop;
81         IOObject::WaitableHandle m_handle;
82
83         friend class MainLoopBase;
84         DISALLOW_COPY_AND_ASSIGN(ReadHandle);
85     };
86
87 private:
88     DISALLOW_COPY_AND_ASSIGN(MainLoopBase);
89 };
90
91 } // namespace lldb_private
92
93
94 #endif // lldb_Host_posix_MainLoopBase_h_