mesa: Fix tiny race condition in _mesa_debug_get_id

Two threads enter and see *id == 0.  Both threads update the value.
Upon returning, one of the threads might see the overwritten value some
of the time and the updated value other times.  Use cmpxchg to ensure
that there's only ever one value written to *id.

Reviewed-by: Matt Turner <mattst88@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12136>
This commit is contained in:
Ian Romanick 2021-07-30 10:57:18 -07:00 committed by Marge Bot
parent 5ffbee84a4
commit 5cee8434fd

View file

@ -193,7 +193,8 @@ void
_mesa_debug_get_id(GLuint *id)
{
if (!(*id)) {
*id = p_atomic_inc_return(&PrevDynamicID);
/* Don't update *id if we raced with some other thread. */
p_atomic_cmpxchg(id, 0, p_atomic_inc_return(&PrevDynamicID));
}
}