LLDB  mainline
SelectHelper.h
Go to the documentation of this file.
1 //===-- SelectHelper.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_UTILITY_SELECTHELPER_H
10 #define LLDB_UTILITY_SELECTHELPER_H
11 
12 #include "lldb/Utility/Status.h"
13 #include "lldb/lldb-types.h"
14 
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Optional.h"
17 
18 #include <chrono>
19 
20 class SelectHelper {
21 public:
22  // Defaults to infinite wait for select unless you call SetTimeout()
23  SelectHelper();
24 
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);
30 
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"
34  // provided.
35  void FDSetRead(lldb::socket_t fd);
36  void FDSetWrite(lldb::socket_t fd);
37  void FDSetError(lldb::socket_t fd);
38 
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
42  // descriptor.
43  bool FDIsSetRead(lldb::socket_t fd) const;
44  bool FDIsSetWrite(lldb::socket_t fd) const;
45  bool FDIsSetError(lldb::socket_t fd) const;
46 
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
49  // was set.
51 
52 protected:
53  struct FDInfo {
55  : read_set(false), write_set(false), error_set(false),
56  read_is_set(false), write_is_set(false), error_is_set(false) {}
57 
59  read_is_set = false;
60  write_is_set = false;
61  error_is_set = false;
62  }
63 
64  bool read_set : 1, write_set : 1, error_set : 1, read_is_set : 1,
66  };
67  llvm::DenseMap<lldb::socket_t, FDInfo> m_fd_map;
68  llvm::Optional<std::chrono::steady_clock::time_point> m_end_time;
69 };
70 
71 #endif // LLDB_UTILITY_SELECTHELPER_H
SelectHelper::FDInfo::read_is_set
bool read_is_set
Definition: SelectHelper.h:64
SelectHelper::FDInfo::FDInfo
FDInfo()
Definition: SelectHelper.h:54
SelectHelper::FDInfo::write_is_set
bool write_is_set
Definition: SelectHelper.h:65
SelectHelper::FDIsSetRead
bool FDIsSetRead(lldb::socket_t fd) const
Definition: SelectHelper.cpp:61
SelectHelper
Definition: SelectHelper.h:20
SelectHelper::Select
lldb_private::Status Select()
Definition: SelectHelper.cpp:93
SelectHelper::SetTimeout
void SetTimeout(const std::chrono::microseconds &timeout)
Definition: SelectHelper.cpp:44
SelectHelper::FDSetError
void FDSetError(lldb::socket_t fd)
Definition: SelectHelper.cpp:57
SelectHelper::FDInfo
Definition: SelectHelper.h:53
SelectHelper::m_fd_map
llvm::DenseMap< lldb::socket_t, FDInfo > m_fd_map
Definition: SelectHelper.h:67
SelectHelper::FDSetWrite
void FDSetWrite(lldb::socket_t fd)
Definition: SelectHelper.cpp:53
lldb::socket_t
int socket_t
Definition: lldb-types.h:60
SelectHelper::FDIsSetError
bool FDIsSetError(lldb::socket_t fd) const
Definition: SelectHelper.cpp:77
SelectHelper::FDSetRead
void FDSetRead(lldb::socket_t fd)
Definition: SelectHelper.cpp:49
SelectHelper::FDInfo::error_set
bool error_set
Definition: SelectHelper.h:64
SelectHelper::FDIsSetWrite
bool FDIsSetWrite(lldb::socket_t fd) const
Definition: SelectHelper.cpp:69
SelectHelper::FDInfo::PrepareForSelect
void PrepareForSelect()
Definition: SelectHelper.h:58
lldb-types.h
lldb_private::Status
Definition: Status.h:44
SelectHelper::FDInfo::read_set
bool read_set
Definition: SelectHelper.h:64
SelectHelper::m_end_time
llvm::Optional< std::chrono::steady_clock::time_point > m_end_time
Definition: SelectHelper.h:68
SelectHelper::FDInfo::write_set
bool write_set
Definition: SelectHelper.h:64
SelectHelper::SelectHelper
SelectHelper()
Definition: SelectHelper.cpp:39
Status.h
SelectHelper::FDInfo::error_is_set
bool error_is_set
Definition: SelectHelper.h:65