SelectHelper.h revision 314564
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// C Includes
14// C++ Includes
15#include <chrono>
16
17// Other libraries and framework includes
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/Optional.h"
20
21// Project includes
22#include "lldb/lldb-forward.h"
23
24class SelectHelper {
25public:
26  // Defaults to infinite wait for select unless you call SetTimeout()
27  SelectHelper();
28
29  // Call SetTimeout() before calling SelectHelper::Select() to set the
30  // timeout based on the current time + the timeout. This allows multiple
31  // calls to SelectHelper::Select() without having to worry about the
32  // absolute timeout as this class manages to set the relative timeout
33  // correctly.
34  void SetTimeout(const std::chrono::microseconds &timeout);
35
36  // Call the FDSet*() functions before calling SelectHelper::Select() to
37  // set the file descriptors that we will watch for when calling
38  // select. This will cause FD_SET() to be called prior to calling select
39  // using the "fd" provided.
40  void FDSetRead(lldb::socket_t fd);
41  void FDSetWrite(lldb::socket_t fd);
42  void FDSetError(lldb::socket_t fd);
43
44  // Call the FDIsSet*() functions after calling SelectHelper::Select()
45  // to check which file descriptors are ready for read/write/error. This
46  // will contain the result of FD_ISSET after calling select for a given
47  // file descriptor.
48  bool FDIsSetRead(lldb::socket_t fd) const;
49  bool FDIsSetWrite(lldb::socket_t fd) const;
50  bool FDIsSetError(lldb::socket_t fd) const;
51
52  // Call the system's select() to wait for descriptors using
53  // timeout provided in a call the SelectHelper::SetTimeout(),
54  // or infinite wait if no timeout was set.
55  lldb_private::Error Select();
56
57protected:
58  struct FDInfo {
59    FDInfo()
60        : read_set(false), write_set(false), error_set(false),
61          read_is_set(false), write_is_set(false), error_is_set(false) {}
62
63    void PrepareForSelect() {
64      read_is_set = false;
65      write_is_set = false;
66      error_is_set = false;
67    }
68
69    bool read_set : 1, write_set : 1, error_set : 1, read_is_set : 1,
70        write_is_set : 1, error_is_set : 1;
71  };
72  llvm::DenseMap<lldb::socket_t, FDInfo> m_fd_map;
73  llvm::Optional<std::chrono::steady_clock::time_point> m_end_time;
74};
75
76#endif // liblldb_SelectHelper_h_
77