augusto2112 updated this revision to Diff 549454. augusto2112 added a comment.
Lock earlier in operator= Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157654/new/ https://reviews.llvm.org/D157654 Files: lldb/include/lldb/Host/posix/PipePosix.h lldb/source/Host/posix/PipePosix.cpp
Index: lldb/source/Host/posix/PipePosix.cpp =================================================================== --- lldb/source/Host/posix/PipePosix.cpp +++ lldb/source/Host/posix/PipePosix.cpp @@ -65,16 +65,19 @@ pipe_posix.ReleaseWriteFileDescriptor()} {} PipePosix &PipePosix::operator=(PipePosix &&pipe_posix) { + std::scoped_lock guard(m_lock, pipe_posix.m_lock); + PipeBase::operator=(std::move(pipe_posix)); - m_fds[READ] = pipe_posix.ReleaseReadFileDescriptor(); - m_fds[WRITE] = pipe_posix.ReleaseWriteFileDescriptor(); + m_fds[READ] = pipe_posix.ReleaseReadFileDescriptorUnlocked(); + m_fds[WRITE] = pipe_posix.ReleaseWriteFileDescriptorUnlocked(); return *this; } PipePosix::~PipePosix() { Close(); } Status PipePosix::CreateNew(bool child_processes_inherit) { - if (CanRead() || CanWrite()) + std::lock_guard<std::mutex> guard(m_lock); + if (CanReadUnlocked() || CanWriteUnlocked()) return Status(EINVAL, eErrorTypePOSIX); Status error; @@ -87,7 +90,7 @@ if (!child_processes_inherit) { if (!SetCloexecFlag(m_fds[0]) || !SetCloexecFlag(m_fds[1])) { error.SetErrorToErrno(); - Close(); + CloseUnlocked(); return error; } } @@ -103,7 +106,8 @@ } Status PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit) { - if (CanRead() || CanWrite()) + std::lock_guard<std::mutex> guard(m_lock); + if (CanReadUnlocked() || CanWriteUnlocked()) return Status("Pipe is already opened"); Status error; @@ -140,7 +144,8 @@ Status PipePosix::OpenAsReader(llvm::StringRef name, bool child_process_inherit) { - if (CanRead() || CanWrite()) + std::lock_guard<std::mutex> guard(m_lock); + if (CanReadUnlocked() || CanWriteUnlocked()) return Status("Pipe is already opened"); int flags = O_RDONLY | O_NONBLOCK; @@ -161,7 +166,8 @@ PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) { - if (CanRead() || CanWrite()) + std::lock_guard<std::mutex> guard(m_lock); + if (CanReadUnlocked() || CanWriteUnlocked()) return Status("Pipe is already opened"); int flags = O_WRONLY | O_NONBLOCK; @@ -171,7 +177,7 @@ using namespace std::chrono; const auto finish_time = Now() + timeout; - while (!CanWrite()) { + while (!CanWriteUnlocked()) { if (timeout != microseconds::zero()) { const auto dur = duration_cast<microseconds>(finish_time - Now()).count(); if (dur <= 0) @@ -196,25 +202,54 @@ return Status(); } -int PipePosix::GetReadFileDescriptor() const { return m_fds[READ]; } +int PipePosix::GetReadFileDescriptor() const { + std::lock_guard<std::mutex> guard(m_lock); + return GetReadFileDescriptorUnlocked(); +} + +int PipePosix::GetReadFileDescriptorUnlocked() const { + return m_fds[READ]; +} + +int PipePosix::GetWriteFileDescriptor() const { + std::lock_guard<std::mutex> guard(m_lock); + return GetWriteFileDescriptorUnlocked(); +} -int PipePosix::GetWriteFileDescriptor() const { return m_fds[WRITE]; } +int PipePosix::GetWriteFileDescriptorUnlocked() const { + return m_fds[WRITE]; +} int PipePosix::ReleaseReadFileDescriptor() { + std::lock_guard<std::mutex> guard(m_lock); + return ReleaseReadFileDescriptorUnlocked(); +} + +int PipePosix::ReleaseReadFileDescriptorUnlocked() { const int fd = m_fds[READ]; m_fds[READ] = PipePosix::kInvalidDescriptor; return fd; } int PipePosix::ReleaseWriteFileDescriptor() { + std::lock_guard<std::mutex> guard(m_lock); + return ReleaseWriteFileDescriptorUnlocked(); +} + +int PipePosix::ReleaseWriteFileDescriptorUnlocked() { const int fd = m_fds[WRITE]; m_fds[WRITE] = PipePosix::kInvalidDescriptor; return fd; } void PipePosix::Close() { - CloseReadFileDescriptor(); - CloseWriteFileDescriptor(); + std::lock_guard<std::mutex> guard(m_lock); + CloseUnlocked(); +} + +void PipePosix::CloseUnlocked() { + CloseReadFileDescriptorUnlocked(); + CloseWriteFileDescriptorUnlocked(); } Status PipePosix::Delete(llvm::StringRef name) { @@ -222,22 +257,41 @@ } bool PipePosix::CanRead() const { + std::lock_guard<std::mutex> guard(m_lock); + return CanReadUnlocked(); +} + +bool PipePosix::CanReadUnlocked() const { return m_fds[READ] != PipePosix::kInvalidDescriptor; } bool PipePosix::CanWrite() const { + std::lock_guard<std::mutex> guard(m_lock); + return CanWriteUnlocked(); +} + +bool PipePosix::CanWriteUnlocked() const { return m_fds[WRITE] != PipePosix::kInvalidDescriptor; } void PipePosix::CloseReadFileDescriptor() { - if (CanRead()) { + std::lock_guard<std::mutex> guard(m_lock); + CloseReadFileDescriptorUnlocked(); +} +void PipePosix::CloseReadFileDescriptorUnlocked() { + if (CanReadUnlocked()) { close(m_fds[READ]); m_fds[READ] = PipePosix::kInvalidDescriptor; } } void PipePosix::CloseWriteFileDescriptor() { - if (CanWrite()) { + std::lock_guard<std::mutex> guard(m_lock); + CloseWriteFileDescriptorUnlocked(); +} + +void PipePosix::CloseWriteFileDescriptorUnlocked() { + if (CanWriteUnlocked()) { close(m_fds[WRITE]); m_fds[WRITE] = PipePosix::kInvalidDescriptor; } @@ -246,11 +300,12 @@ Status PipePosix::ReadWithTimeout(void *buf, size_t size, const std::chrono::microseconds &timeout, size_t &bytes_read) { + std::lock_guard<std::mutex> guard(m_lock); bytes_read = 0; - if (!CanRead()) + if (!CanReadUnlocked()) return Status(EINVAL, eErrorTypePOSIX); - const int fd = GetReadFileDescriptor(); + const int fd = GetReadFileDescriptorUnlocked(); SelectHelper select_helper; select_helper.SetTimeout(timeout); @@ -278,11 +333,12 @@ } Status PipePosix::Write(const void *buf, size_t size, size_t &bytes_written) { + std::lock_guard<std::mutex> guard(m_lock); bytes_written = 0; - if (!CanWrite()) + if (!CanWriteUnlocked()) return Status(EINVAL, eErrorTypePOSIX); - const int fd = GetWriteFileDescriptor(); + const int fd = GetWriteFileDescriptorUnlocked(); SelectHelper select_helper; select_helper.SetTimeout(std::chrono::seconds(0)); select_helper.FDSetWrite(fd); Index: lldb/include/lldb/Host/posix/PipePosix.h =================================================================== --- lldb/include/lldb/Host/posix/PipePosix.h +++ lldb/include/lldb/Host/posix/PipePosix.h @@ -9,8 +9,8 @@ #ifndef LLDB_HOST_POSIX_PIPEPOSIX_H #define LLDB_HOST_POSIX_PIPEPOSIX_H #if defined(__cplusplus) - #include "lldb/Host/PipeBase.h" +#include <mutex> namespace lldb_private { @@ -71,7 +71,21 @@ size_t &bytes_read) override; private: + bool CanReadUnlocked() const; + bool CanWriteUnlocked() const; + + int GetReadFileDescriptorUnlocked() const; + int GetWriteFileDescriptorUnlocked() const; + int ReleaseReadFileDescriptorUnlocked(); + int ReleaseWriteFileDescriptorUnlocked(); + void CloseReadFileDescriptorUnlocked(); + void CloseWriteFileDescriptorUnlocked(); + void CloseUnlocked(); + int m_fds[2]; + + /// Lock for m_fds; + mutable std::mutex m_lock; }; } // namespace lldb_private
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits