[NativeProcessLinux] Integrate MainLoop

Summary:
This commit integrates MainLoop into NativeProcessLinux. By registering a SIGCHLD handler with
the llgs main loop, we can get rid of the special monitor thread in NPL, which saves as a lot of
thread ping-pong when responding to client requests (e.g. qThreadInfo processing time has been
reduced by about 40%). It also makes the code simpler, IMHO.

Reviewers: ovyalov, clayborg, tberghammer, chaoren

Subscribers: lldb-commits

Differential Revision: http://reviews.llvm.org/D11150

This is a resubmission of r242305 after it was reverted due to bad interactions with the stdio
thread.

llvm-svn: 242783
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
index e3832d6..1632f7f 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
@@ -43,25 +43,16 @@
         friend Error
         NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
                 NativeDelegate &native_delegate,
+                MainLoop &mainloop,
                 NativeProcessProtocolSP &process_sp);
 
         friend Error
         NativeProcessProtocol::Attach (lldb::pid_t pid,
-            NativeProcessProtocol::NativeDelegate &native_delegate,
-            NativeProcessProtocolSP &native_process_sp);
+                NativeProcessProtocol::NativeDelegate &native_delegate,
+                MainLoop &mainloop,
+                NativeProcessProtocolSP &process_sp);
 
     public:
-        //------------------------------------------------------------------------------
-        /// @class Operation
-        /// @brief Represents a NativeProcessLinux operation.
-        ///
-        /// Under Linux, it is not possible to ptrace() from any other thread but the
-        /// one that spawned or attached to the process from the start.  Therefore, when
-        /// a NativeProcessLinux is asked to deliver or change the state of an inferior
-        /// process the operation must be "funneled" to a specific thread to perform the
-        /// task.
-        typedef std::function<Error()> Operation;
-
         // ---------------------------------------------------------------------
         // NativeProcessProtocol Interface
         // ---------------------------------------------------------------------
@@ -113,18 +104,9 @@
         Error
         SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
 
-        Error
-        SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override;
-
-        Error
-        RemoveWatchpoint (lldb::addr_t addr) override;
-
         void
         DoStopIDBumped (uint32_t newBumpId) override;
 
-        void
-        Terminate () override;
-
         Error
         GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
 
@@ -134,9 +116,6 @@
         // ---------------------------------------------------------------------
         // Interface used by NativeRegisterContext-derived classes.
         // ---------------------------------------------------------------------
-        Error
-        DoOperation(const Operation &op);
-
         static Error
         PtraceWrapper(int req,
                       lldb::pid_t pid,
@@ -154,12 +133,9 @@
 
     private:
 
-        class Monitor;
-
+        MainLoop::SignalHandleUP m_sigchld_handle;
         ArchSpec m_arch;
 
-        std::unique_ptr<Monitor> m_monitor_up;
-
         LazyBool m_supports_mem_region;
         std::vector<MemoryRegionInfo> m_mem_region_cache;
         Mutex m_mem_region_cache_mutex;
@@ -206,6 +182,7 @@
         /// implementation of Process::DoLaunch.
         void
         LaunchInferior (
+            MainLoop &mainloop,
             Module *module,
             char const *argv[],
             char const *envp[],
@@ -219,10 +196,7 @@
         /// Attaches to an existing process.  Forms the
         /// implementation of Process::DoAttach
         void
-        AttachToInferior (lldb::pid_t pid, Error &error);
-
-        void
-        StartMonitorThread(const InitialOperation &operation, Error &error);
+        AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error);
 
         ::pid_t
         Launch(LaunchArgs *args, Error &error);
@@ -370,6 +344,9 @@
         void
         ThreadWasCreated (lldb::tid_t tid);
 
+        void
+        SigchldHandler();
+
         // Member variables.
         PendingNotificationUP m_pending_notification_up;
     };