JDevlieghere updated this revision to Diff 414775. JDevlieghere edited the summary of this revision. JDevlieghere added a comment.
Rename utility class to `ThreadSafeStream`. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121500/new/ https://reviews.llvm.org/D121500 Files: lldb/include/lldb/Utility/Stream.h lldb/source/Core/Debugger.cpp Index: lldb/source/Core/Debugger.cpp =================================================================== --- lldb/source/Core/Debugger.cpp +++ lldb/source/Core/Debugger.cpp @@ -733,8 +733,10 @@ : UserID(g_unique_id++), Properties(std::make_shared<OptionValueProperties>()), m_input_file_sp(std::make_shared<NativeFile>(stdin, false)), - m_output_stream_sp(std::make_shared<StreamFile>(stdout, false)), - m_error_stream_sp(std::make_shared<StreamFile>(stderr, false)), + m_output_stream_sp( + std::make_shared<ThreadSafeStream<StreamFile>>(stdout, false)), + m_error_stream_sp( + std::make_shared<ThreadSafeStream<StreamFile>>(stderr, false)), m_input_recorder(nullptr), m_broadcaster_manager_sp(BroadcasterManager::MakeBroadcasterManager()), m_terminal_state(), m_target_list(*this), m_platform_list(), Index: lldb/include/lldb/Utility/Stream.h =================================================================== --- lldb/include/lldb/Utility/Stream.h +++ lldb/include/lldb/Utility/Stream.h @@ -19,6 +19,7 @@ #include <cstdarg> #include <cstddef> #include <cstdint> +#include <mutex> #include <type_traits> namespace lldb_private { @@ -44,7 +45,6 @@ Stream *m_stream; /// Bytes we have written so far when ByteDelta was created. size_t m_start; - public: ByteDelta(Stream &s) : m_stream(&s), m_start(s.GetWrittenBytes()) {} /// Returns the number of bytes written to the given Stream since this @@ -412,6 +412,30 @@ RawOstreamForward m_forwarder; }; +template <typename T, typename M = std::mutex> +class ThreadSafeStream : public T { +public: + using T::T; + ~ThreadSafeStream() = default; + + void Flush() override { + std::lock_guard<M> guard(m_mutex); + T::Flush(); + } + + static_assert(std::is_base_of<Stream, T>::value, + "ThreadSafeStream must be derived from Stream"); + +protected: + size_t WriteImpl(const void *s, size_t length) override { + std::lock_guard<M> guard(m_mutex); + return T::WriteImpl(s, length); + } + +private: + M m_mutex; +}; + /// Output an address value to this stream. /// /// Put an address \a addr out to the stream with optional \a prefix and \a
Index: lldb/source/Core/Debugger.cpp =================================================================== --- lldb/source/Core/Debugger.cpp +++ lldb/source/Core/Debugger.cpp @@ -733,8 +733,10 @@ : UserID(g_unique_id++), Properties(std::make_shared<OptionValueProperties>()), m_input_file_sp(std::make_shared<NativeFile>(stdin, false)), - m_output_stream_sp(std::make_shared<StreamFile>(stdout, false)), - m_error_stream_sp(std::make_shared<StreamFile>(stderr, false)), + m_output_stream_sp( + std::make_shared<ThreadSafeStream<StreamFile>>(stdout, false)), + m_error_stream_sp( + std::make_shared<ThreadSafeStream<StreamFile>>(stderr, false)), m_input_recorder(nullptr), m_broadcaster_manager_sp(BroadcasterManager::MakeBroadcasterManager()), m_terminal_state(), m_target_list(*this), m_platform_list(), Index: lldb/include/lldb/Utility/Stream.h =================================================================== --- lldb/include/lldb/Utility/Stream.h +++ lldb/include/lldb/Utility/Stream.h @@ -19,6 +19,7 @@ #include <cstdarg> #include <cstddef> #include <cstdint> +#include <mutex> #include <type_traits> namespace lldb_private { @@ -44,7 +45,6 @@ Stream *m_stream; /// Bytes we have written so far when ByteDelta was created. size_t m_start; - public: ByteDelta(Stream &s) : m_stream(&s), m_start(s.GetWrittenBytes()) {} /// Returns the number of bytes written to the given Stream since this @@ -412,6 +412,30 @@ RawOstreamForward m_forwarder; }; +template <typename T, typename M = std::mutex> +class ThreadSafeStream : public T { +public: + using T::T; + ~ThreadSafeStream() = default; + + void Flush() override { + std::lock_guard<M> guard(m_mutex); + T::Flush(); + } + + static_assert(std::is_base_of<Stream, T>::value, + "ThreadSafeStream must be derived from Stream"); + +protected: + size_t WriteImpl(const void *s, size_t length) override { + std::lock_guard<M> guard(m_mutex); + return T::WriteImpl(s, length); + } + +private: + M m_mutex; +}; + /// Output an address value to this stream. /// /// Put an address \a addr out to the stream with optional \a prefix and \a
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits