1 //===-- FifoFiles.cpp -------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
13 #include <sys/types.h>
23 #include "llvm/Support/FileSystem.h"
25 #include "lldb/lldb-defines.h"
29 namespace lldb_vscode
{
31 FifoFile::FifoFile(StringRef path
) : m_path(path
) {}
33 FifoFile::~FifoFile() {
35 unlink(m_path
.c_str());
39 Expected
<std::shared_ptr
<FifoFile
>> CreateFifoFile(StringRef path
) {
41 return createStringError(inconvertibleErrorCode(), "Unimplemented");
43 if (int err
= mkfifo(path
.data(), 0600))
44 return createStringError(std::error_code(err
, std::generic_category()),
45 "Couldn't create fifo file: %s", path
.data());
46 return std::make_shared
<FifoFile
>(path
);
50 FifoFileIO::FifoFileIO(StringRef fifo_file
, StringRef other_endpoint_name
)
51 : m_fifo_file(fifo_file
), m_other_endpoint_name(other_endpoint_name
) {}
53 Expected
<json::Value
> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout
) {
54 // We use a pointer for this future, because otherwise its normal destructor
55 // would wait for the getline to end, rendering the timeout useless.
56 std::optional
<std::string
> line
;
57 std::future
<void> *future
=
58 new std::future
<void>(std::async(std::launch::async
, [&]() {
59 std::ifstream
reader(m_fifo_file
, std::ifstream::in
);
61 std::getline(reader
, buffer
);
65 if (future
->wait_for(timeout
) == std::future_status::timeout
|| !line
)
66 // Indeed this is a leak, but it's intentional. "future" obj destructor
67 // will block on waiting for the worker thread to join. And the worker
68 // thread might be stuck in blocking I/O. Intentionally leaking the obj
69 // as a hack to avoid blocking main thread, and adding annotation to
70 // supress static code inspection warnings
72 // coverity[leaked_storage]
73 return createStringError(inconvertibleErrorCode(),
74 "Timed out trying to get messages from the " +
75 m_other_endpoint_name
);
77 return json::parse(*line
);
80 Error
FifoFileIO::SendJSON(const json::Value
&json
,
81 std::chrono::milliseconds timeout
) {
83 std::future
<void> *future
=
84 new std::future
<void>(std::async(std::launch::async
, [&]() {
85 std::ofstream
writer(m_fifo_file
, std::ofstream::out
);
86 writer
<< JSONToString(json
) << std::endl
;
89 if (future
->wait_for(timeout
) == std::future_status::timeout
|| !done
) {
90 // Indeed this is a leak, but it's intentional. "future" obj destructor will
91 // block on waiting for the worker thread to join. And the worker thread
92 // might be stuck in blocking I/O. Intentionally leaking the obj as a hack
93 // to avoid blocking main thread, and adding annotation to supress static
94 // code inspection warnings"
96 // coverity[leaked_storage]
97 return createStringError(inconvertibleErrorCode(),
98 "Timed out trying to send messages to the " +
99 m_other_endpoint_name
);
102 return Error::success();
105 } // namespace lldb_vscode