File indexing completed on 2024-05-05 17:50:04

0001 /*
0002    Copyright (C) 2014 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #include "platformtime.h"
0025 
0026 #ifdef _WIN32
0027 // GetTickCount64() requires Vista or greater which is NT version 0x0600
0028 #define _WIN32_WINNT 0x0600
0029 #define WIN32_LEAN_AND_MEAN
0030 #include "windows.h"
0031 #elif defined(__linux__)
0032 #include <time.h>
0033 #else
0034 #include <chrono>
0035 #endif
0036 
0037 namespace PlatformTime
0038 {
0039 
0040 uint64 monotonicMsecs()
0041 {
0042 #ifdef _WIN32
0043     return GetTickCount64();
0044 #elif defined(__linux__)
0045     timespec tspec;
0046     // performance note: at least on Linux AMD64, clock_gettime(CLOCK_MONOTONIC) does not (usually?)
0047     // make a syscall, so it's surprisingly cheap; presumably it uses some built-in CPU timer feature
0048     clock_gettime(CLOCK_MONOTONIC, &tspec);
0049     return uint64(tspec.tv_sec) * 1000 + uint64(tspec.tv_nsec) / 1000000;
0050 #else
0051     auto ret = uint64(std::chrono::duration_cast<std::chrono::milliseconds>(
0052                             std::chrono::steady_clock::now().time_since_epoch()).count());
0053     return ret;
0054 #endif
0055 }
0056 
0057 }