1311128Sdim//===-- SelectHelper.h ------------------------------------------*- C++ -*-===// 2311128Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6311128Sdim// 7311128Sdim//===----------------------------------------------------------------------===// 8311128Sdim 9311128Sdim#ifndef liblldb_SelectHelper_h_ 10311128Sdim#define liblldb_SelectHelper_h_ 11311128Sdim 12344779Sdim#include "lldb/Utility/Status.h" 13344779Sdim#include "lldb/lldb-types.h" 14311128Sdim 15311128Sdim#include "llvm/ADT/DenseMap.h" 16311128Sdim#include "llvm/ADT/Optional.h" 17311128Sdim 18321369Sdim#include <chrono> 19311128Sdim 20311128Sdimclass SelectHelper { 21311128Sdimpublic: 22311128Sdim // Defaults to infinite wait for select unless you call SetTimeout() 23311128Sdim SelectHelper(); 24311128Sdim 25341825Sdim // Call SetTimeout() before calling SelectHelper::Select() to set the timeout 26341825Sdim // based on the current time + the timeout. This allows multiple calls to 27341825Sdim // SelectHelper::Select() without having to worry about the absolute timeout 28341825Sdim // as this class manages to set the relative timeout correctly. 29311128Sdim void SetTimeout(const std::chrono::microseconds &timeout); 30311128Sdim 31341825Sdim // Call the FDSet*() functions before calling SelectHelper::Select() to set 32341825Sdim // the file descriptors that we will watch for when calling select. This will 33341825Sdim // cause FD_SET() to be called prior to calling select using the "fd" 34341825Sdim // provided. 35311128Sdim void FDSetRead(lldb::socket_t fd); 36311128Sdim void FDSetWrite(lldb::socket_t fd); 37311128Sdim void FDSetError(lldb::socket_t fd); 38311128Sdim 39341825Sdim // Call the FDIsSet*() functions after calling SelectHelper::Select() to 40341825Sdim // check which file descriptors are ready for read/write/error. This will 41341825Sdim // contain the result of FD_ISSET after calling select for a given file 42341825Sdim // descriptor. 43311128Sdim bool FDIsSetRead(lldb::socket_t fd) const; 44311128Sdim bool FDIsSetWrite(lldb::socket_t fd) const; 45311128Sdim bool FDIsSetError(lldb::socket_t fd) const; 46311128Sdim 47341825Sdim // Call the system's select() to wait for descriptors using timeout provided 48341825Sdim // in a call the SelectHelper::SetTimeout(), or infinite wait if no timeout 49341825Sdim // was set. 50321369Sdim lldb_private::Status Select(); 51311128Sdim 52311128Sdimprotected: 53311128Sdim struct FDInfo { 54311128Sdim FDInfo() 55311128Sdim : read_set(false), write_set(false), error_set(false), 56311128Sdim read_is_set(false), write_is_set(false), error_is_set(false) {} 57311128Sdim 58311128Sdim void PrepareForSelect() { 59311128Sdim read_is_set = false; 60311128Sdim write_is_set = false; 61311128Sdim error_is_set = false; 62311128Sdim } 63311128Sdim 64311128Sdim bool read_set : 1, write_set : 1, error_set : 1, read_is_set : 1, 65311128Sdim write_is_set : 1, error_is_set : 1; 66311128Sdim }; 67311128Sdim llvm::DenseMap<lldb::socket_t, FDInfo> m_fd_map; 68311128Sdim llvm::Optional<std::chrono::steady_clock::time_point> m_end_time; 69311128Sdim}; 70311128Sdim 71311128Sdim#endif // liblldb_SelectHelper_h_ 72