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