gthread: Use g_atomic() primitives correctly in destructor list

In the Windows destructor list, consistently access
`g_private_destructors` using atomic primitives.

`g_atomic_pointer_compare_and_exchange()` should be equivalent to
`InterlockedCompareExchangePointer()`, but is a bit more understandable
in a general GLib context, and pairs with `g_atomic_pointer_get()`. (I
can’t find a Windows API equivalent for that.)

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #600
This commit is contained in:
Philip Withnall 2020-11-16 16:44:29 +00:00
parent 2d03f99ae4
commit 6bd0a4b297

View File

@ -301,7 +301,7 @@ struct _GPrivateDestructor
GPrivateDestructor *next;
};
static GPrivateDestructor * volatile g_private_destructors;
static GPrivateDestructor *g_private_destructors; /* (atomic) prepend-only */
static CRITICAL_SECTION g_private_lock;
static DWORD
@ -329,7 +329,7 @@ g_private_get_impl (GPrivate *key)
g_thread_abort (errno, "malloc");
destructor->index = impl;
destructor->notify = key->notify;
destructor->next = g_private_destructors;
destructor->next = g_atomic_pointer_get (&g_private_destructors);
/* We need to do an atomic store due to the unlocked
* access to the destructor list from the thread exit
@ -337,13 +337,14 @@ g_private_get_impl (GPrivate *key)
*
* It can double as a sanity check...
*/
if (InterlockedCompareExchangePointer (&g_private_destructors, destructor,
destructor->next) != destructor->next)
if (!g_atomic_pointer_compare_and_exchange (&g_private_destructors,
destructor->next,
destructor))
g_thread_abort (0, "g_private_get_impl(1)");
}
/* Ditto, due to the unlocked access on the fast path */
if (InterlockedCompareExchangePointer (&key->p, impl, NULL) != NULL)
if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
g_thread_abort (0, "g_private_get_impl(2)");
}
LeaveCriticalSection (&g_private_lock);
@ -635,7 +636,7 @@ g_thread_win32_thread_detach (void)
*/
dtors_called = FALSE;
for (dtor = g_private_destructors; dtor; dtor = dtor->next)
for (dtor = g_atomic_pointer_get (&g_private_destructors); dtor; dtor = dtor->next)
{
gpointer value;