AK: Make timeval_add() and timeval_sub() take references.

This commit is contained in:
Andreas Kling 2019-06-06 18:02:28 +02:00
parent bf905225e7
commit 29a9430246
Notes: sideshowbarker 2024-07-19 13:43:30 +09:00
4 changed files with 19 additions and 18 deletions

View file

@ -3,25 +3,28 @@
namespace AK {
template<typename TimevalType>
inline void timeval_sub(const TimevalType* a, const TimevalType* b, TimevalType* result)
inline void timeval_sub(const TimevalType& a, const TimevalType& b, TimevalType& result)
{
result->tv_sec = a->tv_sec - b->tv_sec;
result->tv_usec = a->tv_usec - b->tv_usec;
if (result->tv_usec < 0) {
--result->tv_sec;
result->tv_usec += 1000000;
result.tv_sec = a.tv_sec - b.tv_sec;
result.tv_usec = a.tv_usec - b.tv_usec;
if (result.tv_usec < 0) {
--result.tv_sec;
result.tv_usec += 1000000;
}
}
template<typename TimevalType>
inline void timeval_add(const TimevalType* a, const TimevalType* b, TimevalType* result)
inline void timeval_add(const TimevalType& a, const TimevalType& b, TimevalType& result)
{
result->tv_sec = a->tv_sec + b->tv_sec;
result->tv_usec = a->tv_usec + b->tv_usec;
if (result->tv_usec > 1000000) {
++result->tv_sec;
result->tv_usec -= 1000000;
result.tv_sec = a.tv_sec + b.tv_sec;
result.tv_usec = a.tv_usec + b.tv_usec;
if (result.tv_usec > 1000000) {
++result.tv_sec;
result.tv_usec -= 1000000;
}
}
}
using AK::timeval_add;
using AK::timeval_sub;

View file

@ -1767,8 +1767,7 @@ int Process::sys$select(const Syscall::SC_select_params* params)
return -EINVAL;
if (params->timeout && (params->timeout->tv_sec || params->timeout->tv_usec)) {
auto now = kgettimeofday();
AK::timeval_add(&now, params->timeout, &current->m_select_timeout);
timeval_add(kgettimeofday(), *params->timeout, current->m_select_timeout);
current->m_select_has_timeout = true;
} else {
current->m_select_has_timeout = false;
@ -1842,8 +1841,7 @@ int Process::sys$poll(pollfd* fds, int nfds, int timeout)
timeout -= 1000;
}
tvtimeout.tv_usec = timeout * 1000;
auto now = kgettimeofday();
AK::timeval_add(&now, &tvtimeout, &current->m_select_timeout);
timeval_add(kgettimeofday(), tvtimeout, current->m_select_timeout);
current->m_select_has_timeout = true;
} else {
current->m_select_has_timeout = false;

View file

@ -15,6 +15,6 @@ int CElapsedTimer::elapsed() const
struct timeval now;
gettimeofday(&now, nullptr);
struct timeval diff;
AK::timeval_sub(&now, &m_start_time, &diff);
timeval_sub(now, m_start_time, diff);
return diff.tv_sec * 1000 + diff.tv_usec / 1000;
}

View file

@ -191,7 +191,7 @@ void CEventLoop::wait_for_event(WaitMode mode)
if (!s_timers->is_empty() && queued_events_is_empty) {
gettimeofday(&now, nullptr);
get_next_timer_expiration(timeout);
AK::timeval_sub(&timeout, &now, &timeout);
timeval_sub(timeout, now, timeout);
} else {
should_wait_forever = true;
}