Skip to content

Commit 32dcaa9

Browse files
committed
msg/async/Event{Poll,Epoll}: move timeout calculation to Timeout.h
This is duplicate code, and it's buggy, but I want to fix only one copy. Signed-off-by: Max Kellermann <[email protected]>
1 parent ee6523b commit 32dcaa9

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/msg/async/EventEpoll.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "common/errno.h"
1818
#include <fcntl.h>
1919
#include "EventEpoll.h"
20+
#include "Timeout.h"
2021

2122
#define dout_subsys ceph_subsys_ms
2223

@@ -120,8 +121,7 @@ int EpollDriver::event_wait(std::vector<FiredFileEvent> &fired_events, struct ti
120121
{
121122
int retval, numevents = 0;
122123

123-
retval = epoll_wait(epfd, events, nevent,
124-
tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
124+
retval = epoll_wait(epfd, events, nevent, timeout_to_milliseconds(tvp));
125125
if (retval > 0) {
126126
numevents = retval;
127127
fired_events.resize(numevents);

src/msg/async/EventPoll.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "common/errno.h"
1717
#include "EventPoll.h"
18+
#include "Timeout.h"
1819

1920
#include <unistd.h>
2021
#define dout_subsys ceph_subsys_ms
@@ -161,11 +162,9 @@ int PollDriver::event_wait(std::vector<FiredFileEvent> &fired_events,
161162
struct timeval *tvp) {
162163
int retval, numevents = 0;
163164
#ifdef _WIN32
164-
retval = WSAPoll(pfds, max_pfds,
165-
tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
165+
retval = WSAPoll(pfds, max_pfds, timeout_to_milliseconds(tvp));
166166
#else
167-
retval = poll(pfds, max_pfds,
168-
tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
167+
retval = poll(pfds, max_pfds, timeout_to_milliseconds(tvp));
169168
#endif
170169
if (retval > 0) {
171170
for (int j = 0; j < max_pfds; j++) {

src/msg/async/Timeout.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2+
// vim: ts=8 sw=2 smarttab
3+
/*
4+
* Ceph - scalable distributed file system
5+
*
6+
* Copyright (C) 2024 IONOS SE
7+
*
8+
* Author: Max Kellermann <[email protected]>
9+
*
10+
* This is free software; you can redistribute it and/or
11+
* modify it under the terms of the GNU Lesser General Public
12+
* License version 2.1, as published by the Free Software
13+
* Foundation. See file COPYING.
14+
*
15+
*/
16+
17+
#ifndef CEPH_MSG_TIMEOUT_H
18+
#define CEPH_MSG_TIMEOUT_H
19+
20+
#include <time.h> // for struct timeval
21+
22+
/**
23+
* Convert the given `struct timeval` to milliseconds.
24+
*
25+
* This is supposed to be used as timeout parameter to system calls
26+
* such as poll() and epoll_wait().
27+
*/
28+
constexpr int
29+
timeout_to_milliseconds(const struct timeval &tv) noexcept
30+
{
31+
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
32+
}
33+
34+
/**
35+
* This overload makes the timeout optional; on nullptr, it returns
36+
* -1.
37+
*/
38+
constexpr int
39+
timeout_to_milliseconds(const struct timeval *tv) noexcept
40+
{
41+
return tv != nullptr ? timeout_to_milliseconds(*tv) : -1;
42+
}
43+
44+
#endif

0 commit comments

Comments
 (0)