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