Add initial bits for Qt6 support
[carla.git] / source / modules / water / misc / Time.cpp
blobca395b35c217a2267ab9851d238c995998626c72
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2023 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #include "Time.h"
27 #include "../memory/Atomic.h"
29 #include <ctime>
31 #if defined(CARLA_OS_MAC)
32 # include <mach/mach_time.h>
33 #elif defined(CARLA_OS_WIN)
34 # include <mmsystem.h>
35 #endif
37 #ifdef _MSC_VER
38 # include <sys/timeb.h>
39 # include <sys/types.h>
40 #else
41 # include <sys/time.h>
42 #endif
44 namespace water {
46 namespace TimeHelpers
48 static Atomic<uint32> lastMSCounterValue;
50 #ifdef CARLA_OS_MAC
51 /* NB: these are kept outside the HiResCounterInfo struct and initialised to 1 to avoid
52 division-by-zero errors if some other static constructor calls us before this file's
53 static constructors have had a chance to fill them in correctly..
55 static uint64 hiResCounterNumerator = 0, hiResCounterDenominator = 1;
57 struct HiResCounterInfo {
58 HiResCounterInfo()
60 mach_timebase_info_data_t timebase;
61 (void) mach_timebase_info (&timebase);
63 if (timebase.numer % 1000000 == 0)
65 hiResCounterNumerator = timebase.numer / 1000000;
66 hiResCounterDenominator = timebase.denom;
68 else
70 hiResCounterNumerator = timebase.numer;
71 hiResCounterDenominator = timebase.denom * (uint64) 1000000;
76 static HiResCounterInfo hiResCounterInfo;
77 #endif
80 //==============================================================================
82 static uint32 water_millisecondsSinceStartup() noexcept
84 #if defined(CARLA_OS_MAC)
85 return (uint32) ((mach_absolute_time() * TimeHelpers::hiResCounterNumerator) / TimeHelpers::hiResCounterDenominator);
86 #elif defined(CARLA_OS_WIN)
87 return (uint32) timeGetTime();
88 #else
89 timespec t;
90 #ifdef CLOCK_MONOTONIC_RAW
91 clock_gettime (CLOCK_MONOTONIC_RAW, &t);
92 #else
93 clock_gettime (CLOCK_MONOTONIC, &t);
94 #endif
96 return (uint32) (t.tv_sec * 1000 + t.tv_nsec / 1000000);
97 #endif
100 //==============================================================================
102 int64 Time::currentTimeMillis() noexcept
104 #ifdef _MSC_VER
105 struct _timeb tb;
106 _ftime_s (&tb);
107 return ((int64) tb.time) * 1000 + tb.millitm;
108 #else
109 struct timeval tv;
110 gettimeofday (&tv, nullptr);
111 return ((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
112 #endif
115 //==============================================================================
117 uint32 Time::getMillisecondCounter() noexcept
119 const uint32 now = water_millisecondsSinceStartup();
121 if (now < TimeHelpers::lastMSCounterValue.get())
123 // in multi-threaded apps this might be called concurrently, so
124 // make sure that our last counter value only increases and doesn't
125 // go backwards..
126 if (now < TimeHelpers::lastMSCounterValue.get() - 1000U)
127 TimeHelpers::lastMSCounterValue = now;
129 else
131 TimeHelpers::lastMSCounterValue = now;
134 return now;