LMMS
Loading...
Searching...
No Matches
Time.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
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>
7
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/
10
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.
14
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.
22
23 ==============================================================================
24*/
25
26#include "Time.h"
27#include "../memory/Atomic.h"
28
29#include <ctime>
30
31#if defined(CARLA_OS_MAC)
32# include <mach/mach_time.h>
33#elif defined(CARLA_OS_WIN)
34# include <mmsystem.h>
35#endif
36
37#ifdef _MSC_VER
38# include <sys/timeb.h>
39# include <sys/types.h>
40#else
41# include <sys/time.h>
42#endif
43
44namespace water {
45
46namespace TimeHelpers
47{
49
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..
54 */
55 static uint64 hiResCounterNumerator = 0, hiResCounterDenominator = 1;
56
57 struct HiResCounterInfo {
58 HiResCounterInfo()
59 {
60 mach_timebase_info_data_t timebase;
61 (void) mach_timebase_info (&timebase);
62
63 if (timebase.numer % 1000000 == 0)
64 {
65 hiResCounterNumerator = timebase.numer / 1000000;
66 hiResCounterDenominator = timebase.denom;
67 }
68 else
69 {
70 hiResCounterNumerator = timebase.numer;
71 hiResCounterDenominator = timebase.denom * (uint64) 1000000;
72 }
73 }
74 };
75
76 static HiResCounterInfo hiResCounterInfo;
77 #endif
78}
79
80//==============================================================================
81
83{
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
95
96 return (uint32) (t.tv_sec * 1000 + t.tv_nsec / 1000000);
97#endif
98}
99
100//==============================================================================
101
103{
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
113}
114
115//==============================================================================
116
118{
120
121 if (now < TimeHelpers::lastMSCounterValue.get())
122 {
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)
128 }
129 else
130 {
132 }
133
134 return now;
135}
136
137}
#define noexcept
Definition DistrhoDefines.h:72
Definition Atomic.h:95
struct huft * t
Definition inflate.c:943
Definition Time.cpp:47
static Atomic< uint32 > lastMSCounterValue
Definition Time.cpp:48
int64 currentTimeMillis() noexcept
Definition Time.cpp:102
uint32 getMillisecondCounter() noexcept
Definition Time.cpp:117
Definition AudioSampleBuffer.h:33
static uint32 water_millisecondsSinceStartup() noexcept
Definition Time.cpp:82
unsigned int uint32
Definition water.h:98
unsigned long long uint64
Definition water.h:102
long long int64
Definition water.h:100
#define timeGetTime()
Definition swell-functions.h:83
#define void
Definition unzip.h:396