1 //===-- MainLoopPosix.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_Host_posix_MainLoopPosix_h_
11 #define lldb_Host_posix_MainLoopPosix_h_
13 #include "lldb/Host/MainLoopBase.h"
15 #include "llvm/ADT/DenseMap.h"
17 namespace lldb_private {
19 // Posix implementation of the MainLoopBase class. It can monitor file
21 // readability using pselect. In addition to the common base, this class
22 // provides the ability to
23 // invoke a given handler when a signal is received.
25 // Since this class is primarily intended to be used for single-threaded
26 // processing, it does not
27 // attempt to perform any internal synchronisation and any concurrent accesses
29 // externally. However, it is perfectly legitimate to have more than one
30 // instance of this class
31 // running on separate threads, or even a single thread (with some limitations
34 // TODO: Add locking if this class is to be used in a multi-threaded context.
35 class MainLoopPosix : public MainLoopBase {
40 typedef std::unique_ptr<SignalHandle> SignalHandleUP;
42 ~MainLoopPosix() override;
44 ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp,
45 const Callback &callback,
46 Error &error) override;
48 // Listening for signals from multiple MainLoopPosix instances is perfectly
49 // safe as long as they
50 // don't try to listen for the same signal. The callback function is invoked
52 // returns to the Run() function, not when the hander is executed. This means
54 // treat the callback as a normal function and perform things which would not
56 // signal handler. However, since the callback is not invoked synchronously,
58 // this mechanism to handle SIGSEGV and the like.
59 SignalHandleUP RegisterSignal(int signo, const Callback &callback,
64 // This should only be performed from a callback. Do not attempt to terminate
66 // from another thread.
67 // TODO: Add synchronization if we want to be terminated from another thread.
68 void RequestTermination() override { m_terminate_request = true; }
71 void UnregisterReadObject(IOObject::WaitableHandle handle) override;
73 void UnregisterSignal(int signo);
78 ~SignalHandle() { m_mainloop.UnregisterSignal(m_signo); }
81 SignalHandle(MainLoopPosix &mainloop, int signo)
82 : m_mainloop(mainloop), m_signo(signo) {}
84 MainLoopPosix &m_mainloop;
87 friend class MainLoopPosix;
88 DISALLOW_COPY_AND_ASSIGN(SignalHandle);
93 struct sigaction old_action;
97 llvm::DenseMap<IOObject::WaitableHandle, Callback> m_read_fds;
98 llvm::DenseMap<int, SignalInfo> m_signals;
99 bool m_terminate_request : 1;
102 } // namespace lldb_private
104 #endif // lldb_Host_posix_MainLoopPosix_h_