From 0c13703da3641951bf061cac7c5cef034eda16f9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Oct 2013 09:38:12 +0200 Subject: system/clock: convert to C++ --- src/Timer.cxx | 8 ++--- src/event/Loop.cxx | 8 ++--- src/system/Clock.cxx | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/system/Clock.hxx | 41 ++++++++++++++++++++++ src/system/clock.c | 98 ---------------------------------------------------- src/system/clock.h | 49 -------------------------- 6 files changed, 147 insertions(+), 155 deletions(-) create mode 100644 src/system/Clock.cxx create mode 100644 src/system/Clock.hxx delete mode 100644 src/system/clock.c delete mode 100644 src/system/clock.h (limited to 'src') diff --git a/src/Timer.cxx b/src/Timer.cxx index 75fba03aa..e32ebe04b 100644 --- a/src/Timer.cxx +++ b/src/Timer.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "Timer.hxx" #include "AudioFormat.hxx" -#include "system/clock.h" +#include "system/Clock.hxx" #include @@ -37,7 +37,7 @@ Timer::Timer(const AudioFormat af) void Timer::Start() { - time = monotonic_clock_us(); + time = MonotonicClockUS(); started = true; } @@ -58,7 +58,7 @@ void Timer::Add(int size) unsigned Timer::GetDelay() const { - int64_t delay = (int64_t)(time - monotonic_clock_us()) / 1000; + int64_t delay = (int64_t)(time - MonotonicClockUS()) / 1000; if (delay < 0) return 0; @@ -74,7 +74,7 @@ void Timer::Synchronize() const assert(started); - sleep_duration = time - monotonic_clock_us(); + sleep_duration = time - MonotonicClockUS(); if (sleep_duration > 0) g_usleep(sleep_duration); } diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index faf967f21..e8d9e4b96 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -19,10 +19,10 @@ #include "config.h" #include "Loop.hxx" -#include "system/clock.h" #ifdef USE_EPOLL +#include "system/Clock.hxx" #include "TimeoutMonitor.hxx" #include "SocketMonitor.hxx" #include "IdleMonitor.hxx" @@ -31,7 +31,7 @@ EventLoop::EventLoop(Default) :SocketMonitor(*this), - now_ms(::monotonic_clock_ms()), + now_ms(::MonotonicClockMS()), quit(false), n_events(0), thread(ThreadId::Null()) @@ -114,7 +114,7 @@ EventLoop::Run() assert(!quit); do { - now_ms = ::monotonic_clock_ms(); + now_ms = ::MonotonicClockMS(); /* invoke timers */ @@ -162,7 +162,7 @@ EventLoop::Run() const int n = epoll.Wait(events, MAX_EVENTS, timeout_ms); n_events = std::max(n, 0); - now_ms = ::monotonic_clock_ms(); + now_ms = ::MonotonicClockMS(); assert(!quit); diff --git a/src/system/Clock.cxx b/src/system/Clock.cxx new file mode 100644 index 000000000..347997a44 --- /dev/null +++ b/src/system/Clock.cxx @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Clock.hxx" + +#ifdef WIN32 +#include +#elif defined(__APPLE__) +#include +#else +#include +#ifndef CLOCK_MONOTONIC +#include +#endif +#endif + +unsigned +MonotonicClockMS(void) +{ +#ifdef WIN32 + return GetTickCount(); +#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */ + static mach_timebase_info_data_t base; + if (base.denom == 0) + (void)mach_timebase_info(&base); + + return (unsigned)((mach_absolute_time() * base.numer) + / (1000000 * base.denom)); +#elif defined(CLOCK_MONOTONIC) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +#else + /* we have no monotonic clock, fall back to gettimeofday() */ + struct timeval tv; + gettimeofday(&tv, 0); + return tv.tv_sec * 1000 + tv.tv_usec / 1000; +#endif +} + +uint64_t +MonotonicClockUS(void) +{ +#ifdef WIN32 + LARGE_INTEGER l_value, l_frequency; + + if (!QueryPerformanceCounter(&l_value) || + !QueryPerformanceFrequency(&l_frequency)) + return 0; + + uint64_t value = l_value.QuadPart; + uint64_t frequency = l_frequency.QuadPart; + + if (frequency > 1000000) { + value *= 10000; + value /= frequency / 100; + } else if (frequency < 1000000) { + value *= 10000; + value /= frequency; + value *= 100; + } + + return value; +#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */ + static mach_timebase_info_data_t base; + if (base.denom == 0) + (void)mach_timebase_info(&base); + + return ((uint64_t)mach_absolute_time() * (uint64_t)base.numer) + / (1000 * (uint64_t)base.denom); +#elif defined(CLOCK_MONOTONIC) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000 + (uint64_t)(ts.tv_nsec / 1000); +#else + /* we have no monotonic clock, fall back to gettimeofday() */ + struct timeval tv; + gettimeofday(&tv, 0); + return (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec; +#endif +} + diff --git a/src/system/Clock.hxx b/src/system/Clock.hxx new file mode 100644 index 000000000..7be1127bf --- /dev/null +++ b/src/system/Clock.hxx @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_CLOCK_H +#define MPD_CLOCK_H + +#include "Compiler.h" + +#include + +/** + * Returns the value of a monotonic clock in milliseconds. + */ +gcc_pure +unsigned +MonotonicClockMS(); + +/** + * Returns the value of a monotonic clock in microseconds. + */ +gcc_pure +uint64_t +MonotonicClockUS(); + +#endif diff --git a/src/system/clock.c b/src/system/clock.c deleted file mode 100644 index d987aed48..000000000 --- a/src/system/clock.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "clock.h" - -#ifdef WIN32 -#include -#elif defined(__APPLE__) -#include -#else -#include -#ifndef CLOCK_MONOTONIC -#include -#endif -#endif - -unsigned -monotonic_clock_ms(void) -{ -#ifdef WIN32 - return GetTickCount(); -#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */ - static mach_timebase_info_data_t base; - if (base.denom == 0) - (void)mach_timebase_info(&base); - - return (unsigned)((mach_absolute_time() * base.numer) - / (1000000 * base.denom)); -#elif defined(CLOCK_MONOTONIC) - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000 + ts.tv_nsec / 1000000; -#else - /* we have no monotonic clock, fall back to gettimeofday() */ - struct timeval tv; - gettimeofday(&tv, 0); - return tv.tv_sec * 1000 + tv.tv_usec / 1000; -#endif -} - -uint64_t -monotonic_clock_us(void) -{ -#ifdef WIN32 - LARGE_INTEGER l_value, l_frequency; - - if (!QueryPerformanceCounter(&l_value) || - !QueryPerformanceFrequency(&l_frequency)) - return 0; - - uint64_t value = l_value.QuadPart; - uint64_t frequency = l_frequency.QuadPart; - - if (frequency > 1000000) { - value *= 10000; - value /= frequency / 100; - } else if (frequency < 1000000) { - value *= 10000; - value /= frequency; - value *= 100; - } - - return value; -#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */ - static mach_timebase_info_data_t base; - if (base.denom == 0) - (void)mach_timebase_info(&base); - - return ((uint64_t)mach_absolute_time() * (uint64_t)base.numer) - / (1000 * (uint64_t)base.denom); -#elif defined(CLOCK_MONOTONIC) - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (uint64_t)ts.tv_sec * 1000000 + (uint64_t)(ts.tv_nsec / 1000); -#else - /* we have no monotonic clock, fall back to gettimeofday() */ - struct timeval tv; - gettimeofday(&tv, 0); - return (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec; -#endif -} - diff --git a/src/system/clock.h b/src/system/clock.h deleted file mode 100644 index ae774ff85..000000000 --- a/src/system/clock.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_CLOCK_H -#define MPD_CLOCK_H - -#include "Compiler.h" - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Returns the value of a monotonic clock in milliseconds. - */ -gcc_pure -unsigned -monotonic_clock_ms(void); - -/** - * Returns the value of a monotonic clock in microseconds. - */ -gcc_pure -uint64_t -monotonic_clock_us(void); - -#ifdef __cplusplus -} -#endif - -#endif -- cgit v1.2.3