From 5cee8434fd281ddae949883738e58e83cd577bfc Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Fri, 30 Jul 2021 10:57:18 -0700 Subject: [PATCH] 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 Part-of: --- src/mesa/main/debug_output.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mesa/main/debug_output.c b/src/mesa/main/debug_output.c index 6527aea..d47756c 100644 --- a/src/mesa/main/debug_output.c +++ b/src/mesa/main/debug_output.c @@ -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)); } } -- 2.7.4