SelectHelper.h revision 344779
1//===-- SelectHelper.h ------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_SelectHelper_h_
11#define liblldb_SelectHelper_h_
12
13#include "lldb/Utility/Status.h"
14#include "lldb/lldb-types.h"
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/Optional.h"
18
19#include <chrono>
20
21class SelectHelper {
22public:
23  // Defaults to infinite wait for select unless you call SetTimeout()
24  SelectHelper();
25
26  // Call SetTimeout() before calling SelectHelper::Select() to set the timeout
27  // based on the current time + the timeout. This allows multiple calls to
28  // SelectHelper::Select() without having to worry about the absolute timeout
29  // as this class manages to set the relative timeout correctly.
30  void SetTimeout(const std::chrono::microseconds &timeout);
31
32  // Call the FDSet*() functions before calling SelectHelper::Select() to set
33  // the file descriptors that we will watch for when calling select. This will
34  // cause FD_SET() to be called prior to calling select using the "fd"
35  // provided.
36  void FDSetRead(lldb::socket_t fd);
37  void FDSetWrite(lldb::socket_t fd);
38  void FDSetError(lldb::socket_t fd);
39
40  // Call the FDIsSet*() functions after calling SelectHelper::Select() to
41  // check which file descriptors are ready for read/write/error. This will
42  // contain the result of FD_ISSET after calling select for a given file
43  // descriptor.
44  bool FDIsSetRead(lldb::socket_t fd) const;
45  bool FDIsSetWrite(lldb::socket_t fd) const;
46  bool FDIsSetError(lldb::socket_t fd) const;
47
48  // Call the system's select() to wait for descriptors using timeout provided
49  // in a call the SelectHelper::SetTimeout(), or infinite wait if no timeout
50  // was set.
51  lldb_private::Status Select();
52
53protected:
54  struct FDInfo {
55    FDInfo()
56        : read_set(false), write_set(false), error_set(false),
57          read_is_set(false), write_is_set(false), error_is_set(false) {}
58
59    void PrepareForSelect() {
60      read_is_set = false;
61      write_is_set = false;
62      error_is_set = false;
63    }
64
65    bool read_set : 1, write_set : 1, error_set : 1, read_is_set : 1,
66        write_is_set : 1, error_is_set : 1;
67  };
68  llvm::DenseMap<lldb::socket_t, FDInfo> m_fd_map;
69  llvm::Optional<std::chrono::steady_clock::time_point> m_end_time;
70};
71
72#endif // liblldb_SelectHelper_h_
73