There are several places where multiple threads are accessing the same 
variables simultaneously without any kind of protection.  I propose using 
std::atomic<> to make it safer.  I did a special build of lldb, using the 
google tool 'thread sanitizer' which identified many cases of multiple threads 
accessing the same memory.  std::atomic is low overhead and does not use any 
locks for simple types such as int/bool.

http://reviews.llvm.org/D5302

Files:
  include/lldb/Core/Communication.h
  include/lldb/Core/ConnectionFileDescriptor.h
  include/lldb/Target/Process.h
  source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
  source/lldb-log.cpp
Index: include/lldb/Core/Communication.h
===================================================================
--- include/lldb/Core/Communication.h
+++ include/lldb/Core/Communication.h
@@ -352,7 +352,7 @@
 protected:
     lldb::ConnectionSP m_connection_sp; ///< The connection that is current in use by this communications class.
     HostThread m_read_thread;           ///< The read thread handle in case we need to cancel the thread.
-    bool m_read_thread_enabled;
+    std::atomic<bool> m_read_thread_enabled;
     std::string m_bytes;    ///< A buffer to cache bytes read in the ReadThread function.
     Mutex m_bytes_mutex;    ///< A mutex to protect multi-threaded access to the cached bytes.
     Mutex m_write_mutex;    ///< Don't let multiple threads write at the same time...
Index: include/lldb/Core/ConnectionFileDescriptor.h
===================================================================
--- include/lldb/Core/ConnectionFileDescriptor.h
+++ include/lldb/Core/ConnectionFileDescriptor.h
@@ -106,8 +106,8 @@
 
     Pipe m_pipe;
     Mutex m_mutex;
-    bool m_shutting_down;       // This marks that we are shutting down so if we get woken up from
-                                // BytesAvailable to disconnect, we won't try to read again.
+    std::atomic<bool> m_shutting_down;    // This marks that we are shutting down so if we get woken up from
+                                          // BytesAvailable to disconnect, we won't try to read again.
     bool m_waiting_for_accept;
 private:
     DISALLOW_COPY_AND_ASSIGN (ConnectionFileDescriptor);
Index: include/lldb/Target/Process.h
===================================================================
--- include/lldb/Target/Process.h
+++ include/lldb/Target/Process.h
@@ -3048,7 +3048,7 @@
     uint32_t                    m_process_unique_id;    ///< Each lldb_private::Process class that is created gets a unique integer ID that increments with each new instance
     uint32_t                    m_thread_index_id;      ///< Each thread is created with a 1 based index that won't get re-used.
     std::map<uint64_t, uint32_t> m_thread_id_to_index_id_map;
-    int                         m_exit_status;          ///< The exit status of the process, or -1 if not set.
+    std::atomic<int>            m_exit_status;          ///< The exit status of the process, or -1 if not set.
     std::string                 m_exit_string;          ///< A textual description of why a process exited.
     Mutex                       m_thread_mutex;
     ThreadList                  m_thread_list_real;     ///< The threads for this process as are known to the protocol we are debugging with
Index: source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
===================================================================
--- source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
+++ source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
@@ -328,7 +328,7 @@
     
     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
     GDBRemoteCommunicationClient m_gdb_comm;
-    lldb::pid_t m_debugserver_pid;
+    std::atomic<lldb::pid_t> m_debugserver_pid;
     StringExtractorGDBRemote m_last_stop_packet;
     lldb_private::Mutex m_last_stop_packet_mutex;
     GDBRemoteDynamicRegisterInfo m_register_info;
Index: source/lldb-log.cpp
===================================================================
--- source/lldb-log.cpp
+++ source/lldb-log.cpp
@@ -27,7 +27,7 @@
 // that will construct the static g_lob_sp the first time this function is 
 // called.
 
-static bool g_log_enabled = false;
+static std::atomic<bool> g_log_enabled {false};
 static Log * g_log = NULL;
 static Log *
 GetLog ()
_______________________________________________
lldb-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits

Reply via email to