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
23 ==============================================================================
27 #include "../memory/Atomic.h"
31 #if defined(CARLA_OS_MAC)
32 # include <mach/mach_time.h>
33 #elif defined(CARLA_OS_WIN)
34 # include <mmsystem.h>
38 # include <sys/timeb.h>
39 # include <sys/types.h>
41 # include <sys/time.h>
48 static Atomic
<uint32
> lastMSCounterValue
;
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
{
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
;
70 hiResCounterNumerator
= timebase
.numer
;
71 hiResCounterDenominator
= timebase
.denom
* (uint64
) 1000000;
76 static HiResCounterInfo hiResCounterInfo
;
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();
90 #ifdef CLOCK_MONOTONIC_RAW
91 clock_gettime (CLOCK_MONOTONIC_RAW
, &t
);
93 clock_gettime (CLOCK_MONOTONIC
, &t
);
96 return (uint32
) (t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000);
100 //==============================================================================
102 int64
Time::currentTimeMillis() noexcept
107 return ((int64
) tb
.time
) * 1000 + tb
.millitm
;
110 gettimeofday (&tv
, nullptr);
111 return ((int64
) tv
.tv_sec
) * 1000 + tv
.tv_usec
/ 1000;
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
126 if (now
< TimeHelpers::lastMSCounterValue
.get() - 1000U)
127 TimeHelpers::lastMSCounterValue
= now
;
131 TimeHelpers::lastMSCounterValue
= now
;