1 //===-- SelectHelper.h ------------------------------------------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_SelectHelper_h_
10 #define liblldb_SelectHelper_h_
12 #include "lldb/Utility/Status.h"
13 #include "lldb/lldb-types.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Optional.h"
22 // Defaults to infinite wait for select unless you call SetTimeout()
25 // Call SetTimeout() before calling SelectHelper::Select() to set the timeout
26 // based on the current time + the timeout. This allows multiple calls to
27 // SelectHelper::Select() without having to worry about the absolute timeout
28 // as this class manages to set the relative timeout correctly.
29 void SetTimeout(const std::chrono::microseconds &timeout);
31 // Call the FDSet*() functions before calling SelectHelper::Select() to set
32 // the file descriptors that we will watch for when calling select. This will
33 // cause FD_SET() to be called prior to calling select using the "fd"
35 void FDSetRead(lldb::socket_t fd);
36 void FDSetWrite(lldb::socket_t fd);
37 void FDSetError(lldb::socket_t fd);
39 // Call the FDIsSet*() functions after calling SelectHelper::Select() to
40 // check which file descriptors are ready for read/write/error. This will
41 // contain the result of FD_ISSET after calling select for a given file
43 bool FDIsSetRead(lldb::socket_t fd) const;
44 bool FDIsSetWrite(lldb::socket_t fd) const;
45 bool FDIsSetError(lldb::socket_t fd) const;
47 // Call the system's select() to wait for descriptors using timeout provided
48 // in a call the SelectHelper::SetTimeout(), or infinite wait if no timeout
50 lldb_private::Status Select();
55 : read_set(false), write_set(false), error_set(false),
56 read_is_set(false), write_is_set(false), error_is_set(false) {}
58 void PrepareForSelect() {
64 bool read_set : 1, write_set : 1, error_set : 1, read_is_set : 1,
65 write_is_set : 1, error_is_set : 1;
67 llvm::DenseMap<lldb::socket_t, FDInfo> m_fd_map;
68 llvm::Optional<std::chrono::steady_clock::time_point> m_end_time;
71 #endif // liblldb_SelectHelper_h_