Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:58

0001 //===-- SelectHelper.h ------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_SELECTHELPER_H
0010 #define LLDB_UTILITY_SELECTHELPER_H
0011 
0012 #include "lldb/Utility/Status.h"
0013 #include "lldb/lldb-types.h"
0014 
0015 #include "llvm/ADT/DenseMap.h"
0016 
0017 #include <chrono>
0018 #include <optional>
0019 
0020 class SelectHelper {
0021 public:
0022   // Defaults to infinite wait for select unless you call SetTimeout()
0023   SelectHelper();
0024 
0025   // Call SetTimeout() before calling SelectHelper::Select() to set the timeout
0026   // based on the current time + the timeout. This allows multiple calls to
0027   // SelectHelper::Select() without having to worry about the absolute timeout
0028   // as this class manages to set the relative timeout correctly.
0029   void SetTimeout(const std::chrono::microseconds &timeout);
0030 
0031   // Call the FDSet*() functions before calling SelectHelper::Select() to set
0032   // the file descriptors that we will watch for when calling select. This will
0033   // cause FD_SET() to be called prior to calling select using the "fd"
0034   // provided.
0035   void FDSetRead(lldb::socket_t fd);
0036   void FDSetWrite(lldb::socket_t fd);
0037   void FDSetError(lldb::socket_t fd);
0038 
0039   // Call the FDIsSet*() functions after calling SelectHelper::Select() to
0040   // check which file descriptors are ready for read/write/error. This will
0041   // contain the result of FD_ISSET after calling select for a given file
0042   // descriptor.
0043   bool FDIsSetRead(lldb::socket_t fd) const;
0044   bool FDIsSetWrite(lldb::socket_t fd) const;
0045   bool FDIsSetError(lldb::socket_t fd) const;
0046 
0047   // Call the system's select() to wait for descriptors using timeout provided
0048   // in a call the SelectHelper::SetTimeout(), or infinite wait if no timeout
0049   // was set.
0050   lldb_private::Status Select();
0051 
0052 protected:
0053   struct FDInfo {
0054     FDInfo()
0055         : read_set(false), write_set(false), error_set(false),
0056           read_is_set(false), write_is_set(false), error_is_set(false) {}
0057 
0058     void PrepareForSelect() {
0059       read_is_set = false;
0060       write_is_set = false;
0061       error_is_set = false;
0062     }
0063 
0064     bool read_set : 1, write_set : 1, error_set : 1, read_is_set : 1,
0065         write_is_set : 1, error_is_set : 1;
0066   };
0067   llvm::DenseMap<lldb::socket_t, FDInfo> m_fd_map;
0068   std::optional<std::chrono::steady_clock::time_point> m_end_time;
0069 };
0070 
0071 #endif // LLDB_UTILITY_SELECTHELPER_H