diff options
Diffstat (limited to 'source/Core/Communication.cpp')
-rw-r--r-- | source/Core/Communication.cpp | 139 |
1 files changed, 62 insertions, 77 deletions
diff --git a/source/Core/Communication.cpp b/source/Core/Communication.cpp index ae579d1b00ae5..f1dcb95e8af0c 100644 --- a/source/Core/Communication.cpp +++ b/source/Core/Communication.cpp @@ -9,17 +9,19 @@ // C Includes // C++ Includes +#include <cstring> + // Other libraries and framework includes // Project includes #include "lldb/Core/Communication.h" #include "lldb/Core/Connection.h" +#include "lldb/Core/Listener.h" #include "lldb/Core/Log.h" #include "lldb/Core/Timer.h" #include "lldb/Core/Event.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostThread.h" #include "lldb/Host/ThreadLauncher.h" -#include <string.h> using namespace lldb; using namespace lldb_private; @@ -31,54 +33,47 @@ Communication::GetStaticBroadcasterClass () return class_name; } -//---------------------------------------------------------------------- -// Constructor -//---------------------------------------------------------------------- -Communication::Communication(const char *name) : - Broadcaster (NULL, name), - m_connection_sp (), - m_read_thread_enabled (false), - m_read_thread_did_exit (false), - m_bytes(), - m_bytes_mutex (Mutex::eMutexTypeRecursive), - m_write_mutex (Mutex::eMutexTypeNormal), - m_synchronize_mutex (Mutex::eMutexTypeNormal), - m_callback (NULL), - m_callback_baton (NULL), - m_close_on_eof (true) +Communication::Communication(const char *name) + : Broadcaster(nullptr, name), + m_connection_sp(), + m_read_thread_enabled(false), + m_read_thread_did_exit(false), + m_bytes(), + m_bytes_mutex(), + m_write_mutex(), + m_synchronize_mutex(), + m_callback(nullptr), + m_callback_baton(nullptr), + m_close_on_eof(true) { - lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION, - "%p Communication::Communication (name = %s)", - this, name); - - SetEventName (eBroadcastBitDisconnected, "disconnected"); - SetEventName (eBroadcastBitReadThreadGotBytes, "got bytes"); - SetEventName (eBroadcastBitReadThreadDidExit, "read thread did exit"); - SetEventName (eBroadcastBitReadThreadShouldExit, "read thread should exit"); - SetEventName (eBroadcastBitPacketAvailable, "packet available"); - SetEventName (eBroadcastBitNoMorePendingInput, "no more pending input"); - + lldb_private::LogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION, + "%p Communication::Communication (name = %s)", this, name); + + SetEventName(eBroadcastBitDisconnected, "disconnected"); + SetEventName(eBroadcastBitReadThreadGotBytes, "got bytes"); + SetEventName(eBroadcastBitReadThreadDidExit, "read thread did exit"); + SetEventName(eBroadcastBitReadThreadShouldExit, "read thread should exit"); + SetEventName(eBroadcastBitPacketAvailable, "packet available"); + SetEventName(eBroadcastBitNoMorePendingInput, "no more pending input"); + CheckInWithManager(); } -//---------------------------------------------------------------------- -// Destructor -//---------------------------------------------------------------------- Communication::~Communication() { lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION, "%p Communication::~Communication (name = %s)", - this, m_broadcaster_name.AsCString("")); + this, GetBroadcasterName().AsCString()); Clear(); } void Communication::Clear() { - SetReadThreadBytesReceivedCallback (NULL, NULL); - Disconnect (NULL); - StopReadThread (NULL); + SetReadThreadBytesReceivedCallback(nullptr, nullptr); + Disconnect(nullptr); + StopReadThread(nullptr); } ConnectionStatus @@ -89,7 +84,7 @@ Communication::Connect (const char *url, Error *error_ptr) lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Connect (url = %s)", this, url); lldb::ConnectionSP connection_sp (m_connection_sp); - if (connection_sp.get()) + if (connection_sp) return connection_sp->Connect (url, error_ptr); if (error_ptr) error_ptr->SetErrorString("Invalid connection."); @@ -102,7 +97,7 @@ Communication::Disconnect (Error *error_ptr) lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this); lldb::ConnectionSP connection_sp (m_connection_sp); - if (connection_sp.get()) + if (connection_sp) { ConnectionStatus status = connection_sp->Disconnect (error_ptr); // We currently don't protect connection_sp with any mutex for @@ -123,16 +118,14 @@ Communication::Disconnect (Error *error_ptr) bool Communication::IsConnected () const { - lldb::ConnectionSP connection_sp (m_connection_sp); - if (connection_sp.get()) - return connection_sp->IsConnected (); - return false; + lldb::ConnectionSP connection_sp(m_connection_sp); + return (connection_sp ? connection_sp->IsConnected() : false); } bool Communication::HasConnection () const { - return m_connection_sp.get() != NULL; + return m_connection_sp.get() != nullptr; } size_t @@ -156,7 +149,7 @@ Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, Connectio return cached_bytes; } - if (m_connection_sp.get() == NULL) + if (!m_connection_sp) { if (error_ptr) error_ptr->SetErrorString("Invalid connection."); @@ -171,10 +164,10 @@ Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, Connectio timeout_time.OffsetWithMicroSeconds (timeout_usec); } - Listener listener ("Communication::Read"); - listener.StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit); + ListenerSP listener_sp(Listener::MakeListener("Communication::Read")); + listener_sp->StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit); EventSP event_sp; - while (listener.WaitForEvent (timeout_time.IsValid() ? &timeout_time : NULL, event_sp)) + while (listener_sp->WaitForEvent (timeout_time.IsValid() ? &timeout_time : nullptr, event_sp)) { const uint32_t event_type = event_sp->GetType(); if (event_type & eBroadcastBitReadThreadGotBytes) @@ -185,7 +178,7 @@ Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, Connectio if (event_type & eBroadcastBitReadThreadDidExit) { if (GetCloseOnEOF ()) - Disconnect (NULL); + Disconnect(nullptr); break; } } @@ -195,7 +188,7 @@ Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, Connectio // We aren't using a read thread, just read the data synchronously in this // thread. lldb::ConnectionSP connection_sp (m_connection_sp); - if (connection_sp.get()) + if (connection_sp) { return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr); } @@ -206,13 +199,12 @@ Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, Connectio return 0; } - size_t Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr) { lldb::ConnectionSP connection_sp (m_connection_sp); - Mutex::Locker locker(m_write_mutex); + std::lock_guard<std::mutex> guard(m_write_mutex); lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Write (src = %p, src_len = %" PRIu64 ") connection = %p", this, @@ -220,7 +212,7 @@ Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, (uint64_t)src_len, connection_sp.get()); - if (connection_sp.get()) + if (connection_sp) return connection_sp->Write (src, src_len, status, error_ptr); if (error_ptr) @@ -229,7 +221,6 @@ Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, return 0; } - bool Communication::StartReadThread (Error *error_ptr) { @@ -242,9 +233,8 @@ Communication::StartReadThread (Error *error_ptr) lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::StartReadThread ()", this); - char thread_name[1024]; - snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString()); + snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", GetBroadcasterName().AsCString()); m_read_thread_enabled = true; m_read_thread_did_exit = false; @@ -265,7 +255,7 @@ Communication::StopReadThread (Error *error_ptr) m_read_thread_enabled = false; - BroadcastEvent (eBroadcastBitReadThreadShouldExit, NULL); + BroadcastEvent(eBroadcastBitReadThreadShouldExit, nullptr); // error = m_read_thread.Cancel(); @@ -286,12 +276,12 @@ Communication::JoinReadThread (Error *error_ptr) size_t Communication::GetCachedBytes (void *dst, size_t dst_len) { - Mutex::Locker locker(m_bytes_mutex); - if (m_bytes.size() > 0) + std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex); + if (!m_bytes.empty()) { - // If DST is NULL and we have a thread, then return the number + // If DST is nullptr and we have a thread, then return the number // of bytes that are available so the caller can call again - if (dst == NULL) + if (dst == nullptr) return m_bytes.size(); const size_t len = std::min<size_t>(dst_len, m_bytes.size()); @@ -310,7 +300,7 @@ Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broad lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::AppendBytesToCache (src = %p, src_len = %" PRIu64 ", broadcast = %i)", this, bytes, (uint64_t)len, broadcast); - if ((bytes == NULL || len == 0) + if ((bytes == nullptr || len == 0) && (status != lldb::eConnectionStatusEndOfFile)) return; if (m_callback) @@ -318,9 +308,9 @@ Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broad // If the user registered a callback, then call it and do not broadcast m_callback (m_callback_baton, bytes, len); } - else if (bytes != NULL && len > 0) + else if (bytes != nullptr && len > 0) { - Mutex::Locker locker(m_bytes_mutex); + std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex); m_bytes.append ((const char *)bytes, len); if (broadcast) BroadcastEventIfUnique (eBroadcastBitReadThreadGotBytes); @@ -334,10 +324,8 @@ Communication::ReadFromConnection (void *dst, ConnectionStatus &status, Error *error_ptr) { - lldb::ConnectionSP connection_sp (m_connection_sp); - if (connection_sp.get()) - return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr); - return 0; + lldb::ConnectionSP connection_sp(m_connection_sp); + return (connection_sp ? connection_sp->Read(dst, dst_len, timeout_usec, status, error_ptr) : 0); } bool @@ -403,7 +391,7 @@ Communication::ReadThread (lldb::thread_arg_t p) case eConnectionStatusNoConnection: // No connection case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection done = true; - // Fall through... + LLVM_FALLTHROUGH; case eConnectionStatusTimedOut: // Request timed out if (log) error.LogIfError (log, @@ -425,11 +413,8 @@ Communication::ReadThread (lldb::thread_arg_t p) } void -Communication::SetReadThreadBytesReceivedCallback -( - ReadThreadBytesReceived callback, - void *callback_baton -) +Communication::SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, + void *callback_baton) { m_callback = callback; m_callback_baton = callback_baton; @@ -439,11 +424,11 @@ void Communication::SynchronizeWithReadThread () { // Only one thread can do the synchronization dance at a time. - Mutex::Locker locker(m_synchronize_mutex); + std::lock_guard<std::mutex> guard(m_synchronize_mutex); // First start listening for the synchronization event. - Listener listener("Communication::SyncronizeWithReadThread"); - listener.StartListeningForEvents(this, eBroadcastBitNoMorePendingInput); + ListenerSP listener_sp(Listener::MakeListener("Communication::SyncronizeWithReadThread")); + listener_sp->StartListeningForEvents(this, eBroadcastBitNoMorePendingInput); // If the thread is not running, there is no point in synchronizing. if (!m_read_thread_enabled || m_read_thread_did_exit) @@ -454,14 +439,14 @@ Communication::SynchronizeWithReadThread () // Wait for the synchronization event. EventSP event_sp; - listener.WaitForEvent(NULL, event_sp); + listener_sp->WaitForEvent(nullptr, event_sp); } void Communication::SetConnection (Connection *connection) { - Disconnect (NULL); - StopReadThread(NULL); + Disconnect(nullptr); + StopReadThread(nullptr); m_connection_sp.reset(connection); } |