Wait for data in pipe and read it.
| 185 | |
| 186 | /// Wait for data in pipe and read it. |
| 187 | bool wait(int timeout_ms) |
| 188 | { |
| 189 | while (true) |
| 190 | { |
| 191 | int fd = notification_pipe.fds_rw[0]; |
| 192 | pollfd poll_fd{fd, POLLIN, 0}; |
| 193 | |
| 194 | int poll_res = poll(&poll_fd, 1, timeout_ms); |
| 195 | if (poll_res < 0) |
| 196 | { |
| 197 | if (errno == EINTR) |
| 198 | { |
| 199 | --timeout_ms; /// Quite a hacky way to update timeout. Just to make sure we avoid infinite waiting. |
| 200 | if (timeout_ms == 0) |
| 201 | return false; |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | throw ErrnoException(ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR, "Cannot poll pipe"); |
| 206 | } |
| 207 | if (poll_res == 0) |
| 208 | return false; |
| 209 | |
| 210 | int notification_num = 0; |
| 211 | ssize_t read_res = ::read(fd, ¬ification_num, sizeof(notification_num)); |
| 212 | |
| 213 | if (read_res < 0) |
| 214 | { |
| 215 | if (errno == EINTR) |
| 216 | continue; |
| 217 | |
| 218 | throw ErrnoException(ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR, "Cannot read from pipe"); |
| 219 | } |
| 220 | |
| 221 | if (read_res == sizeof(notification_num)) |
| 222 | { |
| 223 | if (notification_num == sequence_num.load(std::memory_order_relaxed)) |
| 224 | return true; |
| 225 | continue; /// Drain delayed notifications. |
| 226 | } |
| 227 | |
| 228 | throw Exception(ErrorCodes::LOGICAL_ERROR, "Read wrong number of bytes from pipe"); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | using ThreadIdToName = std::unordered_map<UInt64, String, DefaultHash<UInt64>>; |
| 233 |