1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Fix for a very obscure race-condition involving font string initialisation.

This commit is contained in:
jules 2014-04-05 17:39:31 +01:00
parent 897c3c8044
commit f4c83a9411
2 changed files with 34 additions and 23 deletions

View file

@ -317,29 +317,37 @@ void Font::checkTypefaceSuitability()
}
//==============================================================================
const String& Font::getDefaultSansSerifFontName()
struct FontPlaceholderNames
{
static const String name ("<Sans-Serif>");
return name;
FontPlaceholderNames()
: sans ("<Sans-Serif>"),
serif ("<Serif>"),
mono ("<Monospaced>"),
regular ("<Regular>")
{
}
String sans, serif, mono, regular;
};
const FontPlaceholderNames& getFontPlaceholderNames()
{
static FontPlaceholderNames names;
return names;
}
const String& Font::getDefaultSerifFontName()
{
static const String name ("<Serif>");
return name;
}
#if JUCE_MSVC
// This is a workaround for the lack of thread-safety in MSVC's handling of function-local
// statics - if multiple threads all try to create the first Font object at the same time,
// it can cause a race-condition in creating these placeholder strings.
struct FontNamePreloader { FontNamePreloader() { getFontPlaceholderNames(); } };
static FontNamePreloader fnp;
#endif
const String& Font::getDefaultMonospacedFontName()
{
static const String name ("<Monospaced>");
return name;
}
const String& Font::getDefaultStyle()
{
static const String style ("<Regular>");
return style;
}
const String& Font::getDefaultSansSerifFontName() { return getFontPlaceholderNames().sans; }
const String& Font::getDefaultSerifFontName() { return getFontPlaceholderNames().serif; }
const String& Font::getDefaultMonospacedFontName() { return getFontPlaceholderNames().mono; }
const String& Font::getDefaultStyle() { return getFontPlaceholderNames().regular; }
const String& Font::getTypefaceName() const noexcept { return font->typefaceName; }
const String& Font::getTypefaceStyle() const noexcept { return font->typefaceStyle; }

View file

@ -225,10 +225,13 @@ private:
hits.set (0);
misses.set (0);
return glyphs.getLast();
}
return findLeastRecentlyUsedGlyph();
if (CachedGlyphType* g = findLeastRecentlyUsedGlyph())
return g;
addNewGlyphSlots (32);
return glyphs.getLast();
}
void addNewGlyphSlots (int num)
@ -241,8 +244,8 @@ private:
CachedGlyphType* findLeastRecentlyUsedGlyph() const noexcept
{
CachedGlyphType* oldest = glyphs.getLast();
int oldestCounter = oldest->lastAccessCount;
CachedGlyphType* oldest = nullptr;
int oldestCounter = std::numeric_limits<int>::max();
for (int i = glyphs.size() - 1; --i >= 0;)
{