Win32: Use cairo_atomic_once_t to initialize TLS slot

We were using the double-checked locking pattern, which requires
memory fences to be safe on architectures with weak memory
guarantess (e.g ARM64)

Fixes https://gitlab.freedesktop.org/cairo/cairo/-/issues/897
This commit is contained in:
Luca Bacci 2025-06-16 17:38:26 +02:00
parent 2e2080247f
commit d9a11c3736
2 changed files with 5 additions and 8 deletions

View file

@ -53,7 +53,6 @@ CAIRO_MUTEX_DECLARE (_cairo_ft_unscaled_font_map_mutex)
#if CAIRO_HAS_WIN32_FONT
CAIRO_MUTEX_DECLARE (_cairo_win32_font_face_mutex)
CAIRO_MUTEX_DECLARE (_cairo_win32_font_dc_mutex)
#endif
#if CAIRO_HAS_XLIB_SURFACE

View file

@ -143,16 +143,14 @@ _cairo_win32_scaled_font_init_glyph_path (cairo_win32_scaled_font_t *scaled_font
static HDC
_get_global_font_dc (void)
{
static cairo_atomic_once_t once = CAIRO_ATOMIC_ONCE_INIT;
static DWORD hdc_tls_index;
HDC hdc;
if (!hdc_tls_index) {
CAIRO_MUTEX_LOCK (_cairo_win32_font_dc_mutex);
if (!hdc_tls_index) {
hdc_tls_index = TlsAlloc ();
assert (hdc_tls_index != TLS_OUT_OF_INDEXES);
}
CAIRO_MUTEX_UNLOCK (_cairo_win32_font_dc_mutex);
if (!_cairo_atomic_init_once_enter (&once)) {
hdc_tls_index = TlsAlloc ();
assert (hdc_tls_index != TLS_OUT_OF_INDEXES);
_cairo_atomic_init_once_leave (&once);
}
hdc = TlsGetValue (hdc_tls_index);