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 //===----------------------------------------------------------------------===//
10 #include "JSONUtils.h"
14 #include <sys/types.h>
27 FifoFile::FifoFile(StringRef path
) : m_path(path
) {}
29 FifoFile::~FifoFile() {
31 unlink(m_path
.c_str());
35 Expected
<std::shared_ptr
<FifoFile
>> CreateFifoFile(StringRef path
) {
37 return createStringError(inconvertibleErrorCode(), "Unimplemented");
39 if (int err
= mkfifo(path
.data(), 0600))
40 return createStringError(std::error_code(err
, std::generic_category()),
41 "Couldn't create fifo file: %s", path
.data());
42 return std::make_shared
<FifoFile
>(path
);
46 FifoFileIO::FifoFileIO(StringRef fifo_file
, StringRef other_endpoint_name
)
47 : m_fifo_file(fifo_file
), m_other_endpoint_name(other_endpoint_name
) {}
49 Expected
<json::Value
> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout
) {
50 // We use a pointer for this future, because otherwise its normal destructor
51 // would wait for the getline to end, rendering the timeout useless.
52 std::optional
<std::string
> line
;
53 std::future
<void> *future
=
54 new std::future
<void>(std::async(std::launch::async
, [&]() {
55 std::ifstream
reader(m_fifo_file
, std::ifstream::in
);
57 std::getline(reader
, buffer
);
61 if (future
->wait_for(timeout
) == std::future_status::timeout
|| !line
)
62 // Indeed this is a leak, but it's intentional. "future" obj destructor
63 // will block on waiting for the worker thread to join. And the worker
64 // thread might be stuck in blocking I/O. Intentionally leaking the obj
65 // as a hack to avoid blocking main thread, and adding annotation to
66 // supress static code inspection warnings
68 // coverity[leaked_storage]
69 return createStringError(inconvertibleErrorCode(),
70 "Timed out trying to get messages from the " +
71 m_other_endpoint_name
);
73 return json::parse(*line
);
76 Error
FifoFileIO::SendJSON(const json::Value
&json
,
77 std::chrono::milliseconds timeout
) {
79 std::future
<void> *future
=
80 new std::future
<void>(std::async(std::launch::async
, [&]() {
81 std::ofstream
writer(m_fifo_file
, std::ofstream::out
);
82 writer
<< JSONToString(json
) << std::endl
;
85 if (future
->wait_for(timeout
) == std::future_status::timeout
|| !done
) {
86 // Indeed this is a leak, but it's intentional. "future" obj destructor will
87 // block on waiting for the worker thread to join. And the worker thread
88 // might be stuck in blocking I/O. Intentionally leaking the obj as a hack
89 // to avoid blocking main thread, and adding annotation to supress static
90 // code inspection warnings"
92 // coverity[leaked_storage]
93 return createStringError(inconvertibleErrorCode(),
94 "Timed out trying to send messages to the " +
95 m_other_endpoint_name
);
98 return Error::success();
101 } // namespace lldb_dap