Make atexit callbacks threadsafe (#930)

This commit is contained in:
Ebrahim Byagowi 2018-03-30 05:00:28 +04:30 committed by Behdad Esfahbod
parent d3a432a7b2
commit 70d36543aa
9 changed files with 105 additions and 51 deletions

View File

@ -724,8 +724,14 @@ static HB_LOCALE_T C_locale;
static void
free_C_locale (void)
{
if (C_locale)
HB_FREE_LOCALE (C_locale);
retry:
HB_LOCALE_T locale = (HB_LOCALE_T) hb_atomic_ptr_get (&C_locale);
if (!hb_atomic_ptr_cmpexch (&C_locale, locale, nullptr))
goto retry;
if (locale)
HB_FREE_LOCALE (locale);
}
#endif

View File

@ -423,7 +423,12 @@ static hb_font_funcs_t *static_ft_funcs = nullptr;
static
void free_static_ft_funcs (void)
{
hb_font_funcs_destroy (static_ft_funcs);
retry:
hb_font_funcs_t *ft_funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ft_funcs);
if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, ft_funcs, nullptr))
goto retry;
hb_font_funcs_destroy (ft_funcs);
}
#endif
@ -685,7 +690,12 @@ static FT_Library ft_library;
static
void free_ft_library (void)
{
FT_Done_FreeType (ft_library);
retry:
FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
if (!hb_atomic_ptr_cmpexch (&ft_library, library, nullptr))
goto retry;
FT_Done_FreeType (library);
}
#endif

View File

@ -370,7 +370,12 @@ static hb_unicode_funcs_t *static_glib_funcs = nullptr;
static
void free_static_glib_funcs (void)
{
hb_unicode_funcs_destroy (static_glib_funcs);
retry:
hb_unicode_funcs_t *glib_funcs = (hb_unicode_funcs_t *) hb_atomic_ptr_get (&static_glib_funcs);
if (!hb_atomic_ptr_cmpexch (&static_glib_funcs, glib_funcs, nullptr))
goto retry;
hb_unicode_funcs_destroy (glib_funcs);
}
#endif

View File

@ -351,7 +351,12 @@ static hb_unicode_funcs_t *static_icu_funcs = nullptr;
static
void free_static_icu_funcs (void)
{
hb_unicode_funcs_destroy (static_icu_funcs);
retry:
hb_unicode_funcs_t *icu_funcs = (hb_unicode_funcs_t *) hb_atomic_ptr_get (&static_icu_funcs);
if (!hb_atomic_ptr_cmpexch (&static_icu_funcs, icu_funcs, nullptr))
goto retry;
hb_unicode_funcs_destroy (icu_funcs);
}
#endif

View File

@ -217,7 +217,12 @@ static hb_font_funcs_t *static_ot_funcs = nullptr;
static
void free_static_ot_funcs (void)
{
hb_font_funcs_destroy (static_ot_funcs);
retry:
hb_font_funcs_t *ot_funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs);
if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, ot_funcs, nullptr))
goto retry;
hb_font_funcs_destroy (ot_funcs);
}
#endif

View File

@ -51,7 +51,12 @@ static const char **static_shaper_list;
static
void free_static_shaper_list (void)
{
free (static_shaper_list);
retry:
const char **shaper_list = (const char **) hb_atomic_ptr_get (&static_shaper_list);
if (!hb_atomic_ptr_cmpexch (&static_shaper_list, shaper_list, nullptr))
goto retry;
free (shaper_list);
}
#endif

View File

@ -44,8 +44,13 @@ static const hb_shaper_pair_t *static_shapers;
static
void free_static_shapers (void)
{
if (unlikely (static_shapers != all_shapers))
free ((void *) static_shapers);
retry:
hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers);
if (!hb_atomic_ptr_cmpexch (&static_shapers, shapers, nullptr))
goto retry;
if (unlikely (shapers != all_shapers))
free ((void *) shapers);
}
#endif

View File

@ -237,7 +237,12 @@ static hb_unicode_funcs_t *static_ucdn_funcs = nullptr;
static
void free_static_ucdn_funcs (void)
{
hb_unicode_funcs_destroy (static_ucdn_funcs);
retry:
hb_unicode_funcs_t *ucdn_funcs = (hb_unicode_funcs_t *) hb_atomic_ptr_get (&static_ucdn_funcs);
if (!hb_atomic_ptr_cmpexch (&static_ucdn_funcs, ucdn_funcs, nullptr))
goto retry;
hb_unicode_funcs_destroy (ucdn_funcs);
}
#endif

View File

@ -223,11 +223,19 @@ struct hb_uniscribe_shaper_funcs_t {
};
static hb_uniscribe_shaper_funcs_t *uniscribe_funcs;
#ifdef HB_USE_ATEXIT
static inline void
free_uniscribe_funcs (void)
{
retry:
hb_uniscribe_shaper_funcs_t *local_uniscribe_funcs =
(hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs);
if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, local_uniscribe_funcs, nullptr))
goto retry;
free (uniscribe_funcs);
}
#endif
static hb_uniscribe_shaper_funcs_t *
hb_uniscribe_shaper_get_funcs (void)