From a2b5962b6cf45d1daa25ef7e992723ce8cf144d3 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sat, 26 Jun 2021 18:30:11 -0600 Subject: [PATCH] add new files for PBD::get_microseconds() --- libs/pbd/microseconds.cc | 69 +++++++++++++++++++++++++++++++++++++ libs/pbd/pbd/microseconds.h | 34 ++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 libs/pbd/microseconds.cc create mode 100644 libs/pbd/pbd/microseconds.h diff --git a/libs/pbd/microseconds.cc b/libs/pbd/microseconds.cc new file mode 100644 index 0000000000..da7c38d594 --- /dev/null +++ b/libs/pbd/microseconds.cc @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2021 Paul Davis + * Copyright (C) 2013-2015 Tim Mayberry + * Copyright (C) 2014-2015 John Emmas + * + * 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 + +#ifdef PLATFORM_WINDOWS +#include +#include // for LARGE_INTEGER +#endif + +#include "pbd/microseconds.h" + +#ifdef PLATFORM_WINDOWS +static double timer_rate_usecs = 0.0; +#endif + +void +PBD::microsecond_timer_init () +{ +#ifdef PLATFORM_WINDOWS + LARGE_INTEGER freq; + if (!QueryPerformanceFrequency(&freq) || freq.QuadPart < 1) { + info << X_("Failed to determine frequency of QPC\n") << endmsg; + timer_rate_us = 0; + } else { + timer_rate_us = 1000000.0 / freq.QuadPart; + } +#endif +} + +PBD::microseconds_t +PBD::get_microseconds () +{ +#ifdef PLATFORM_WINDOWS + LARGE_INTEGER time; + + if (time_rate_usecs) { + if (QueryPerformanceCounter (&time)) { + return (microseconds_t) (current_val.QuadPart * timer_rate_us); + } + } + + return (microseconds_t) 0; +#else + struct timespec ts; + if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0) { + /* EEEK! */ + return 0; + } + return (microseconds_t)ts.tv_sec * 1000000 + (ts.tv_nsec / 1000); +#endif +} diff --git a/libs/pbd/pbd/microseconds.h b/libs/pbd/pbd/microseconds.h new file mode 100644 index 0000000000..0b61f9247c --- /dev/null +++ b/libs/pbd/pbd/microseconds.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2021 Paul Davis + * Copyright (C) 2013-2015 Tim Mayberry + * Copyright (C) 2014-2015 John Emmas + * + * 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 __libpbd_microseconds_h__ +#define __libpbd_microseconds_h__ + +#include + +#include "pbd/libpbd_visibility.h" + +namespace PBD { + typedef uint64_t microseconds_t; + LIBPBD_API microseconds_t get_microseconds(); + void microsecond_timer_init (); +} + +#endif /* __libpbd_microseconds_h__ */