xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Host/posix/PipePosix.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===-- PipePosix.h ---------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_HOST_POSIX_PIPEPOSIX_H
10 #define LLDB_HOST_POSIX_PIPEPOSIX_H
11 #include "lldb/Host/PipeBase.h"
12 #include <mutex>
13 
14 namespace lldb_private {
15 
16 /// \class PipePosix PipePosix.h "lldb/Host/posix/PipePosix.h"
17 /// A posix-based implementation of Pipe, a class that abtracts
18 ///        unix style pipes.
19 ///
20 /// A class that abstracts the LLDB core from host pipe functionality.
21 class PipePosix : public PipeBase {
22 public:
23   static int kInvalidDescriptor;
24 
25   PipePosix();
26   PipePosix(lldb::pipe_t read, lldb::pipe_t write);
27   PipePosix(const PipePosix &) = delete;
28   PipePosix(PipePosix &&pipe_posix);
29   PipePosix &operator=(const PipePosix &) = delete;
30   PipePosix &operator=(PipePosix &&pipe_posix);
31 
32   ~PipePosix() override;
33 
34   Status CreateNew(bool child_process_inherit) override;
35   Status CreateNew(llvm::StringRef name, bool child_process_inherit) override;
36   Status CreateWithUniqueName(llvm::StringRef prefix,
37                               bool child_process_inherit,
38                               llvm::SmallVectorImpl<char> &name) override;
39   Status OpenAsReader(llvm::StringRef name,
40                       bool child_process_inherit) override;
41   Status
42   OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit,
43                           const std::chrono::microseconds &timeout) override;
44 
45   bool CanRead() const override;
46   bool CanWrite() const override;
47 
48   lldb::pipe_t GetReadPipe() const override {
49     return lldb::pipe_t(GetReadFileDescriptor());
50   }
51   lldb::pipe_t GetWritePipe() const override {
52     return lldb::pipe_t(GetWriteFileDescriptor());
53   }
54 
55   int GetReadFileDescriptor() const override;
56   int GetWriteFileDescriptor() const override;
57   int ReleaseReadFileDescriptor() override;
58   int ReleaseWriteFileDescriptor() override;
59   void CloseReadFileDescriptor() override;
60   void CloseWriteFileDescriptor() override;
61 
62   // Close both descriptors
63   void Close() override;
64 
65   Status Delete(llvm::StringRef name) override;
66 
67   Status Write(const void *buf, size_t size, size_t &bytes_written) override;
68   Status ReadWithTimeout(void *buf, size_t size,
69                          const std::chrono::microseconds &timeout,
70                          size_t &bytes_read) override;
71 
72 private:
73   bool CanReadUnlocked() const;
74   bool CanWriteUnlocked() const;
75 
76   int GetReadFileDescriptorUnlocked() const;
77   int GetWriteFileDescriptorUnlocked() const;
78   int ReleaseReadFileDescriptorUnlocked();
79   int ReleaseWriteFileDescriptorUnlocked();
80   void CloseReadFileDescriptorUnlocked();
81   void CloseWriteFileDescriptorUnlocked();
82   void CloseUnlocked();
83 
84   int m_fds[2];
85 
86   /// Mutexes for m_fds;
87   mutable std::mutex m_read_mutex;
88   mutable std::mutex m_write_mutex;
89 };
90 
91 } // namespace lldb_private
92 
93 #endif // LLDB_HOST_POSIX_PIPEPOSIX_H
94