diff --git a/src/hb-common.cc b/src/hb-common.cc index 6972aa5bf5e04fa8c087442d0a9d308cf24ce766..331d255b45f38e12cdf839c6ee6a062bc317594f 100644 --- a/src/hb-common.cc +++ b/src/hb-common.cc @@ -182,7 +182,6 @@ void free_langs (void) static hb_language_item_t * lang_find_or_insert (const char *key) { - retry: hb_language_item_t *first_lang = (hb_language_item_t *) hb_atomic_ptr_get (&langs); @@ -203,8 +202,8 @@ retry: } #ifdef HAVE_ATEXIT - if (!first_lang) /* First person registers atexit() callback. */ - atexit (free_langs); + if (!first_lang) + atexit (free_langs); /* First person registers atexit() callback. */ #endif return lang; diff --git a/src/hb-ft.cc b/src/hb-ft.cc index aa09bd70a7f9c5179c25d791b343176397109f2a..ce68ca751bdf22c9470cbf808ad1f72f15ebd036 100644 --- a/src/hb-ft.cc +++ b/src/hb-ft.cc @@ -398,21 +398,37 @@ hb_ft_font_create (FT_Face ft_face, } +static FT_Library ft_library; + +static +void free_ft_library (void) +{ + FT_Done_FreeType (ft_library); +} + static FT_Library -_get_ft_library (void) +get_ft_library (void) { - static struct ft_library_singleton +retry: + FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library); + + if (unlikely (!library)) { - ft_library_singleton (void) { - FT_Init_FreeType (&ft_library); - } - ~ft_library_singleton (void) { - FT_Done_FreeType (ft_library); + /* Not found; allocate one. */ + if (FT_Init_FreeType (&library)) + return NULL; + + if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) { + FT_Done_FreeType (library); + goto retry; } - FT_Library ft_library; - } ft_library_singleton; - return ft_library_singleton.ft_library; +#ifdef HAVE_ATEXIT + atexit (free_ft_library); /* First person registers atexit() callback. */ +#endif + } + + return library; } static void @@ -431,7 +447,7 @@ hb_ft_font_set_funcs (hb_font_t *font) DEBUG_MSG (FT, font, "Font face has empty blob"); FT_Face ft_face = NULL; - FT_Error err = FT_New_Memory_Face (_get_ft_library (), + FT_Error err = FT_New_Memory_Face (get_ft_library (), (const FT_Byte *) blob_data, blob_length, hb_face_get_index (font->face),