Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance: Fix EventScheduler::timeRemaining() comment #1916

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ EventScheduler::timeRemaining() const
if (tasks->when <= current_dtime) // we are on time or late
return 0; // fire the event ASAP

const double diff = tasks->when - current_dtime; // microseconds
const double diff = tasks->when - current_dtime; // seconds
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not change the type of diff variable to auto because this code secretly relies on diff type being an arithmetic type. For example, when using std::chrono::duration for diff, 1000*diff expression below will compile but will not convert seconds to milliseconds as intended. Event::when type, this whole method (including its return type), and method callers should be reworked using std::chrono, but this is a minimal PR fixing (my 2013 commit aa14d75) bug that wasted 30 minutes of my time.

Said that, if you insist on auto upgrade, just apply this suggestion:

Suggested change
const double diff = tasks->when - current_dtime; // seconds
const auto diff = tasks->when - current_dtime; // seconds

// Round UP: If we come back a nanosecond earlier, we will wait again!
const int timeLeft = static_cast<int>(ceil(1000*diff)); // milliseconds
// Avoid hot idle: A series of rapid select() calls with zero timeout.
Expand Down