Browse Source

Avoided static-order-of-initialisation problems from causing crashes in the Time::getMillisecondCounter function.

tags/2021-05-28
jules 11 years ago
parent
commit
5930215e09
2 changed files with 21 additions and 16 deletions
  1. +20
    -15
      modules/juce_core/native/juce_mac_SystemStats.mm
  2. +1
    -1
      modules/juce_gui_basics/widgets/juce_TreeView.h

+ 20
- 15
modules/juce_core/native/juce_mac_SystemStats.mm View File

@@ -241,35 +241,41 @@ String SystemStats::getDisplayLanguage()
} }
//============================================================================== //==============================================================================
class HiResCounterHandler
/* NB: these are kept outside the HiResCounterInfo struct and initialised to 1 to avoid
division-by-zero errors if some other static constructor calls us before this file's
static constructors have had a chance to fill them in correctly..
*/
static uint64 hiResCounterNumerator = 0, hiResCounterDenominator = 1;
class HiResCounterInfo
{ {
public: public:
HiResCounterHandler()
HiResCounterInfo()
{ {
mach_timebase_info_data_t timebase; mach_timebase_info_data_t timebase;
(void) mach_timebase_info (&timebase); (void) mach_timebase_info (&timebase);
if (timebase.numer % 1000000 == 0) if (timebase.numer % 1000000 == 0)
{ {
numerator = timebase.numer / 1000000;
denominator = timebase.denom;
hiResCounterNumerator = timebase.numer / 1000000;
hiResCounterDenominator = timebase.denom;
} }
else else
{ {
numerator = timebase.numer;
denominator = timebase.denom * (uint64) 1000000;
hiResCounterNumerator = timebase.numer;
hiResCounterDenominator = timebase.denom * (uint64) 1000000;
} }
highResTimerFrequency = (timebase.denom * (uint64) 1000000000) / timebase.numer; highResTimerFrequency = (timebase.denom * (uint64) 1000000000) / timebase.numer;
highResTimerToMillisecRatio = numerator / (double) denominator;
highResTimerToMillisecRatio = hiResCounterNumerator / (double) hiResCounterDenominator;
} }
inline uint32 millisecondsSinceStartup() const noexcept
uint32 millisecondsSinceStartup() const noexcept
{ {
return (uint32) ((mach_absolute_time() * numerator) / denominator);
return (uint32) ((mach_absolute_time() * hiResCounterNumerator) / hiResCounterDenominator);
} }
inline double getMillisecondCounterHiRes() const noexcept
double getMillisecondCounterHiRes() const noexcept
{ {
return mach_absolute_time() * highResTimerToMillisecRatio; return mach_absolute_time() * highResTimerToMillisecRatio;
} }
@@ -277,15 +283,14 @@ public:
int64 highResTimerFrequency; int64 highResTimerFrequency;
private: private:
uint64 numerator, denominator;
double highResTimerToMillisecRatio; double highResTimerToMillisecRatio;
}; };
static HiResCounterHandler hiResCounterHandler;
static HiResCounterInfo hiResCounterInfo;
uint32 juce_millisecondsSinceStartup() noexcept { return hiResCounterHandler.millisecondsSinceStartup(); }
double Time::getMillisecondCounterHiRes() noexcept { return hiResCounterHandler.getMillisecondCounterHiRes(); }
int64 Time::getHighResolutionTicksPerSecond() noexcept { return hiResCounterHandler.highResTimerFrequency; }
uint32 juce_millisecondsSinceStartup() noexcept { return hiResCounterInfo.millisecondsSinceStartup(); }
double Time::getMillisecondCounterHiRes() noexcept { return hiResCounterInfo.getMillisecondCounterHiRes(); }
int64 Time::getHighResolutionTicksPerSecond() noexcept { return hiResCounterInfo.highResTimerFrequency; }
int64 Time::getHighResolutionTicks() noexcept { return (int64) mach_absolute_time(); } int64 Time::getHighResolutionTicks() noexcept { return (int64) mach_absolute_time(); }
bool Time::setSystemTimeToThisTime() const bool Time::setSystemTimeToThisTime() const


+ 1
- 1
modules/juce_gui_basics/widgets/juce_TreeView.h View File

@@ -744,7 +744,7 @@ public:
void setIndentSize (int newIndentSize); void setIndentSize (int newIndentSize);
/** Searches the tree for an item with the specified identifier. /** Searches the tree for an item with the specified identifier.
The identifer string must have been created by calling TreeViewItem::getItemIdentifierString().
The identifier string must have been created by calling TreeViewItem::getItemIdentifierString().
If no such item exists, this will return false. If the item is found, all of its items If no such item exists, this will return false. If the item is found, all of its items
will be automatically opened. will be automatically opened.
*/ */


Loading…
Cancel
Save