]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/MainLoop.h
Merge ^/head r338690 through r338730.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Host / MainLoop.h
1 //===-- MainLoop.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_MainLoop_h_
11 #define lldb_Host_MainLoop_h_
12
13 #include "lldb/Host/Config.h"
14 #include "lldb/Host/MainLoopBase.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include <csignal>
17
18 #if !HAVE_PPOLL && !HAVE_SYS_EVENT_H && !defined(__ANDROID__)
19 #define SIGNAL_POLLING_UNSUPPORTED 1
20 #endif
21
22 namespace lldb_private {
23
24 // Implementation of the MainLoopBase class. It can monitor file descriptors
25 // for readability using ppoll, kqueue, poll or WSAPoll. On Windows it only
26 // supports polling sockets, and will not work on generic file handles or
27 // pipes. On systems without kqueue or ppoll handling singnals is not
28 // supported. In addition to the common base, this class provides the ability
29 // to invoke a given handler when a signal is received.
30 //
31 // Since this class is primarily intended to be used for single-threaded
32 // processing, it does not attempt to perform any internal synchronisation and
33 // any concurrent accesses must be protected  externally. However, it is
34 // perfectly legitimate to have more than one instance of this class running on
35 // separate threads, or even a single thread (with some limitations on signal
36 // monitoring).
37 // TODO: Add locking if this class is to be used in a multi-threaded context.
38 class MainLoop : public MainLoopBase {
39 private:
40   class SignalHandle;
41
42 public:
43   typedef std::unique_ptr<SignalHandle> SignalHandleUP;
44
45   MainLoop();
46   ~MainLoop() override;
47
48   ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
49                                   const Callback &callback,
50                                   Status &error) override;
51
52   // Listening for signals from multiple MainLoop instances is perfectly safe
53   // as long as they don't try to listen for the same signal. The callback
54   // function is invoked when the control returns to the Run() function, not
55   // when the hander is executed. This mean that you can treat the callback as
56   // a normal function and perform things which would not be safe in a signal
57   // handler. However, since the callback is not invoked synchronously, you
58   // cannot use this mechanism to handle SIGSEGV and the like.
59   SignalHandleUP RegisterSignal(int signo, const Callback &callback,
60                                 Status &error);
61
62   Status Run() override;
63
64   // This should only be performed from a callback. Do not attempt to terminate
65   // the processing from another thread.
66   // TODO: Add synchronization if we want to be terminated from another thread.
67   void RequestTermination() override { m_terminate_request = true; }
68
69 protected:
70   void UnregisterReadObject(IOObject::WaitableHandle handle) override;
71
72   void UnregisterSignal(int signo);
73
74 private:
75   void ProcessReadObject(IOObject::WaitableHandle handle);
76   void ProcessSignal(int signo);
77
78   class SignalHandle {
79   public:
80     ~SignalHandle() { m_mainloop.UnregisterSignal(m_signo); }
81
82   private:
83     SignalHandle(MainLoop &mainloop, int signo)
84         : m_mainloop(mainloop), m_signo(signo) {}
85
86     MainLoop &m_mainloop;
87     int m_signo;
88
89     friend class MainLoop;
90     DISALLOW_COPY_AND_ASSIGN(SignalHandle);
91   };
92
93   struct SignalInfo {
94     Callback callback;
95 #if HAVE_SIGACTION
96     struct sigaction old_action;
97 #endif
98     bool was_blocked : 1;
99   };
100   class RunImpl;
101
102   llvm::DenseMap<IOObject::WaitableHandle, Callback> m_read_fds;
103   llvm::DenseMap<int, SignalInfo> m_signals;
104 #if HAVE_SYS_EVENT_H
105   int m_kqueue;
106 #endif
107   bool m_terminate_request : 1;
108 };
109
110 } // namespace lldb_private
111
112 #endif // lldb_Host_MainLoop_h_